grenades: Improve API (#615)

This commit is contained in:
LoneWolfHT 2020-05-14 10:03:06 -07:00 committed by GitHub
parent 0c67cb5219
commit eddd04168b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 41 deletions

View file

@ -1,10 +1,11 @@
# Grenades API # Grenades API
Still WIP. Please suggest new features here: https://forum.minetest.net/viewtopic.php?f=9&t=21466 Please suggest new features here: https://forum.minetest.net/viewtopic.php?f=9&t=21466
## API ## API
grenades.register_grenade("name", { -- Name of the grenade (Like 'smoke' or 'flashbang') ```lua
grenades.register_grenade("name", { -- Name of the grenade (Like 'smoke' or 'flashbang')
description = "", -- A short description of the grenade. description = "", -- A short description of the grenade.
image = "", -- The name of the grenade's texture image = "", -- The name of the grenade's texture
on_explode = function(pos, name) on_explode = function(pos, name)
@ -12,7 +13,12 @@ Still WIP. Please suggest new features here: https://forum.minetest.net/viewtopi
-- <pos> the place the grenade 'exploded' at -- <pos> the place the grenade 'exploded' at
-- <name> the name of the player that threw the grenade -- <name> the name of the player that threw the grenade
end, end,
placeable = false, -- Optional, default is false on_collide = function(obj, name)
-- This function is called when the grenade collides with a surface
-- <obj> the grenade object
-- <name> the name of the player that threw the grenade
-- return true to cause grenade explosion
end,
clock = 3, -- Optional, controls how long until grenade detonates. Default is 3 clock = 3, -- Optional, controls how long until grenade detonates. Default is 3
particle = { -- Adds particles in the grenade's trail particle = { -- Adds particles in the grenade's trail
image = "grenades_smoke.png", -- The particle's image image = "grenades_smoke.png", -- The particle's image
@ -21,4 +27,5 @@ Still WIP. Please suggest new features here: https://forum.minetest.net/viewtopi
glow = 0, -- Brightens the texture in darkness glow = 0, -- Brightens the texture in darkness
interval = 5, -- How long it takes before a particle can be added interval = 5, -- How long it takes before a particle can be added
} }
}) })
```

View file

@ -194,6 +194,6 @@ grenades.register_grenade("grenades:smoke", {
life = 1, life = 1,
size = 4, size = 4,
glow = 0, glow = 0,
interval = 5, interval = 0.3,
} }
}) })

View file

@ -2,16 +2,17 @@ grenades = {
grenade_deaccel = 9 grenade_deaccel = 9
} }
local function throw_grenade(name, player) function grenades.throw_grenade(name, startspeed, player)
local dir = player:get_look_dir() local dir = player:get_look_dir()
local pos = player:get_pos() 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) local obj = minetest.add_entity({x = pos.x + dir.x, y = pos.y + 1.5 + dir.y, z = pos.z + dir.z}, name)
local m = 20 obj:set_velocity(vector.multiply(dir, startspeed))
obj:set_velocity({x = dir.x * m, y = dir.y * m, z = dir.z * m})
obj:set_acceleration({x = 0, y = -9.8, z = 0}) obj:set_acceleration({x = 0, y = -9.8, z = 0})
return(obj:get_luaentity()) obj:get_luaentity().thrower_name = player:get_player_name()
return obj:get_luaentity()
end end
function grenades.register_grenade(name, def) function grenades.register_grenade(name, def)
@ -20,8 +21,8 @@ function grenades.register_grenade(name, def)
end end
local grenade_entity = { local grenade_entity = {
initial_properties = {
physical = true, physical = true,
sliding = 1,
collide_with_objects = false, collide_with_objects = false,
visual = "sprite", visual = "sprite",
visual_size = {x = 0.5, y = 0.5, z = 0.5}, visual_size = {x = 0.5, y = 0.5, z = 0.5},
@ -29,6 +30,8 @@ function grenades.register_grenade(name, def)
collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.15, 0.2}, collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.15, 0.2},
pointable = false, pointable = false,
static_save = false, static_save = false,
},
sliding = 1,
particle = 0, particle = 0,
timer = 0, timer = 0,
on_step = function(self, dtime) on_step = function(self, dtime)
@ -46,6 +49,16 @@ function grenades.register_grenade(name, def)
-- Check for a collision on the x/y/z axis -- 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 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)
end
obj:remove()
end
if math.abs(self.last_vel.x - vel.x) > 5 then -- Check for a large reduction in velocity 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 vel.x = self.last_vel.x * -0.3 -- Invert velocity and reduce it a bit
end end
@ -87,13 +100,13 @@ function grenades.register_grenade(name, def)
-- Grenade Particles -- Grenade Particles
if def.particle and self.particle >= 4 then if def.particle and self.particle >= def.particle.interval then
self.particle = 0 self.particle = 0
minetest.add_particle({ minetest.add_particle({
pos = obj:get_pos(), pos = obj:get_pos(),
velocity = vector.divide(vel, 2), velocity = vector.divide(vel, 2),
acceleration = vector.divide(obj:get_acceleration(), -5), acceleration = vector.divide(obj:get_acceleration() or vector.new(1, 1, 1), -5),
expirationtime = def.particle.life, expirationtime = def.particle.life,
size = def.particle.size, size = def.particle.size,
collisiondetection = false, collisiondetection = false,
@ -103,14 +116,14 @@ function grenades.register_grenade(name, def)
glow = def.particle.glow glow = def.particle.glow
}) })
elseif def.particle and self.particle < def.particle.interval then elseif def.particle and self.particle < def.particle.interval then
self.particle = self.particle + 1 self.particle = self.particle + dtime
end end
-- Explode when clock is up -- Explode when clock is up
if self.timer > def.clock or not self.thrower_name then if self.timer > def.clock or not self.thrower_name then
if self.thrower_name then if self.thrower_name then
minetest.log("[Grenades] A grenade thrown by " .. self.thrower_name .. minetest.log("action", "[Grenades] A grenade thrown by " .. self.thrower_name ..
" explodes at " .. minetest.pos_to_string(vector.round(pos))) " explodes at " .. minetest.pos_to_string(vector.round(pos)))
def.on_explode(pos, self.thrower_name) def.on_explode(pos, self.thrower_name)
end end
@ -129,14 +142,11 @@ function grenades.register_grenade(name, def)
newdef.range = 0 newdef.range = 0
newdef.inventory_image = def.image newdef.inventory_image = def.image
newdef.on_use = function(itemstack, user, pointed_thing) newdef.on_use = function(itemstack, user, pointed_thing)
local player_name = user:get_player_name()
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
local grenade = throw_grenade(name, user) grenades.throw_grenade(name, 20, user)
grenade.thrower_name = player_name
if not minetest.settings:get_bool("creative_mode") then if not minetest.settings:get_bool("creative_mode") then
itemstack = "" itemstack:take_item(1)
end end
end end