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 0000000..3d42a7d Binary files /dev/null and b/textures/building_lib_arrow.png differ