diff options
| author | Lars Müller <34514239+appgurueu@users.noreply.github.com> | 2022-10-30 16:53:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-30 16:53:14 +0100 |
| commit | 077627181ee2eac3c0dacc3d8dc49825837e474c (patch) | |
| tree | a8a6298198738f2edb30bd7e733a7a0d2005affa /games/devtest | |
| parent | b8292319924994352d56d6111faa73fe315d149a (diff) | |
| download | minetest-077627181ee2eac3c0dacc3d8dc49825837e474c.tar.xz | |
Allow rotating entity selectionboxes (#12379)
Diffstat (limited to 'games/devtest')
| -rw-r--r-- | games/devtest/mods/testentities/init.lua | 1 | ||||
| -rw-r--r-- | games/devtest/mods/testentities/selectionbox.lua | 96 |
2 files changed, 97 insertions, 0 deletions
diff --git a/games/devtest/mods/testentities/init.lua b/games/devtest/mods/testentities/init.lua index 94d8d47c2..6e0f8acb8 100644 --- a/games/devtest/mods/testentities/init.lua +++ b/games/devtest/mods/testentities/init.lua @@ -1,2 +1,3 @@ dofile(minetest.get_modpath("testentities").."/visuals.lua") +dofile(minetest.get_modpath("testentities").."/selectionbox.lua") dofile(minetest.get_modpath("testentities").."/armor.lua") diff --git a/games/devtest/mods/testentities/selectionbox.lua b/games/devtest/mods/testentities/selectionbox.lua new file mode 100644 index 000000000..36a8daa41 --- /dev/null +++ b/games/devtest/mods/testentities/selectionbox.lua @@ -0,0 +1,96 @@ +local function color(hex) + return ("blank.png^[noalpha^[colorize:#%06X:255"):format(hex) +end + +local function random_color() + return color(math.random(0, 0xFFFFFF)) +end + +local function random_rotation() + return 2 * math.pi * vector.new(math.random(), math.random(), math.random()) +end + +local active_selectionbox_entities = 0 -- count active entities + +minetest.register_entity("testentities:selectionbox", { + initial_properties = { + visual = "cube", + infotext = "Punch to randomize rotation, rightclick to toggle rotation" + }, + on_activate = function(self) + active_selectionbox_entities = active_selectionbox_entities + 1 + + local w, h, l = math.random(), math.random(), math.random() + self.object:set_properties({ + textures = {random_color(), random_color(), random_color(), random_color(), random_color(), random_color()}, + selectionbox = {rotate = true, -w/2, -h/2, -l/2, w/2, h/2, l/2}, + visual_size = vector.new(w, h, l), + automatic_rotate = 2 * math.pi * (math.random() - 0.5) + }) + assert(self.object:get_properties().selectionbox.rotate) + self.object:set_armor_groups({punch_operable = 1}) + self.object:set_rotation(random_rotation()) + end, + on_deactivate = function() + active_selectionbox_entities = active_selectionbox_entities - 1 + end, + on_punch = function(self) + self.object:set_rotation(random_rotation()) + end, + on_rightclick = function(self) + self.object:set_properties({ + automatic_rotate = self.object:get_properties().automatic_rotate == 0 and 2 * math.pi * (math.random() - 0.5) or 0 + }) + end +}) + +local hud_ids = {} +minetest.register_globalstep(function() + if active_selectionbox_entities == 0 then + return + end + + for _, player in pairs(minetest.get_connected_players()) do + local offset = player:get_eye_offset() + offset.y = offset.y + player:get_properties().eye_height + local pos1 = vector.add(player:get_pos(), offset) + local raycast = minetest.raycast(pos1, vector.add(pos1, vector.multiply(player:get_look_dir(), 10)), true, false) + local pointed_thing = raycast() + if pointed_thing.ref == player then + pointed_thing = raycast() + end + local remove_hud_element = true + local pname = player:get_player_name() + local hud_id = hud_ids[pname] + if pointed_thing and pointed_thing.type == "object" then + local ent = pointed_thing.ref:get_luaentity() + if ent and ent.name == "testentities:selectionbox" then + hud_ids[pname] = hud_id or player:hud_add({ + hud_elem_type = "text", -- See HUD element types + position = {x=0.5, y=0.5}, + text = "X", + number = 0xFF0000, + alignment = {x=0, y=0}, + }) + local shade = math.random(0, 0xFF) + minetest.add_particle({ + -- Random shade of red for the intersection point + texture = color(0x10000 * shade), + pos = pointed_thing.intersection_point, + size = 0.1 + }) + minetest.add_particle({ + -- Same shade of green for the corresponding intersection normal + texture = color(0x100 * shade), + pos = vector.add(pointed_thing.intersection_point, pointed_thing.intersection_normal * 0.1), + size = 0.1 + }) + remove_hud_element = false + end + end + if remove_hud_element and hud_id then + player:hud_remove(hud_id) + hud_ids[pname] = nil + end + end +end) |
