71 lines
2.0 KiB
Lua
71 lines
2.0 KiB
Lua
local bullet = {
|
|
timer = 0,
|
|
initial_properties = {
|
|
collide_with_objects = false,
|
|
physical = true,
|
|
visual = "wielditem"
|
|
},
|
|
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.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
|
|
if col.object == self.owner then
|
|
return
|
|
end
|
|
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
|
|
--This is a hack. There is probably a way to calculate the
|
|
--position where the player is looking a little ways away and
|
|
--spawn the bullet there so that the it doesn't collide with
|
|
--the player, but i don't know how to do that...
|
|
self.object:set_properties({
|
|
collide_with_objects = true
|
|
})
|
|
end
|
|
else
|
|
self.object:remove()
|
|
end
|
|
end
|
|
|
|
minetest.register_entity("guns:bullet", bullet)
|