snowball.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. local function throw_snowball(item, player)
  2. local playerpos = player:get_pos()
  3. local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.625,z=playerpos.z}, 'epic:snowball')
  4. local dir = player:get_look_dir()
  5. obj:set_velocity({x=dir.x*30, y=dir.y*30, z=dir.z*30})
  6. obj:set_acceleration({x=dir.x*-3, y=-dir.y^8*80-10, z=dir.z*-3})
  7. item:take_item()
  8. return item
  9. end
  10. minetest.register_entity('epic:snowball', {
  11. timer=0,
  12. collisionbox = {0,0,0,0,0,0},
  13. physical = false,
  14. textures = {'default_snowball.png'},
  15. lastpos={},
  16. on_step = function(self, dtime)
  17. self.timer = self.timer + dtime
  18. local pos = self.object:get_pos()
  19. local node = minetest.get_node(pos)
  20. local objs = minetest.env:get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 1)
  21. for k, obj in pairs(objs) do
  22. if obj:get_luaentity() ~= nil then
  23. if obj:get_luaentity().name ~= "epic:snowball" and obj:get_luaentity().name ~= "__builtin:item" then
  24. local damage = 1
  25. obj:punch(self.object, 1.0, {
  26. full_punch_interval=1.0,
  27. damage_groups={fleshy=damage},
  28. }, nil)
  29. self.object:remove()
  30. end
  31. else
  32. local damage = 1
  33. obj:punch(self.object, 1.0, {
  34. full_punch_interval=1.0,
  35. damage_groups={fleshy=damage},
  36. }, nil)
  37. self.object:remove()
  38. end
  39. end
  40. if self.lastpos.x~=nil then
  41. if node.name ~= 'air' then
  42. self.object:remove()
  43. minetest.set_node(self.lastpos, {name='epic:snow'})
  44. minetest.check_for_falling(self.lastpos)
  45. end
  46. end
  47. self.lastpos={x=pos.x, y=pos.y, z=pos.z}
  48. end
  49. })
  50. minetest.override_item('default:snow', {
  51. on_use = throw_snowball
  52. })
  53. minetest.override_item('trail:snow', {
  54. on_use = throw_snowball
  55. })
  56. minetest.register_node("epic:snow", {
  57. description = "Snow",
  58. tiles = {"default_snow.png"},
  59. inventory_image = "default_snowball.png",
  60. wield_image = "default_snowball.png",
  61. paramtype = "light",
  62. buildable_to = true,
  63. floodable = true,
  64. drawtype = "nodebox",
  65. node_box = {
  66. type = "fixed",
  67. fixed = {
  68. {-0.5, -0.5, -0.5, 0.5, -0.25, 0.5},
  69. },
  70. },
  71. collision_box = {
  72. type = "fixed",
  73. fixed = {
  74. {-0.5, -0.5, -0.5, 0.5, -6 / 16, 0.5},
  75. },
  76. },
  77. groups = {crumbly = 3, falling_node = 1, snowy = 1, not_in_creative_inventory=1},
  78. sounds = default.node_sound_snow_defaults(),
  79. on_construct = function(pos)
  80. pos.y = pos.y - 1
  81. if minetest.get_node(pos).name == "default:dirt_with_grass" then
  82. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  83. end
  84. end,
  85. })
  86. minetest.register_abm({
  87. nodenames = {'epic:snow'},
  88. interval = 30,
  89. chance = 15,
  90. action = function(pos)
  91. minetest.set_node(pos, {name = 'air'})
  92. end
  93. })