grenades: Improve API (#615)
This commit is contained in:
parent
0c67cb5219
commit
eddd04168b
3 changed files with 58 additions and 41 deletions
|
@ -1,10 +1,11 @@
|
|||
# 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
|
||||
|
||||
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.
|
||||
image = "", -- The name of the grenade's texture
|
||||
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
|
||||
-- <name> the name of the player that threw the grenade
|
||||
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
|
||||
particle = { -- Adds particles in the grenade's trail
|
||||
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
|
||||
interval = 5, -- How long it takes before a particle can be added
|
||||
}
|
||||
})
|
||||
})
|
||||
```
|
||||
|
|
|
@ -194,6 +194,6 @@ grenades.register_grenade("grenades:smoke", {
|
|||
life = 1,
|
||||
size = 4,
|
||||
glow = 0,
|
||||
interval = 5,
|
||||
interval = 0.3,
|
||||
}
|
||||
})
|
||||
|
|
|
@ -2,16 +2,17 @@ grenades = {
|
|||
grenade_deaccel = 9
|
||||
}
|
||||
|
||||
local function throw_grenade(name, player)
|
||||
function grenades.throw_grenade(name, startspeed, player)
|
||||
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)
|
||||
|
||||
local m = 20
|
||||
obj:set_velocity({x = dir.x * m, y = dir.y * m, z = dir.z * m})
|
||||
obj:set_velocity(vector.multiply(dir, startspeed))
|
||||
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
|
||||
|
||||
function grenades.register_grenade(name, def)
|
||||
|
@ -20,8 +21,8 @@ function grenades.register_grenade(name, def)
|
|||
end
|
||||
|
||||
local grenade_entity = {
|
||||
initial_properties = {
|
||||
physical = true,
|
||||
sliding = 1,
|
||||
collide_with_objects = false,
|
||||
visual = "sprite",
|
||||
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},
|
||||
pointable = false,
|
||||
static_save = false,
|
||||
},
|
||||
sliding = 1,
|
||||
particle = 0,
|
||||
timer = 0,
|
||||
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
|
||||
|
||||
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
|
||||
vel.x = self.last_vel.x * -0.3 -- Invert velocity and reduce it a bit
|
||||
end
|
||||
|
@ -87,13 +100,13 @@ function grenades.register_grenade(name, def)
|
|||
|
||||
-- Grenade Particles
|
||||
|
||||
if def.particle and self.particle >= 4 then
|
||||
if def.particle and self.particle >= def.particle.interval then
|
||||
self.particle = 0
|
||||
|
||||
minetest.add_particle({
|
||||
pos = obj:get_pos(),
|
||||
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,
|
||||
size = def.particle.size,
|
||||
collisiondetection = false,
|
||||
|
@ -103,14 +116,14 @@ function grenades.register_grenade(name, def)
|
|||
glow = def.particle.glow
|
||||
})
|
||||
elseif def.particle and self.particle < def.particle.interval then
|
||||
self.particle = self.particle + 1
|
||||
self.particle = self.particle + dtime
|
||||
end
|
||||
|
||||
-- Explode when clock is up
|
||||
|
||||
if self.timer > def.clock or not 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)))
|
||||
def.on_explode(pos, self.thrower_name)
|
||||
end
|
||||
|
@ -129,14 +142,11 @@ function grenades.register_grenade(name, def)
|
|||
newdef.range = 0
|
||||
newdef.inventory_image = def.image
|
||||
newdef.on_use = function(itemstack, user, pointed_thing)
|
||||
local player_name = user:get_player_name()
|
||||
|
||||
if pointed_thing.type ~= "node" then
|
||||
local grenade = throw_grenade(name, user)
|
||||
grenade.thrower_name = player_name
|
||||
grenades.throw_grenade(name, 20, user)
|
||||
|
||||
if not minetest.settings:get_bool("creative_mode") then
|
||||
itemstack = ""
|
||||
itemstack:take_item(1)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue