grenades/init.lua

166 lines
4.4 KiB
Lua
Raw Permalink Normal View History

2019-12-30 19:07:23 +01:00
grenades = {
grenade_accel = 13
}
2018-11-22 20:58:20 +01:00
local function throw_grenade(name, player)
2019-01-13 22:09:39 +01:00
local dir = player:get_look_dir()
local pos = player:get_pos()
2019-12-30 19:07:23 +01:00
local obj = minetest.add_entity({x = pos.x + dir.x, y = pos.y + 1.6, z = pos.z + dir.z}, name)
2018-11-22 20:58:20 +01:00
2019-12-30 19:07:23 +01:00
local m = 30
obj:set_velocity({x = dir.x * m, y = dir.y * m/1.5, z = dir.z * m})
obj:set_acceleration({x = 0, y = -13, z = 0})
2018-11-22 20:58:20 +01:00
2019-01-13 22:09:39 +01:00
return(obj:get_luaentity())
2018-11-22 20:58:20 +01:00
end
function grenades.register_grenade(name, def)
2019-04-03 20:58:50 +02:00
if not def.clock then
2019-12-30 19:07:23 +01:00
def.clock = 4
2019-01-13 22:09:39 +01:00
end
local grenade_entity = {
2019-04-03 20:58:50 +02:00
physical = true,
2019-12-30 19:07:23 +01:00
sliding = 1,
2019-01-13 22:09:39 +01:00
collide_with_objects = true,
visual = "sprite",
2019-04-03 20:58:50 +02:00
visual_size = {x = 0.5, y = 0.5, z = 0.5},
2019-01-13 22:09:39 +01:00
textures = {def.image},
2019-12-30 19:07:23 +01:00
collisionbox = {-0.2, -0.3, -0.2, 0.2, 0.15, 0.2},
2019-04-03 20:58:50 +02:00
pointable = false,
static_save = false,
particle = 0,
2019-12-30 19:07:23 +01:00
timer = 0,
2019-01-13 22:09:39 +01:00
on_step = function(self, dtime)
local obj = self.object
2019-04-03 20:58:50 +02:00
local vel = obj:get_velocity()
2019-01-13 22:09:39 +01:00
local pos = obj:get_pos()
2019-12-30 19:07:23 +01:00
local norm_vel -- Normalized velocity
2019-01-13 22:09:39 +01:00
2019-04-03 20:58:50 +02:00
self.timer = self.timer + dtime
if not self.last_vel then
self.last_vel = vel
2019-01-13 22:09:39 +01:00
end
2019-12-30 19:07:23 +01:00
-- Check for a collision on the x/y/z axis
2019-04-03 20:58:50 +02:00
2019-12-30 19:07:23 +01:00
if not vector.equals(self.last_vel, vel) and vector.distance(self.last_vel, vel) > 4 then
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
2019-04-03 20:58:50 +02:00
end
2019-12-30 19:07:23 +01:00
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
2019-04-03 20:58:50 +02:00
end
2019-12-30 19:07:23 +01:00
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
2019-04-03 20:58:50 +02:00
end
2019-12-30 19:07:23 +01:00
obj:set_velocity(vel)
2019-01-13 22:09:39 +01:00
end
2019-12-30 19:07:23 +01:00
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
2019-04-03 20:58:50 +02:00
2019-12-30 19:07:23 +01:00
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
obj:set_velocity(vector.new(0, -9.8, 0)) -- Make sure it stays unmoving
obj:set_acceleration(vector.new())
end
else
norm_vel = vector.normalize(vel)
2019-04-03 20:58:50 +02:00
2019-12-30 19:07:23 +01:00
obj:set_acceleration({
x = -norm_vel.x * grenades.grenade_accel * self.sliding,
y = -9.8,
z = -norm_vel.z * grenades.grenade_accel * self.sliding,
})
end
2019-04-03 20:58:50 +02:00
-- Grenade Particles
2019-01-13 22:09:39 +01:00
if def.particle and self.particle >= 4 then
self.particle = 0
minetest.add_particle({
pos = obj:get_pos(),
2019-04-03 20:58:50 +02:00
velocity = vector.divide(vel, 2),
2019-01-13 22:09:39 +01:00
acceleration = vector.divide(obj:get_acceleration(), -5),
expirationtime = def.particle.life,
size = def.particle.size,
2019-04-03 20:58:50 +02:00
collisiondetection = false,
collision_removal = false,
2019-01-13 22:09:39 +01:00
vertical = false,
texture = def.particle.image,
glow = def.particle.glow
})
elseif def.particle and self.particle < def.particle.interval then
self.particle = self.particle + 1
end
2019-04-03 20:58:50 +02:00
-- Explode when clock is up
if self.timer > def.clock or not self.thrower_name then
if self.thrower_name then
2019-12-30 19:07:23 +01:00
minetest.log("[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
2019-01-13 22:09:39 +01:00
obj:remove()
end
end
}
2019-04-03 20:58:50 +02:00
minetest.register_entity(name, grenade_entity)
2019-01-13 22:09:39 +01:00
local newdef = {}
newdef.description = def.description
newdef.stack_max = 1
2019-12-30 19:07:23 +01:00
newdef.range = 0
2019-01-13 22:09:39 +01:00
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
2019-04-03 20:58:50 +02:00
local grenade = throw_grenade(name, user)
2019-01-13 22:09:39 +01:00
grenade.thrower_name = player_name
if not minetest.settings:get_bool("creative_mode") then
itemstack = ""
end
end
return itemstack
end
if def.placeable == true then
newdef.tiles = {def.image}
newdef.selection_box = {
type = "fixed",
fixed = {-0.3, -0.5, -0.3, 0.3, 0.4, 0.3},
}
newdef.groups = {oddly_breakable_by_hand = 2}
newdef.paramtype = "light"
newdef.sunlight_propagates = true
newdef.walkable = false
newdef.drawtype = "plantlike"
2019-04-03 20:58:50 +02:00
minetest.register_node(name, newdef)
2019-01-13 22:09:39 +01:00
else
2019-04-03 20:58:50 +02:00
minetest.register_craftitem(name, newdef)
2019-01-13 22:09:39 +01:00
end
2018-11-22 20:58:20 +01:00
2019-12-30 19:07:23 +01:00
minetest.register_craftitem(name, newdef)
end