From 0bb615b1e9b34226b8f69a61b5694cc06bef62c6 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 7 Nov 2022 20:37:44 +0100 Subject: [PATCH] entity rework --- entity.lua | 32 +++++++++++++++ init.lua | 3 +- display.lua => preview.lua | 70 +++++++++++--------------------- textures/building_lib_arrow.png | Bin 0 -> 5165 bytes 4 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 entity.lua rename display.lua => preview.lua (50%) create mode 100644 textures/building_lib_arrow.png diff --git a/entity.lua b/entity.lua new file mode 100644 index 0000000..486e825 --- /dev/null +++ b/entity.lua @@ -0,0 +1,32 @@ + +-- id -> true +local active_entities = {} + +minetest.register_entity("building_lib:display", { + initial_properties = { + physical = false, + static_save = false, + collisionbox = {0, 0, 0, 0, 0, 0}, + visual = "upright_sprite", + visual_size = {x=10, y=10}, + glow = 10 + }, + on_step = function(self) + if not active_entities[self.id] then + -- not valid anymore + self.object:remove() + end + end +}) + +function building_lib.add_entity(pos, id) + active_entities[id] = true + local ent = minetest.add_entity(pos, "building_lib:display") + local luaent = ent:get_luaentity() + luaent.id = id + return ent +end + +function building_lib.remove_entities(id) + active_entities[id] = nil +end diff --git a/init.lua b/init.lua index 9e4af27..bc2c67c 100644 --- a/init.lua +++ b/init.lua @@ -5,7 +5,8 @@ building_lib = { } local MP = minetest.get_modpath("building_lib") -dofile(MP .. "/display.lua") +dofile(MP .. "/entity.lua") +dofile(MP .. "/preview.lua") dofile(MP .. "/api.lua") dofile(MP .. "/wield_events.lua") dofile(MP .. "/common.lua") diff --git a/display.lua b/preview.lua similarity index 50% rename from display.lua rename to preview.lua index 5e7c0f2..815bcf7 100644 --- a/display.lua +++ b/preview.lua @@ -1,39 +1,10 @@ --- playername => minetest.pos_to_string(pos1) .. "/" .. minetest.pos_to_string(pos2) -local active_entities = {} +-- playername => key +local active_preview = {} -minetest.register_entity("building_lib:display", { - initial_properties = { - physical = false, - static_save = false, - collisionbox = {0, 0, 0, 0, 0, 0}, - visual = "upright_sprite", - visual_size = {x=10, y=10}, - glow = 10 - }, - on_step = function(self) - local entry = active_entities[self.playername] - if not entry or entry ~= self.key then - -- not valid anymore - self.object:remove() - end - end -}) -minetest.register_chatcommand("test", { - func = function(name) - local player = minetest.get_player_by_name(name) - local pos = player:get_pos() - local mapblock_pos = mapblock_lib.get_mapblock(pos) - building_lib.show_preview(name, mapblock_pos, vector.add(mapblock_pos, {x=1, y=1, z=0})) - end -}) - -local function add_preview_entity(texture, playername, key, visual_size, pos, rotation) - local ent = minetest.add_entity(pos, "building_lib:display") - local luaent = ent:get_luaentity() - luaent.playername = playername - luaent.key = key +local function add_preview_entity(texture, key, visual_size, pos, rotation) + local ent = building_lib.add_entity(pos, key) ent:set_properties({ visual_size = visual_size, textures = {texture} @@ -41,19 +12,17 @@ local function add_preview_entity(texture, playername, key, visual_size, pos, ro ent:set_rotation(rotation) end -function building_lib.has_preview(playername) - return active_entities[playername] -end - function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_pos2) mapblock_pos2 = mapblock_pos2 or mapblock_pos1 local key = minetest.pos_to_string(mapblock_pos1) .. "/" .. minetest.pos_to_string(mapblock_pos2) .. "/" .. texture - if active_entities[playername] == key then + if active_preview[playername] == key then -- already active on the same region return end - active_entities[playername] = key + -- clear previous entities + building_lib.clear_preview(playername) + active_preview[playername] = key local min, _ = mapblock_lib.get_mapblock_bounds_from_mapblock(mapblock_pos1) @@ -62,42 +31,42 @@ function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_ local half_size = vector.divide(size, 2) -- 8 .. n -- z- - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.x, y=size.y}, vector.add(min, {x=half_size.x-0.5, y=half_size.y-0.5, z=-0.5}), {x=0, y=0, z=0} ) -- z+ - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.x, y=size.y}, vector.add(min, {x=half_size.x-0.5, y=half_size.y-0.5, z=size.z-0.5}), {x=0, y=0, z=0} ) -- x- - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.z, y=size.y}, vector.add(min, {x=-0.5, y=half_size.y-0.5, z=half_size.z-0.5}), {x=0, y=math.pi/2, z=0} ) -- x+ - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.z, y=size.y}, vector.add(min, {x=size.x-0.5, y=half_size.y-0.5, z=half_size.z-0.5}), {x=0, y=math.pi/2, z=0} ) -- y- - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.x, y=size.z}, vector.add(min, {x=half_size.x-0.5, y=-0.5, z=half_size.z-0.5}), {x=math.pi/2, y=0, z=0} ) -- y+ - add_preview_entity(texture, playername, key, + add_preview_entity(texture, key, {x=size.x, y=size.z}, vector.add(min, {x=half_size.x-0.5, y=size.y-0.5, z=half_size.z-0.5}), {x=math.pi/2, y=0, z=0} @@ -105,5 +74,12 @@ function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_ end function building_lib.clear_preview(playername) - active_entities[playername] = nil -end \ No newline at end of file + if active_preview[playername] then + building_lib.remove_entities(active_preview[playername]) + active_preview[playername] = nil + end +end + +minetest.register_on_leaveplayer(function(player) + building_lib.clear_preview(player:get_player_name()) +end) \ No newline at end of file diff --git a/textures/building_lib_arrow.png b/textures/building_lib_arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..3d42a7d7d4e136aade26e729888f813adcd02a75 GIT binary patch literal 5165 zcmeHKX;>5I77kmXY(`6qDAh46f|6vC$u3bKVF?-<0R^lTCYebXfh0|W30$ofwHBo; zm)0tEK|#c;(Az347A-{)sES~vrNC7wP+W@(;8LyhOF)I^_K(lK{b!zMl9}_J^PY3w zbH0=LvSm@>E{@Y2NhFesBtjewe#z#`;T7;bb8rMDk!;E`W8)3cs1efYG)f{BhYT57 z9Kub6l0-6fRxC~Vp$l?;+~S{U>wSmZSeK;CTtB1MsUhkvTe5T0_NnBMDV?w9CX75* z_q)&VZE1K|K5Jvro$9_RD(5zJ3-c0o(bSTC(VnZ*2f*U26<-l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