From eddd04168b1c91a0fed43de3466eeacbdcc305ea Mon Sep 17 00:00:00 2001 From: LoneWolfHT Date: Thu, 14 May 2020 10:03:06 -0700 Subject: [PATCH] grenades: Improve API (#615) --- mods/pvp/grenades/api.md | 45 ++++++++++++++++------------- mods/pvp/grenades/grenades.lua | 2 +- mods/pvp/grenades/init.lua | 52 ++++++++++++++++++++-------------- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/mods/pvp/grenades/api.md b/mods/pvp/grenades/api.md index 24a4c5f..e808d96 100644 --- a/mods/pvp/grenades/api.md +++ b/mods/pvp/grenades/api.md @@ -1,24 +1,31 @@ # 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') - description = "", -- A short description of the grenade. - image = "", -- The name of the grenade's texture - on_explode = function(pos, name) - -- This function is called when the grenade 'explodes' - -- the place the grenade 'exploded' at - -- the name of the player that threw the grenade - end, - placeable = false, -- Optional, default is false - 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 - } - }) +```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) + -- This function is called when the grenade 'explodes' + -- the place the grenade 'exploded' at + -- the name of the player that threw the grenade + end, + on_collide = function(obj, name) + -- This function is called when the grenade collides with a surface + -- the grenade object + -- 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 + 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 + } +}) +``` diff --git a/mods/pvp/grenades/grenades.lua b/mods/pvp/grenades/grenades.lua index a711ad2..06aa946 100644 --- a/mods/pvp/grenades/grenades.lua +++ b/mods/pvp/grenades/grenades.lua @@ -194,6 +194,6 @@ grenades.register_grenade("grenades:smoke", { life = 1, size = 4, glow = 0, - interval = 5, + interval = 0.3, } }) diff --git a/mods/pvp/grenades/init.lua b/mods/pvp/grenades/init.lua index 8df6e4f..a9fa222 100644 --- a/mods/pvp/grenades/init.lua +++ b/mods/pvp/grenades/init.lua @@ -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,15 +21,17 @@ function grenades.register_grenade(name, def) end 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, - 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, 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