add cooldown and take snowballs on use

This commit is contained in:
whosit 2024-11-23 15:09:17 +03:00
parent 9ea770574a
commit b8ce1d1596

View File

@ -4,6 +4,21 @@ local v_multiply = vector.multiply
local v_dot = vector.dot
local COOLDOWN = 0.2 -- TODO make configurable
local player_last_throw_time = {}
core.register_on_leaveplayer(
function(player, _timed_out)
local player_name = player:get_player_name()
if player_name ~= "" then
player_last_throw_time[player_name] = nil
end
end
)
local function spawn_snow_puff(pos, normal)
local shift = normal * 0.1
local center = pos + shift
@ -15,7 +30,7 @@ local function spawn_snow_puff(pos, normal)
drag = 0.1,
attract = {
kind = "point",
strength = -10,
strength = -14,
origin = center - shift,
},
acc = v_new(0, -8, 0),
@ -24,6 +39,7 @@ local function spawn_snow_puff(pos, normal)
collisiondetection = true,
collision_removal = true,
texpool = { "yl_snowball_snowflake.png" },
--glow = 2,
}
core.add_particlespawner(def)
end
@ -63,6 +79,15 @@ yl_snowball.on_use = function(itemstack, user, pointed_thing)
return itemstack
end
local player = user
local player_name = player:get_player_name()
local current_time = os.clock() --core.get_gametime()
if current_time - (player_last_throw_time[player_name] or 0) < COOLDOWN then
return nil
end
player_last_throw_time[player_name] = current_time
local speed = 15
local start, look, p_vel = get_start_params(player)
-- projectile velocity points where we look + affected by player movement
@ -76,6 +101,10 @@ yl_snowball.on_use = function(itemstack, user, pointed_thing)
player,
nil)
if not core.check_player_privs(player, "creative") then
itemstack:take_item(1)
end
return itemstack
end
@ -89,7 +118,7 @@ core.register_craftitem(
stack_max= 99,
inventory_image = "yl_snowball.png",
on_use = function(...) -- FIXME remove wrapper
yl_snowball.on_use(...)
return yl_snowball.on_use(...)
end,
}
)