forked from mtcontrib/throwing
268 lines
7.2 KiB
Lua
268 lines
7.2 KiB
Lua
--~
|
|
--~ Shot and reload system
|
|
--~
|
|
|
|
local stiffness= 0
|
|
local reload = 0
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
local playername = player:get_player_name()
|
|
local privs = minetest.get_player_privs(playername)
|
|
privs.throw = true
|
|
minetest.set_player_privs(playername, privs)
|
|
end)
|
|
|
|
local throwing_shoot_arrow = function(itemstack, player)
|
|
for _,arrow in ipairs(arrows) do
|
|
if player:get_inventory():get_stack("main", player:get_wield_index()+1):get_name() == arrow[1] then
|
|
local playername = player:get_player_name()
|
|
if minetest.get_player_privs(playername).throw then
|
|
local privs = minetest.get_player_privs(playername)
|
|
privs.throw = nil
|
|
minetest.set_player_privs(playername, privs)
|
|
local privs = minetest.get_player_privs(playername)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
player:get_inventory():remove_item("main", arrow[1])
|
|
end
|
|
local playerpos = player:getpos()
|
|
local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, arrow[2])
|
|
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)
|
|
minetest.sound_play("throwing_bow_sound", {pos=playerpos})
|
|
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
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function reloaded (player, id)
|
|
local playername = player:get_player_name()
|
|
local privs = minetest.get_player_privs(playername)
|
|
privs.throw = true
|
|
minetest.set_player_privs(playername, privs)
|
|
player:hud_change(id, "number", 0x00FF00)
|
|
player:hud_change(id, "text", "Ready!")
|
|
player:hud_change(id, "position", {x=0.6,y=0.515})
|
|
minetest.after(0.5, function(player, id)
|
|
player:hud_remove(id)
|
|
end, player, id)
|
|
end
|
|
|
|
local function reloading (player)
|
|
minetest.register_on_dieplayer(function(player)
|
|
local playername = player:get_player_name()
|
|
local privs = minetest.get_player_privs(playername)
|
|
privs.throw = true
|
|
minetest.set_player_privs(playername, privs)
|
|
end)
|
|
local throwing_hud = player:hud_add({
|
|
hud_elem_type = "text",
|
|
position = {x=0.6,y=0.5},
|
|
text = "Reloading...",
|
|
number = 0xFF0000,
|
|
})
|
|
minetest.after(reload, reloaded, player, throwing_hud)
|
|
end
|
|
|
|
--~
|
|
--~ Bows
|
|
--~
|
|
|
|
if not DISABLE_WOODEN_BOW then
|
|
minetest.register_tool("throwing:bow_wood", {
|
|
description = "Wooden Bow",
|
|
inventory_image = "throwing_bow_wood.png",
|
|
wield_scale = {x=1, y=1, z=0.5},
|
|
stack_max = 1,
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
stiffness = 11
|
|
reload = 1.2
|
|
if throwing_shoot_arrow(itemstack, user, pointed_thing) then
|
|
reloading(user)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
itemstack:add_wear(65535/50)
|
|
end
|
|
end
|
|
return itemstack
|
|
end,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_wood',
|
|
recipe = {
|
|
{'', 'default:stick', ''},
|
|
{'farming:string', '', 'default:stick'},
|
|
{'', 'default:stick', ''},
|
|
}
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_wood',
|
|
recipe = {
|
|
{'', 'default:stick', ''},
|
|
{'default:stick', '', 'farming:string'},
|
|
{'', 'default:stick', ''},
|
|
}
|
|
})
|
|
end
|
|
|
|
if not DISABLE_LONGBOW then
|
|
minetest.register_tool("throwing:longbow", {
|
|
description = "Longbow",
|
|
inventory_image = "throwing_longbow.png",
|
|
wield_scale = {x=1, y=2.5, z=0.5},
|
|
stack_max = 1,
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
stiffness = 18
|
|
reload = 2.5
|
|
if throwing_shoot_arrow(itemstack, user, pointed_thing) then
|
|
reloading(user)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
itemstack:add_wear(65535/100)
|
|
end
|
|
end
|
|
return itemstack
|
|
end,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:longbow',
|
|
recipe = {
|
|
{'farming:string', 'group:wood', ''},
|
|
{'farming:string', '', 'group:wood'},
|
|
{'farming:string', 'group:wood', ''},
|
|
}
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:longbow',
|
|
recipe = {
|
|
{'', 'group:wood', 'farming:string'},
|
|
{'group:wood', '', 'farming:string'},
|
|
{'', 'group:wood', 'farming:string'},
|
|
}
|
|
})
|
|
end
|
|
|
|
if not DISABLE_COMPOSITE_BOW then
|
|
minetest.register_tool("throwing:bow_composite", {
|
|
description = "Composite Bow",
|
|
inventory_image = "throwing_bow_composite.png",
|
|
wield_scale = {x=1, y=1.4, z=0.5},
|
|
stack_max = 1,
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
stiffness = 18
|
|
reload = 1.5
|
|
if throwing_shoot_arrow(itemstack, user, pointed_thing) then
|
|
reloading(user)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
itemstack:add_wear(65535/150)
|
|
end
|
|
end
|
|
return itemstack
|
|
end,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_composite',
|
|
recipe = {
|
|
{'farming:string', 'group:wood', ''},
|
|
{'farming:string', '', 'default:steel_ingot'},
|
|
{'farming:string', 'group:wood', ''},
|
|
}
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_composite',
|
|
recipe = {
|
|
{'', 'group:wood', 'farming:string'},
|
|
{'default:steel_ingot', '', 'farming:string'},
|
|
{'', 'group:wood', 'farming:string'},
|
|
}
|
|
})
|
|
end
|
|
|
|
if not DISABLE_STEEL_BOW then
|
|
minetest.register_tool("throwing:bow_steel", {
|
|
description = "Steel Bow",
|
|
inventory_image = "throwing_bow_steel.png",
|
|
wield_scale = {x=1, y=1.4, z=0.5},
|
|
stack_max = 1,
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
stiffness = 20
|
|
reload = 1.7
|
|
if throwing_shoot_arrow(itemstack, user, pointed_thing) then
|
|
reloading(user)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
itemstack:add_wear(65535/250)
|
|
end
|
|
end
|
|
return itemstack
|
|
end,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_steel',
|
|
recipe = {
|
|
{'farming:string', 'default:steel_ingot', ''},
|
|
{'farming:string', '', 'default:steel_ingot'},
|
|
{'farming:string', 'default:steel_ingot', ''},
|
|
}
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_steel',
|
|
recipe = {
|
|
{'', 'default:steel_ingot', 'farming:string'},
|
|
{'default:steel_ingot', '', 'farming:string'},
|
|
{'', 'default:steel_ingot', 'farming:string'},
|
|
}
|
|
})
|
|
end
|
|
|
|
if not DISABLE_ROYAL_BOW then
|
|
minetest.register_tool("throwing:bow_royal", {
|
|
description = "Royal Bow",
|
|
inventory_image = "throwing_bow_royal.png",
|
|
wield_scale = {x=1, y=1.4, z=0.5},
|
|
stack_max = 1,
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
stiffness = 16
|
|
reload = 1.6
|
|
if throwing_shoot_arrow(itemstack, user, pointed_thing) then
|
|
reloading(user)
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
itemstack:add_wear(65535/1000)
|
|
end
|
|
end
|
|
return itemstack
|
|
end,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_royal',
|
|
recipe = {
|
|
{'farming:string', 'group:wood', 'default:diamond'},
|
|
{'farming:string', '', 'default:gold_ingot'},
|
|
{'farming:string', 'group:wood', 'default:diamond'},
|
|
}
|
|
})
|
|
|
|
minetest.register_craft({
|
|
output = 'throwing:bow_royal',
|
|
recipe = {
|
|
{'default:diamond', 'group:wood', 'farming:string'},
|
|
{'default:gold_ingot', '', 'farming:string'},
|
|
{'default:diamond', 'group:wood', 'farming:string'},
|
|
}
|
|
})
|
|
end
|