selectionbox.lua 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. local function color(hex)
  2. return ("blank.png^[noalpha^[colorize:#%06X:255"):format(hex)
  3. end
  4. local function random_color()
  5. return color(math.random(0, 0xFFFFFF))
  6. end
  7. local function random_rotation()
  8. return 2 * math.pi * vector.new(math.random(), math.random(), math.random())
  9. end
  10. local active_selectionbox_entities = 0 -- count active entities
  11. core.register_entity("testentities:selectionbox", {
  12. initial_properties = {
  13. visual = "cube",
  14. infotext = "Punch to randomize rotation, rightclick to toggle rotation"
  15. },
  16. on_activate = function(self)
  17. active_selectionbox_entities = active_selectionbox_entities + 1
  18. local w, h, l = math.random(), math.random(), math.random()
  19. self.object:set_properties({
  20. textures = {random_color(), random_color(), random_color(), random_color(), random_color(), random_color()},
  21. selectionbox = {rotate = true, -w/2, -h/2, -l/2, w/2, h/2, l/2},
  22. visual_size = vector.new(w, h, l),
  23. automatic_rotate = 2 * math.pi * (math.random() - 0.5)
  24. })
  25. assert(self.object:get_properties().selectionbox.rotate)
  26. self.object:set_armor_groups({punch_operable = 1})
  27. self.object:set_rotation(random_rotation())
  28. end,
  29. on_deactivate = function()
  30. active_selectionbox_entities = active_selectionbox_entities - 1
  31. end,
  32. on_punch = function(self)
  33. self.object:set_rotation(random_rotation())
  34. end,
  35. on_rightclick = function(self)
  36. self.object:set_properties({
  37. automatic_rotate = self.object:get_properties().automatic_rotate == 0 and 2 * math.pi * (math.random() - 0.5) or 0
  38. })
  39. end
  40. })
  41. local hud_ids = {}
  42. core.register_globalstep(function()
  43. if active_selectionbox_entities == 0 then
  44. return
  45. end
  46. for _, player in pairs(core.get_connected_players()) do
  47. local offset = player:get_eye_offset()
  48. offset.y = offset.y + player:get_properties().eye_height
  49. local pos1 = vector.add(player:get_pos(), offset)
  50. local raycast = core.raycast(pos1, vector.add(pos1, vector.multiply(player:get_look_dir(), 10)), true, false)
  51. local pointed_thing = raycast()
  52. if pointed_thing.ref == player then
  53. pointed_thing = raycast()
  54. end
  55. local remove_hud_element = true
  56. local pname = player:get_player_name()
  57. local hud_id = hud_ids[pname]
  58. if pointed_thing and pointed_thing.type == "object" then
  59. local ent = pointed_thing.ref:get_luaentity()
  60. if ent and ent.name == "testentities:selectionbox" then
  61. hud_ids[pname] = hud_id or player:hud_add({
  62. type = "text", -- See HUD element types
  63. position = {x=0.5, y=0.5},
  64. text = "X",
  65. number = 0xFF0000,
  66. alignment = {x=0, y=0},
  67. })
  68. local shade = math.random(0, 0xFF)
  69. core.add_particle({
  70. -- Random shade of red for the intersection point
  71. texture = color(0x10000 * shade),
  72. pos = pointed_thing.intersection_point,
  73. size = 0.1
  74. })
  75. core.add_particle({
  76. -- Same shade of green for the corresponding intersection normal
  77. texture = color(0x100 * shade),
  78. pos = vector.add(pointed_thing.intersection_point, pointed_thing.intersection_normal * 0.1),
  79. size = 0.1
  80. })
  81. remove_hud_element = false
  82. end
  83. end
  84. if remove_hud_element and hud_id then
  85. player:hud_remove(hud_id)
  86. hud_ids[pname] = nil
  87. end
  88. end
  89. end)