throwing/functions.lua

61 lines
2.0 KiB
Lua
Raw Normal View History

--~
--~ Shot and reload system
--~
function throwing_shoot_arrow (itemstack, player, stiffness, is_cross)
local arrow = itemstack:get_metadata()
itemstack:set_metadata("")
player:set_wielded_item(itemstack)
local playerpos = player:getpos()
local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, arrow)
local dir = player:get_look_dir()
obj:setvelocity({x=dir.x*stiffness, y=dir.y*stiffness, z=dir.z*stiffness})
obj:setacceleration({x=dir.x*-3, y=-10, z=dir.z*-3})
obj:setyaw(player:get_look_yaw()+math.pi)
if is_cross then
minetest.sound_play("throwing_crossbow_sound", {pos=playerpos})
else
minetest.sound_play("throwing_bow_sound", {pos=playerpos})
end
if obj:get_luaentity().player == "" then
obj:get_luaentity().player = player
end
obj:get_luaentity().stack = player:get_inventory():get_stack("main", player:get_wield_index()+2)
obj:get_luaentity().inventory = player:get_inventory()
return true
end
function throwing_unload (itemstack, player, unloaded, wear)
if itemstack:get_metadata() then
for _,arrow in ipairs(throwing_arrows) do
if itemstack:get_metadata() == arrow[2] then
if not minetest.setting_getbool("creative_mode") then
player:get_inventory():add_item("main", arrow[1])
end
end
end
end
if wear >= 65535 then
player:set_wielded_item({})
else
player:set_wielded_item({name=unloaded, wear=wear})
end
end
function throwing_reload (itemstack, player, pos, is_cross, loaded)
if itemstack:get_name() == player:get_wielded_item():get_name() then
if (pos.x == player:getpos().x and pos.y == player:getpos().y and pos.z == player:getpos().z) or not is_cross then
local wear = itemstack:get_wear()
for _,arrow in ipairs(throwing_arrows) do
if player:get_inventory():get_stack("main", player:get_wield_index()+1):get_name() == arrow[1] then
if not minetest.setting_getbool("creative_mode") then
player:get_inventory():remove_item("main", arrow[1])
end
meta = arrow[2]
player:set_wielded_item({name=loaded, wear=wear, metadata=meta})
end
end
end
end
end