capturetheflag/mods/pvp/grenades/init.lua

157 lines
4.2 KiB
Lua
Raw Permalink Normal View History

2020-05-05 17:30:47 +00:00
grenades = {
2020-12-02 18:58:11 +00:00
grenade_deaccel = 8
2020-05-05 17:30:47 +00:00
}
2020-05-14 17:03:06 +00:00
function grenades.throw_grenade(name, startspeed, player)
2020-05-05 17:30:47 +00:00
local dir = player:get_look_dir()
local pos = player:get_pos()
local obj = minetest.add_entity({x = pos.x + dir.x, y = pos.y + 1.5 + dir.y, z = pos.z + dir.z}, name)
2020-05-14 17:03:06 +00:00
obj:set_velocity(vector.multiply(dir, startspeed))
2020-05-05 17:30:47 +00:00
obj:set_acceleration({x = 0, y = -9.8, z = 0})
2020-05-14 17:03:06 +00:00
obj:get_luaentity().thrower_name = player:get_player_name()
return obj:get_luaentity()
2020-05-05 17:30:47 +00:00
end
function grenades.register_grenade(name, def)
if not def.clock then
def.clock = 4
end
local grenade_entity = {
2020-05-14 17:03:06 +00:00
initial_properties = {
physical = true,
collide_with_objects = false,
visual = "sprite",
visual_size = {x = 0.5, y = 0.5, z = 0.5},
textures = {def.image},
collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.15, 0.2},
pointable = false,
static_save = false,
},
2020-05-05 17:30:47 +00:00
sliding = 1,
particle = 0,
timer = 0,
on_step = function(self, dtime, moveresult)
2020-05-05 17:30:47 +00:00
local obj = self.object
local vel = obj:get_velocity()
local pos = obj:get_pos()
local norm_vel -- Normalized velocity
self.timer = self.timer + dtime
if not self.last_vel then
self.last_vel = vel
end
-- Check for a collision on the x/y/z axis
if moveresult.collides and moveresult.collisions then
if def.on_collide then
local c_result = def.on_collide(obj, self.thrower_name)
if c_result == true then
if self.thrower_name then
minetest.log("action", "[Grenades] A grenade thrown by " .. self.thrower_name ..
" explodes at " .. minetest.pos_to_string(vector.round(pos)))
def.on_explode(pos, self.thrower_name)
end
obj:remove()
elseif c_result == "stop" then
vel = vector.new()
self.last_vel = vector.new()
obj:set_velocity(vector.new())
obj:set_acceleration(vector.new(0, 0, 0))
2020-05-14 17:03:06 +00:00
end
2020-05-05 17:30:47 +00:00
end
if moveresult.collisions[1] and moveresult.collisions[1].axis then
local axis = moveresult.collisions[1].axis
2020-05-05 17:30:47 +00:00
vel[axis] = self.last_vel[axis] * -0.3
2020-05-05 17:30:47 +00:00
end
obj:set_velocity(vel)
end
self.last_vel = vel
norm_vel = vector.normalize(vel)
2020-05-05 17:30:47 +00:00
if not vector.equals(vel, vector.new()) then
2020-05-05 17:30:47 +00:00
obj:set_acceleration({
x = -norm_vel.x * grenades.grenade_deaccel * (moveresult.touching_ground and 2 or 1),
2020-05-05 17:30:47 +00:00
y = -9.8,
z = -norm_vel.z * grenades.grenade_deaccel * (moveresult.touching_ground and 2 or 1),
2020-05-05 17:30:47 +00:00
})
end
if moveresult.touching_ground then -- Is the grenade sliding?
if vector.distance(vector.new(), vel) <= 2 and not vector.equals(vel, vector.new()) then -- Grenade is barely moving, make sure it stays that way
obj:set_velocity(vector.new())
obj:set_acceleration(vector.new(0, -9.8, 0))
end
end
2020-05-05 17:30:47 +00:00
-- Grenade Particles
2020-05-14 17:03:06 +00:00
if def.particle and self.particle >= def.particle.interval then
2020-05-05 17:30:47 +00:00
self.particle = 0
minetest.add_particle({
pos = obj:get_pos(),
velocity = vector.divide(vel, 2),
2020-05-14 17:03:06 +00:00
acceleration = vector.divide(obj:get_acceleration() or vector.new(1, 1, 1), -5),
2020-05-05 17:30:47 +00:00
expirationtime = def.particle.life,
size = def.particle.size,
collisiondetection = false,
collision_removal = false,
vertical = false,
texture = def.particle.image,
glow = def.particle.glow
})
elseif def.particle and self.particle < def.particle.interval then
2020-05-14 17:03:06 +00:00
self.particle = self.particle + dtime
2020-05-05 17:30:47 +00:00
end
-- Explode when clock is up
if self.timer > def.clock or not self.thrower_name then
if self.thrower_name then
2020-05-14 17:03:06 +00:00
minetest.log("action", "[Grenades] A grenade thrown by " .. self.thrower_name ..
2020-05-05 17:30:47 +00:00
" explodes at " .. minetest.pos_to_string(vector.round(pos)))
def.on_explode(pos, self.thrower_name)
end
obj:remove()
end
end
}
minetest.register_entity(name, grenade_entity)
local newdef = {}
newdef.description = def.description
newdef.stack_max = def.stack_max or 1
2020-05-05 17:30:47 +00:00
newdef.range = 0
newdef.inventory_image = def.image
newdef.on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
2020-12-02 18:58:11 +00:00
grenades.throw_grenade(name, 17, user)
2020-05-05 17:30:47 +00:00
if not minetest.settings:get_bool("creative_mode") then
2020-05-14 17:03:06 +00:00
itemstack:take_item(1)
2020-05-05 17:30:47 +00:00
end
end
return itemstack
end
minetest.register_craftitem(name, newdef)
end
dofile(minetest.get_modpath("grenades") .. "/grenades.lua")