diff --git a/entity.lua b/entity.lua index 918a9d5..5bbbc2f 100644 --- a/entity.lua +++ b/entity.lua @@ -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) diff --git a/init.lua b/init.lua index c567b08..fbfc619 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/pointed.lua b/pointed.lua new file mode 100644 index 0000000..272b40c --- /dev/null +++ b/pointed.lua @@ -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 diff --git a/preview.lua b/preview.lua index 017470c..762efb1 100644 --- a/preview.lua +++ b/preview.lua @@ -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) diff --git a/textures/pick_and_place_plus.png b/textures/pick_and_place_plus.png index 9e93c60..1e7b5a2 100644 Binary files a/textures/pick_and_place_plus.png and b/textures/pick_and_place_plus.png differ diff --git a/tool.lua b/tool.lua index c19ac92..1366f8c 100644 --- a/tool.lua +++ b/tool.lua @@ -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 })