working display

This commit is contained in:
BuckarooBanzay 2023-06-23 14:11:49 +02:00
parent 4938c09b27
commit 1e116e3a16
6 changed files with 40 additions and 61 deletions

View File

@ -7,8 +7,8 @@ minetest.register_entity("pick_and_place:display", {
physical = false,
static_save = false,
collisionbox = {0, 0, 0, 0, 0, 0},
visual = "upright_sprite",
visual_size = {x=10, y=10},
visual = "cube",
visual_size = {x=1, y=1, z=1},
glow = 10
},
on_step = function(self)

View File

@ -3,6 +3,7 @@ pick_and_place = {}
local MP = minetest.get_modpath("pick_and_place")
dofile(MP .. "/common.lua")
dofile(MP .. "/pointed.lua")
dofile(MP .. "/serialize.lua")
dofile(MP .. "/entity.lua")
dofile(MP .. "/node.lua")

12
pointed.lua Normal file
View File

@ -0,0 +1,12 @@
--- returns the pointed position
function pick_and_place.get_pointed_position(player, distance)
distance = distance or 10
local ppos = player:get_pos()
local eye_height = player:get_properties().eye_height
ppos = vector.add(ppos, {x=0, y=eye_height, z=0})
local look_dir = player:get_look_dir()
local pos = vector.add(ppos, vector.multiply(look_dir, distance))
return vector.round(pos)
end

View File

@ -2,16 +2,8 @@
-- playername => key
local active_preview = {}
local function add_preview_entity(texture, key, visual_size, pos, rotation)
local ent = pick_and_place.add_entity(pos, key)
ent:set_properties({
visual_size = visual_size,
textures = {texture}
})
ent:set_rotation(rotation)
end
function pick_and_place.show_preview(playername, texture, color, pos1, pos2)
pos2 = pos2 or pos1
texture = texture .. "^[colorize:" .. color
local key =
@ -27,51 +19,22 @@ function pick_and_place.show_preview(playername, texture, color, pos1, pos2)
pick_and_place.clear_preview(playername)
active_preview[playername] = key
local size = vector.subtract(pos2, pos1)
local half_size = vector.divide(size, 2) -- 8 .. n
-- z-
add_preview_entity(texture, key,
{x=size.x, y=size.y},
vector.add(pos1, {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, key,
{x=size.x, y=size.y},
vector.add(pos1, {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, key,
{x=size.z, y=size.y},
vector.add(pos1, {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, key,
{x=size.z, y=size.y},
vector.add(pos1, {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, key,
{x=size.x, y=size.z},
vector.add(pos1, {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, key,
{x=size.x, y=size.z},
vector.add(pos1, {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}
)
local visual_size = vector.add(vector.subtract(pos2, pos1), 1)
local offset = vector.divide(vector.subtract(pos2, pos1), 2)
local origin = vector.subtract(pos1, offset)
local ent = pick_and_place.add_entity(origin, key)
ent:set_properties({
visual_size = visual_size,
textures = {
texture,
texture,
texture,
texture,
texture,
texture
}
})
end
function pick_and_place.clear_preview(playername)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@ -7,13 +7,16 @@ minetest.register_tool("pick_and_place:placer", {
on_use = function(itemstack, player)
print("on_use: " .. itemstack:get_name() .. ", " .. player:get_player_name())
end,
on_focus = function(itemstack, player)
print("on_focus: " .. itemstack:get_name() .. ", " .. player:get_player_name())
on_secondary_use = function(itemstack, player)
print("on_secondary_use: " .. itemstack:get_name() .. ", " .. player:get_player_name())
end,
on_step = function(itemstack, player)
print("on_step: " .. itemstack:get_name() .. ", " .. player:get_player_name())
on_step = function(_, player)
local playername = player:get_player_name()
local pointed_pos = pick_and_place.get_pointed_position(player)
pick_and_place.show_preview(playername, "pick_and_place_plus.png", "#00ff00", pointed_pos, vector.add(pointed_pos, {x=1, y=2, z=3}))
end,
on_blur = function(itemstack, player)
print("on_blur: " .. itemstack:get_name() .. ", " .. player:get_player_name())
on_deselect = function(_, player)
local playername = player:get_player_name()
pick_and_place.clear_preview(playername)
end
})