Fix entity overloading and add missing entity check

- Remove bad clear_screen()
- Use minetest.is_player()
- Fix entity with rotated LCD
- Use on_rotate and ad new rotate function, refactor code
This commit is contained in:
GreenXenith 2018-11-25 15:24:02 -08:00 committed by Auke Kok
parent c7f4185e92
commit a2230bdeb1

108
lcd.lua
View File

@ -128,16 +128,59 @@ local clearscreen = function(pos)
end end
end end
local set_texture = function(ent)
local meta = minetest.get_meta(ent.object:get_pos())
local text = meta:get_string("text")
ent.object:set_properties({
textures = {
generate_texture(create_lines(text))
}
})
end
local get_entity = function(pos)
local lcd_entity
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, o in ipairs(objects) do
local o_entity = o:get_luaentity()
if o_entity and o_entity.name == "digilines_lcd:text" then
if not lcd_entity then
lcd_entity = o_entity
else
-- Remove extras, if any
o:remove()
end
end
end
return lcd_entity
end
local rotate_text = function(pos, param)
local entity = get_entity(pos)
if not entity then
return
end
local lcd_info = lcds[param or minetest.get_node(pos).param2]
if not lcd_info then
return
end
entity.object:set_pos(vector.add(pos, lcd_info.delta))
entity.object:set_yaw(lcd_info.yaw or 0)
end
local prepare_writing = function(pos) local prepare_writing = function(pos)
local lcd_info = lcds[minetest.get_node(pos).param2] local entity = get_entity(pos)
if lcd_info == nil then return end if entity then
local text = minetest.add_entity( set_texture(entity)
{x = pos.x + lcd_info.delta.x, rotate_text(pos)
y = pos.y + lcd_info.delta.y, end
z = pos.z + lcd_info.delta.z}, "digilines_lcd:text") end
text:setyaw(lcd_info.yaw or 0)
--* text:setpitch(lcd_info.yaw or 0) local spawn_entity = function(pos)
return text if not get_entity(pos) then
minetest.add_entity(pos, "digilines_lcd:text")
rotate_text(pos)
end
end end
local on_digiline_receive = function(pos, _, channel, msg) local on_digiline_receive = function(pos, _, channel, msg)
@ -147,7 +190,7 @@ local on_digiline_receive = function(pos, _, channel, msg)
meta:set_string("text", msg) meta:set_string("text", msg)
meta:set_string("infotext", msg) meta:set_string("infotext", msg)
clearscreen(pos)
if msg ~= "" then if msg ~= "" then
prepare_writing(pos) prepare_writing(pos)
end end
@ -165,30 +208,34 @@ minetest.register_node("digilines:lcd", {
inventory_image = "lcd_lcd.png", inventory_image = "lcd_lcd.png",
wield_image = "lcd_lcd.png", wield_image = "lcd_lcd.png",
tiles = {"lcd_anyside.png"}, tiles = {"lcd_anyside.png"},
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
light_source = 6,
paramtype2 = "wallmounted", paramtype2 = "wallmounted",
node_box = lcd_box, node_box = lcd_box,
selection_box = lcd_box, selection_box = lcd_box,
groups = {choppy = 3, dig_immediate = 2}, groups = {choppy = 3, dig_immediate = 2},
after_place_node = function(pos)
after_place_node = function (pos)
local param2 = minetest.get_node(pos).param2 local param2 = minetest.get_node(pos).param2
if param2 == 0 or param2 == 1 then if param2 == 0 or param2 == 1 then
minetest.add_node(pos, {name = "digilines:lcd", param2 = 3}) minetest.add_node(pos, {name = "digilines:lcd", param2 = 3})
end end
prepare_writing (pos) spawn_entity(pos)
prepare_writing(pos)
end, end,
on_construct = reset_meta,
on_construct = function(pos) on_destruct = clearscreen,
reset_meta(pos) on_punch = function(pos, node, puncher, pointed_thing)
if minetest.is_player(puncher) then
spawn_entity(pos)
end
end, end,
on_rotate = function(pos, node, user, mode, new_param2)
on_destruct = function(pos) if mode ~= screwdriver.ROTATE_FACE then
clearscreen(pos) return false
end
rotate_text(pos, new_param2)
end, end,
on_receive_fields = function(pos, _, fields, sender) on_receive_fields = function(pos, _, fields, sender)
local name = sender:get_player_name() local name = sender:get_player_name()
if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then
@ -199,28 +246,27 @@ minetest.register_node("digilines:lcd", {
minetest.get_meta(pos):set_string("channel", fields.channel) minetest.get_meta(pos):set_string("channel", fields.channel)
end end
end, end,
digiline = {
digiline =
{
receptor = {}, receptor = {},
effector = { effector = {
action = on_digiline_receive action = on_digiline_receive
}, },
}, },
})
light_source = 6, minetest.register_lbm({
label = "Replace Missing Text Entities",
name = "digilines:replace_text",
nodenames = {"digilines:lcd"},
run_at_every_load = true,
action = spawn_entity,
}) })
minetest.register_entity(":digilines_lcd:text", { minetest.register_entity(":digilines_lcd:text", {
collisionbox = { 0, 0, 0, 0, 0, 0 }, collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = "upright_sprite", visual = "upright_sprite",
textures = {}, textures = {},
on_activate = set_texture,
on_activate = function(self)
local meta = minetest.get_meta(self.object:getpos())
local text = meta:get_string("text")
self.object:set_properties({textures={generate_texture(create_lines(text))}})
end
}) })
minetest.register_craft({ minetest.register_craft({