entities.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. -- Entities that test their callbacks
  2. local message = function(msg)
  3. core.log("action", "[callbacks] "..msg)
  4. core.chat_send_all(msg)
  5. end
  6. local get_object_name = function(obj)
  7. local name = "<nil>"
  8. if obj then
  9. if obj:is_player() then
  10. name = obj:get_player_name()
  11. else
  12. name = "<entity>"
  13. end
  14. end
  15. return name
  16. end
  17. local spos = function(self)
  18. return core.pos_to_string(vector.round(self.object:get_pos()))
  19. end
  20. -- Callback test entity (all callbacks except on_step)
  21. core.register_entity("callbacks:callback", {
  22. initial_properties = {
  23. visual = "upright_sprite",
  24. textures = { "callbacks_callback_entity.png" },
  25. },
  26. on_activate = function(self, staticdata, dtime_s)
  27. message("Callback entity: on_activate! pos="..spos(self).."; dtime_s="..dtime_s)
  28. end,
  29. on_deactivate = function(self, removal)
  30. message("Callback entity: on_deactivate! pos="..spos(self) .. "; removal=" .. tostring(removal))
  31. end,
  32. on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
  33. local name = get_object_name(puncher)
  34. message(
  35. "Callback entity: on_punch! "..
  36. "pos="..spos(self).."; puncher="..name.."; "..
  37. "time_from_last_punch="..time_from_last_punch.."; "..
  38. "tool_capabilities="..tostring(dump(tool_capabilities)).."; "..
  39. "dir="..tostring(dump(dir)).."; damage="..damage)
  40. end,
  41. on_rightclick = function(self, clicker)
  42. local name = get_object_name(clicker)
  43. message("Callback entity: on_rightclick! pos="..spos(self).."; clicker="..name)
  44. end,
  45. on_death = function(self, killer)
  46. local name = get_object_name(killer)
  47. message("Callback entity: on_death! pos="..spos(self).."; killer="..name)
  48. end,
  49. on_attach_child = function(self, child)
  50. local name = get_object_name(child)
  51. message("Callback entity: on_attach_child! pos="..spos(self).."; child="..name)
  52. end,
  53. on_detach_child = function(self, child)
  54. local name = get_object_name(child)
  55. message("Callback entity: on_detach_child! pos="..spos(self).."; child="..name)
  56. end,
  57. on_detach = function(self, parent)
  58. local name = get_object_name(parent)
  59. message("Callback entity: on_detach! pos="..spos(self).."; parent="..name)
  60. end,
  61. get_staticdata = function(self)
  62. message("Callback entity: get_staticdata! pos="..spos(self))
  63. end,
  64. })
  65. -- Only test on_step callback
  66. core.register_entity("callbacks:callback_step", {
  67. visual = "upright_sprite",
  68. textures = { "callbacks_callback_entity_step.png" },
  69. on_step = function(self, dtime)
  70. message("on_step callback entity: on_step! pos="..spos(self).."; dtime="..dtime)
  71. end,
  72. })
  73. -- Callback punch with nil puncher
  74. core.register_entity("callbacks:callback_puncher", {
  75. initial_properties = {
  76. visual = "upright_sprite",
  77. textures = { "callbacks_callback_entity.png" },
  78. infotext = "Callback entity for nil puncher test.",
  79. },
  80. on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
  81. if puncher then
  82. puncher:punch(nil, time_from_last_punch, tool_capabilities, dir)
  83. self.object:punch(nil, time_from_last_punch, tool_capabilities, dir)
  84. else
  85. message(
  86. "Callback entity: on_punch with nil puncher "..
  87. "pos="..spos(self).."; "..
  88. "time_from_last_punch="..time_from_last_punch.."; "..
  89. "tool_capabilities="..dump(tool_capabilities).."; "..
  90. "dir="..dump(dir).."; damage="..damage)
  91. end
  92. end,
  93. })