grenades: Improve API (#615)
This commit is contained in:
parent
0c67cb5219
commit
eddd04168b
3 changed files with 58 additions and 41 deletions
|
@ -1,24 +1,31 @@
|
||||||
# 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
|
||||||
description = "", -- A short description of the grenade.
|
grenades.register_grenade("name", { -- Name of the grenade (Like 'smoke' or 'flashbang')
|
||||||
image = "", -- The name of the grenade's texture
|
description = "", -- A short description of the grenade.
|
||||||
on_explode = function(pos, name)
|
image = "", -- The name of the grenade's texture
|
||||||
-- This function is called when the grenade 'explodes'
|
on_explode = function(pos, name)
|
||||||
-- <pos> the place the grenade 'exploded' at
|
-- This function is called when the grenade 'explodes'
|
||||||
-- <name> the name of the player that threw the grenade
|
-- <pos> the place the grenade 'exploded' at
|
||||||
end,
|
-- <name> the name of the player that threw the grenade
|
||||||
placeable = false, -- Optional, default is false
|
end,
|
||||||
clock = 3, -- Optional, controls how long until grenade detonates. Default is 3
|
on_collide = function(obj, name)
|
||||||
particle = { -- Adds particles in the grenade's trail
|
-- This function is called when the grenade collides with a surface
|
||||||
image = "grenades_smoke.png", -- The particle's image
|
-- <obj> the grenade object
|
||||||
life = 1, -- How long (seconds) it takes for the particle to disappear
|
-- <name> the name of the player that threw the grenade
|
||||||
size = 4, -- Size of the particle
|
-- return true to cause grenade explosion
|
||||||
glow = 0, -- Brightens the texture in darkness
|
end,
|
||||||
interval = 5, -- How long it takes before a particle can be added
|
clock = 3, -- Optional, controls how long until grenade detonates. Default is 3
|
||||||
}
|
particle = { -- Adds particles in the grenade's trail
|
||||||
})
|
image = "grenades_smoke.png", -- The particle's image
|
||||||
|
life = 1, -- How long (seconds) it takes for the particle to disappear
|
||||||
|
size = 4, -- Size of the particle
|
||||||
|
glow = 0, -- Brightens the texture in darkness
|
||||||
|
interval = 5, -- How long it takes before a particle can be added
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -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,15 +21,17 @@ function grenades.register_grenade(name, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
local grenade_entity = {
|
local grenade_entity = {
|
||||||
physical = true,
|
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,
|
||||||
|
},
|
||||||
sliding = 1,
|
sliding = 1,
|
||||||
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,
|
|
||||||
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue