Add Sticky grenades (#677)

* Improved grenade API
* Added sticky grenade
* Improved grenade textures
This commit is contained in:
LoneWolfHT 2020-09-12 20:10:40 -07:00 committed by GitHub
parent e4a64124cf
commit 81d7553f7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 35 deletions

View file

@ -279,6 +279,14 @@ crafting.register_recipe({
level = 1,
})
crafting.register_recipe({
type = "inv",
output = "grenades:frag_sticky 1",
items = { "grenades:frag", "default:stick 4" },
always_known = true,
level = 1,
})
crafting.register_recipe({
type = "inv",
output = "grenades:smoke 1",

View file

@ -18,6 +18,7 @@ grenades.register_grenade("name", { -- Name of the grenade (Like 'smoke' or 'fla
-- <obj> the grenade object
-- <name> the name of the player that threw the grenade
-- return true to cause grenade explosion
-- return "stop" to stop the grenade from moving
end,
clock = 3, -- Optional, controls how long until grenade detonates. Default is 3
particle = { -- Adds particles in the grenade's trail

View file

@ -9,7 +9,7 @@ local function remove_flora(pos, radius)
end
end
grenades.register_grenade("grenades:frag", {
local fragdef = {
description = "Frag grenade (Kills anyone near blast)",
image = "grenades_frag.png",
on_explode = function(pos, name)
@ -71,7 +71,16 @@ grenades.register_grenade("grenades:frag", {
end
end
end,
})
}
grenades.register_grenade("grenades:frag", table.copy(fragdef))
fragdef.description = "Sticky Frag grenade (Sticks to surfaces)"
fragdef.image = "grenades_frag_sticky.png"
fragdef.on_collide = function(obj)
return "stop"
end
grenades.register_grenade("grenades:frag_sticky", fragdef)
-- Flashbang Grenade

View file

@ -34,7 +34,7 @@ function grenades.register_grenade(name, def)
sliding = 1,
particle = 0,
timer = 0,
on_step = function(self, dtime)
on_step = function(self, dtime, moveresult)
local obj = self.object
local vel = obj:get_velocity()
local pos = obj:get_pos()
@ -48,27 +48,29 @@ function grenades.register_grenade(name, def)
-- Check for a collision on the x/y/z axis
if not vector.equals(self.last_vel, vel) and vector.distance(self.last_vel, vel) > 4 then
if def.on_collide and def.on_collide(obj, self.thrower_name) 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)
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))
end
obj:remove()
end
if math.abs(self.last_vel.x - vel.x) > 5 then -- Check for a large reduction in velocity
vel.x = self.last_vel.x * -0.3 -- Invert velocity and reduce it a bit
end
if moveresult.collisions[1] and moveresult.collisions[1].axis then
local axis = moveresult.collisions[1].axis
if math.abs(self.last_vel.y - vel.y) > 5 then -- Check for a large reduction in velocity
vel.y = self.last_vel.y * -0.2 -- Invert velocity and reduce it a bit
end
if math.abs(self.last_vel.z - vel.z) > 5 then -- Check for a large reduction in velocity
vel.z = self.last_vel.z * -0.3 -- Invert velocity and reduce it a bit
vel[axis] = self.last_vel[axis] * -0.3
end
obj:set_velocity(vel)
@ -76,27 +78,22 @@ function grenades.register_grenade(name, def)
self.last_vel = vel
if self.sliding == 1 and vel.y == 0 then -- Check if grenade is sliding
self.sliding = 2 -- Multiplies drag by 2
elseif self.sliding > 1 and vel.y ~= 0 then
self.sliding = 1 -- Doesn't affect drag
end
if self.sliding > 1 then -- Is the grenade sliding?
if vector.distance(vector.new(), vel) <= 1 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
else
norm_vel = vector.normalize(vel)
norm_vel = vector.normalize(vel)
if not vector.equals(vel, vector.new()) then
obj:set_acceleration({
x = -norm_vel.x * grenades.grenade_deaccel * self.sliding,
x = -norm_vel.x * grenades.grenade_deaccel * (moveresult.touching_ground and 2 or 1),
y = -9.8,
z = -norm_vel.z * grenades.grenade_deaccel * self.sliding,
z = -norm_vel.z * grenades.grenade_deaccel * (moveresult.touching_ground and 2 or 1),
})
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
-- Grenade Particles

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 B

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB