minetest-guns/bullet.lua
Jouni 6e3c930fb7 Changes
- allow setting all bullet properties
- change back to bullets not colliding first
- add sounds
- add magazine cooldown
2023-04-26 17:21:20 +03:00

68 lines
1.7 KiB
Lua

local bullet = {
timer = 0,
initial_properties = {
static_save = false,
collide_with_objects = false,
physical = true
},
on_collision = {}
}
bullet.register_on_collision = function(self, func)
if type(func) == "function" then
table.insert(self.on_collision, func)
end
end
bullet.run_on_collision = function(self, itemstack, player, collision)
for _, func in ipairs(self.on_collision) do
func(itemstack, player, collision)
end
end
bullet.on_step = function(self, dtime, moveresult)
self.timer = self.timer + dtime
if self.timer < 10 and self.owner and self.itemstack then
if moveresult.collides then
local col = moveresult.collisions[1]
if col then
self:run_on_collision(self.itemstack, self.owner, col)
if col.type == "node" then
local node = minetest.registered_nodes[minetest.get_node(col.node_pos).name]
if node and node.tiles then
for i=1,math.random(4,8) do
if node.tiles[1] then
minetest.add_particle({
pos = self.object:get_pos(),
velocity = {
x=math.random(-10,10),
y=math.random(-10,10),
z=math.random(-10,10)
},
expirationtime = 0.5,
size = math.random(1,2),
collisiondetection = true,
bounce = {0.5, 2},
texture = node.tiles[1].."^[resize:4x4"
})
end
end
end
elseif col.type == "object" then
local dmg = {full_punch_interval = 1, damage_groups = self.damage}
col.object:punch(self.owner, 1, dmg, nil)
end
self.object:remove()
end
elseif self.timer > 0.05 then
self.object:set_properties({
collide_with_objects = true
})
end
else
self.object:remove()
end
end
minetest.register_entity("guns:bullet", bullet)