From 094613eb2979775ee77a51ed008cf2eb43bb3de0 Mon Sep 17 00:00:00 2001 From: upsilon Date: Wed, 21 Jun 2017 17:29:20 +0200 Subject: [PATCH] Add a sound a delay to the bow --- README.md | 2 ++ init.lua | 37 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e7df14b..b50e71b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Definition: definition table, containing: Default is false. * cooldown: bow cooldown. Default is setting throwing.bow_cooldown * function spawn_arrow_entity(position, arrow, player): defaults to throwing.spawn_arrow_entity + * sound: sound to be played when the bow is used + * delay: delay before throwing the arrow ]] -- Example: diff --git a/init.lua b/init.lua index 87173cc..cbd0823 100644 --- a/init.lua +++ b/init.lua @@ -340,28 +340,35 @@ function throwing.register_bow(name, def) local index = (def.throw_itself and user:get_wield_index()) or user:get_wield_index()+1 local res, new_stack = def.allow_shot(user, user:get_inventory():get_stack("main", index), index) - -- Throw itself? if not res then return new_stack or itemstack end - -- Shoot arrow - if shoot_arrow(itemstack, user, def.throw_itself, new_stack) then - if not minetest.setting_getbool("creative_mode") then - itemstack:add_wear(65535 / (def.uses or 50)) + -- Sound + if def.sound then + minetest.sound_play(def.sound, {to_player=user:get_player_name()}) + end + + minetest.after(def.delay or 0, function() + -- Shoot arrow + if shoot_arrow(itemstack, user, def.throw_itself, new_stack) then + if not minetest.setting_getbool("creative_mode") then + itemstack:add_wear(65535 / (def.uses or 50)) + end 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 + 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 + user:get_inventory():set_stack("main", user:get_wield_index(), itemstack) + end) return itemstack end minetest.register_tool(name, def)