entity rework

This commit is contained in:
BuckarooBanzay 2022-11-07 20:37:44 +01:00
parent e969b6c8a7
commit 0bb615b1e9
4 changed files with 57 additions and 48 deletions

32
entity.lua Normal file
View File

@ -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

View File

@ -5,7 +5,8 @@ building_lib = {
} }
local MP = minetest.get_modpath("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 .. "/api.lua")
dofile(MP .. "/wield_events.lua") dofile(MP .. "/wield_events.lua")
dofile(MP .. "/common.lua") dofile(MP .. "/common.lua")

View File

@ -1,39 +1,10 @@
-- playername => minetest.pos_to_string(pos1) .. "/" .. minetest.pos_to_string(pos2) -- playername => key
local active_entities = {} 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", { local function add_preview_entity(texture, key, visual_size, pos, rotation)
func = function(name) local ent = building_lib.add_entity(pos, key)
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
ent:set_properties({ ent:set_properties({
visual_size = visual_size, visual_size = visual_size,
textures = {texture} textures = {texture}
@ -41,19 +12,17 @@ local function add_preview_entity(texture, playername, key, visual_size, pos, ro
ent:set_rotation(rotation) ent:set_rotation(rotation)
end end
function building_lib.has_preview(playername)
return active_entities[playername]
end
function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_pos2) function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_pos2)
mapblock_pos2 = mapblock_pos2 or mapblock_pos1 mapblock_pos2 = mapblock_pos2 or mapblock_pos1
local key = minetest.pos_to_string(mapblock_pos1) .. "/" .. minetest.pos_to_string(mapblock_pos2) .. "/" .. texture 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 -- already active on the same region
return return
end 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) 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 local half_size = vector.divide(size, 2) -- 8 .. n
-- z- -- z-
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.x, y=size.y}, {x=size.x, y=size.y},
vector.add(min, {x=half_size.x-0.5, y=half_size.y-0.5, z=-0.5}), vector.add(min, {x=half_size.x-0.5, y=half_size.y-0.5, z=-0.5}),
{x=0, y=0, z=0} {x=0, y=0, z=0}
) )
-- z+ -- z+
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.x, y=size.y}, {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}), 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=0, y=0, z=0}
) )
-- x- -- x-
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.z, y=size.y}, {x=size.z, y=size.y},
vector.add(min, {x=-0.5, y=half_size.y-0.5, z=half_size.z-0.5}), 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=0, y=math.pi/2, z=0}
) )
-- x+ -- x+
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.z, y=size.y}, {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}), 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} {x=0, y=math.pi/2, z=0}
) )
-- y- -- y-
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.x, y=size.z}, {x=size.x, y=size.z},
vector.add(min, {x=half_size.x-0.5, y=-0.5, z=half_size.z-0.5}), 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} {x=math.pi/2, y=0, z=0}
) )
-- y+ -- y+
add_preview_entity(texture, playername, key, add_preview_entity(texture, key,
{x=size.x, y=size.z}, {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}), 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} {x=math.pi/2, y=0, z=0}
@ -105,5 +74,12 @@ function building_lib.show_preview(texture, playername, mapblock_pos1, mapblock_
end end
function building_lib.clear_preview(playername) function building_lib.clear_preview(playername)
active_entities[playername] = nil if active_preview[playername] then
building_lib.remove_entities(active_preview[playername])
active_preview[playername] = nil
end
end end
minetest.register_on_leaveplayer(function(player)
building_lib.clear_preview(player:get_player_name())
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB