forcegun.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. minetest.register_tool("rangedweapons:forcegun", {
  2. description = "" ..core.colorize("#35cdff","Force gun\n") ..core.colorize("#FFFFFF", "Completelly harmless... by itself...\n")..core.colorize("#FFFFFF", "It's projectile will push either the entity it hits directly, or everyone near the node it collides with far away.\n") ..core.colorize("#FFFFFF", "Perfect for rocket-jumping or YEETing enemies away.\n")..core.colorize("#FFFFFF", "Power usage: 40\n")..core.colorize("#FFFFFF", "Gun type:Power Special-gun\n") ..core.colorize("#FFFFFF", "Bullet velocity: 60"),
  3. range = 0,
  4. wield_scale = {x=2.0,y=2.0,z=1.75},
  5. inventory_image = "rangedweapons_forcegun.png",
  6. on_use = function(itemstack, user, pointed_thing)
  7. local pos = user:get_pos()
  8. local dir = user:get_look_dir()
  9. local yaw = user:get_look_yaw()
  10. local inv = user:get_inventory()
  11. if inv:contains_item("main", "rangedweapons:power_particle 40") then
  12. if pos and dir then
  13. inv:remove_item("main", "rangedweapons:power_particle 25")
  14. pos.y = pos.y + 1.5
  15. local obj = minetest.add_entity(pos, "rangedweapons:forceblast")
  16. if obj then
  17. minetest.sound_play("rangedweapons_rocket", {object=obj})
  18. obj:set_velocity({x=dir.x * 60, y=dir.y * 60, z=dir.z * 60})
  19. obj:setyaw(yaw + math.pi)
  20. proj_dir = dir
  21. local ent = obj:get_luaentity()
  22. if ent then
  23. ent.player = ent.player or user
  24. end
  25. end
  26. end
  27. end
  28. end,
  29. })
  30. local rangedweapons_forceblast = {
  31. timer = 0,
  32. initial_properties = {
  33. physical = true,
  34. hp_max = 420,
  35. glow = 30,
  36. visual = "sprite",
  37. visual_size = {x=0.4, y=0.4,},
  38. textures = {"rangedweapons_force_bullet.png"},
  39. lastpos = {},
  40. collide_with_objects = false,
  41. collisionbox = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
  42. },
  43. }
  44. rangedweapons_forceblast.on_step = function(self, dtime, moveresult)
  45. self.timer = self.timer + dtime
  46. local pos = self.object:get_pos()
  47. proj_dir = proj_dir or ({x=0,y=0,z=0})
  48. if self.timer > 10 then
  49. self.object:remove()
  50. end
  51. if self.timer > 0.05 then
  52. self.object:set_properties({collide_with_objects = true})
  53. end
  54. if moveresult.collides == true then
  55. if moveresult.collisions[1] ~= nil then
  56. if moveresult.collisions[1].type == "object" then
  57. if moveresult.collisions[1].object:is_player() then
  58. moveresult.collisions[1].object:add_player_velocity({x=proj_dir.x * 20, y=5+ (proj_dir.y * 20), z=proj_dir.z * 20})
  59. else
  60. moveresult.collisions[1].object:add_velocity({x=proj_dir.x * 20, y=5+ (proj_dir.y * 20), z=proj_dir.z * 20})
  61. end
  62. minetest.add_particle({
  63. pos = ({x = pos.x, y = pos.y, z = pos.z}),
  64. velocity ={x=0,y=0,z=0},
  65. acceleration ={x=0,y=0,z=0},
  66. expirationtime = 0.20,
  67. size = 16,
  68. collisiondetection = true,
  69. collision_removal = false,
  70. vertical = false,
  71. texture = "rangedweapons_force_blast.png",
  72. glow = 20,
  73. animation = {type="vertical_frames", aspect_w=64, aspect_h=64, length = 0.20,},
  74. })
  75. self.object:remove()
  76. end
  77. if moveresult.collisions[1].type == "node" then
  78. local objs = minetest.get_objects_inside_radius({x = pos.x, y = pos.y, z = pos.z}, 7)
  79. for k, obj in pairs(objs) do
  80. if obj:get_pos() then
  81. posd_x = pos.x - obj:get_pos().x
  82. posd_y = pos.y - obj:get_pos().y
  83. posd_z = pos.z - obj:get_pos().z
  84. else
  85. posd_x = 1
  86. posd_y = 1
  87. posd_z = 1
  88. end
  89. if posd_y < 0 and posd_y > -1 then posd_y = -1 end
  90. if posd_y > 0 and posd_y < 1 then posd_y = 1 end
  91. if posd_y > 0 then posd_y=posd_y*3 end
  92. posd_y = (posd_y + 0.5) * (((math.abs(posd_x)+0.5)+(math.abs(posd_z)+0.5))/2)
  93. if posd_y > -1.0 and posd_y < 0 then posd_y = -1.0 end
  94. if obj:get_luaentity() ~= nil then
  95. if obj:get_luaentity().name ~= "rangedweapons:forceblast" then
  96. obj:add_velocity({x=10*(-posd_x), y=30*(-1/posd_y), z=10*(-posd_z)})
  97. self.object:remove()
  98. end
  99. else
  100. obj:add_player_velocity({x=30*((-posd_x)/(1+math.abs(posd_x))), y=25*(-1/posd_y), z=30*((-posd_z)/(1+math.abs(posd_z)))})
  101. self.object:remove()
  102. end
  103. end
  104. minetest.add_particle({
  105. pos = ({x = pos.x, y = pos.y, z = pos.z}),
  106. velocity ={x=0,y=0,z=0},
  107. acceleration ={x=0,y=0,z=0},
  108. expirationtime = 0.20,
  109. size = 128,
  110. collisiondetection = true,
  111. collision_removal = false,
  112. vertical = false,
  113. texture = "rangedweapons_force_blast.png",
  114. glow = 20,
  115. animation = {type="vertical_frames", aspect_w=64, aspect_h=64, length = 0.20,},
  116. })
  117. self.object:remove()
  118. end
  119. self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
  120. end
  121. end
  122. end
  123. minetest.register_entity("rangedweapons:forceblast", rangedweapons_forceblast)