Add a cooldown

This commit is contained in:
upsilon 2017-06-20 20:19:27 +02:00
parent 5f94a6b27d
commit 1be6638b56
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C
2 changed files with 23 additions and 2 deletions

View File

@ -22,6 +22,8 @@ throwing.vertical_acceleration = -10
throwing.allow_arrow_placing = false
throwing.arrow_teleport_in_protected = true
throwing.bow_cooldown = 0.2
```
## API
@ -45,6 +47,7 @@ Definition: definition table, containing:
* throw_itself (optional): whether the bow should throw itself instead of the arrow next to it in the inventory.
If present, allow_shot is ignored.
Default is false.
* cooldown: bow cooldown. Default is setting throwing.bow_cooldown
]]
-- Example:

View File

@ -292,6 +292,8 @@ end
---------- Bows -----------
local bow_cooldown = {}
function throwing.register_bow(name, def)
if not string.find(name, ":") then
name = throwing.modname..":"..name
@ -302,23 +304,39 @@ function throwing.register_bow(name, def)
return throwing.is_arrow(itemstack)
end
end
def.inventory_image = def.texture
def.textre = nil
if not def.inventory_image then
def.inventory_image = def.texture
end
def.on_use = function(itemstack, user, pointed_thing)
-- Cooldown
local meta = itemstack:get_meta()
local cooldown = def.cooldown or tonumber(minetest.settings:get("throwing.bow_cooldown")) or 0.2
if cooldown > 0 and meta:get_int("cooldown") > os.time() then
return
end
-- Throw itself?
if not def.throw_itself and not def.allow_shot(user, user:get_inventory():get_stack("main", user:get_wield_index()+1)) then
return itemstack
end
-- Shoot arrow
if shoot_arrow(itemstack, user, def.throw_itself) then
if not minetest.setting_getbool("creative_mode") then
itemstack:add_wear(65535 / (def.uses or 50))
end
end
if def.throw_itself then
-- This is a bug. If we return ItemStack(nil), the player punches the entity,
-- and if the entity if a __builtin:item, it gets back to his inventory.
minetest.after(0.1, function()
user:get_inventory():remove_item("main", itemstack)
end)
elseif cooldown > 0 then
meta:set_int("cooldown", os.time() + cooldown)
end
return itemstack
end