loading and shooting from quiver/arrow slots and new b3d character

This commit is contained in:
Juraj Vajda 2022-10-26 23:44:26 -04:00
parent 1b52ec8194
commit b84c0409e8
6 changed files with 129 additions and 49 deletions

View File

@ -62,5 +62,6 @@ read_globals = {
"armor", "armor",
"default", "default",
"i3", "i3",
"unified_inventory" "unified_inventory",
"player_api"
} }

100
api.lua
View File

@ -444,7 +444,6 @@ end
function XBows.load(self, itemstack, user, pointed_thing) function XBows.load(self, itemstack, user, pointed_thing)
local player_name = user:get_player_name() local player_name = user:get_player_name()
local inv = user:get_inventory()--[[@as InvRef]] local inv = user:get_inventory()--[[@as InvRef]]
local inv_list = inv:get_list('main')
local bow_name = itemstack:get_name() local bow_name = itemstack:get_name()
local bow_def = self.registered_bows[bow_name] local bow_def = self.registered_bows[bow_name]
---@alias ItemStackArrows {["stack"]: ItemStack, ["idx"]: number|integer}[] ---@alias ItemStackArrows {["stack"]: ItemStack, ["idx"]: number|integer}[]
@ -476,17 +475,24 @@ function XBows.load(self, itemstack, user, pointed_thing)
XBowsQuiver:remove_hud(user) XBowsQuiver:remove_hud(user)
---find itemstack arrow in players inventory ---find itemstack arrow in players inventory
for i, st in ipairs(inv_list) do local arrow_stack = inv:get_stack('x_bows:arrow_inv', 1)
local st_name = st:get_name() local is_allowed_ammunition = self:is_allowed_ammunition(bow_name, arrow_stack:get_name())
if not st:is_empty() and self.registered_arrows[st_name] then if self.registered_arrows[arrow_stack:get_name()] and is_allowed_ammunition then
local is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name) table.insert(itemstack_arrows, {stack = arrow_stack, idx = 1})
end
if self.registered_arrows[st_name] and is_allowed_ammunition then -- for i, st in ipairs(inv_list) do
table.insert(itemstack_arrows, {stack = st, idx = i}) -- local st_name = st:get_name()
end
end -- if not st:is_empty() and self.registered_arrows[st_name] then
end -- local is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name)
-- if self.registered_arrows[st_name] and is_allowed_ammunition then
-- table.insert(itemstack_arrows, {stack = st, idx = i})
-- end
-- end
-- end
-- take 1st found arrow in the list -- take 1st found arrow in the list
itemstack_arrow = #itemstack_arrows > 0 and itemstack_arrows[1].stack or nil itemstack_arrow = #itemstack_arrows > 0 and itemstack_arrows[1].stack or nil
@ -515,7 +521,7 @@ function XBows.load(self, itemstack, user, pointed_thing)
if not self:is_creative(v_user:get_player_name()) and v_itemstack_arrow_meta:get_int('is_arrow_from_quiver') ~= 1 then if not self:is_creative(v_user:get_player_name()) and v_itemstack_arrow_meta:get_int('is_arrow_from_quiver') ~= 1 then
v_itemstack_arrow:take_item() v_itemstack_arrow:take_item()
v_inv:set_stack('main', v_itemstack_arrows[1].idx, v_itemstack_arrow) v_inv:set_stack('x_bows:arrow_inv', v_itemstack_arrows[1].idx, v_itemstack_arrow)
end end
end end
end, user, bow_name, itemstack_arrow, inv, itemstack_arrows) end, user, bow_name, itemstack_arrow, inv, itemstack_arrows)
@ -1450,14 +1456,11 @@ function XBowsQuiver.get_itemstack_arrow_from_quiver(self, player)
local quiver_id local quiver_id
local quiver_name local quiver_name
---find matching quiver item in players inventory with the open formspec name ---check quiver inventory slot
if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then if player_inv and player_inv:contains_item('x_bows:quiver_inv', 'x_bows:quiver') then
local inv_list = player_inv:get_list('main')
for i, st in ipairs(inv_list) do
if not st:is_empty() and st:get_name() == 'x_bows:quiver' then
local st_meta = st:get_meta()
local player_name = player:get_player_name() local player_name = player:get_player_name()
local quiver_stack = player_inv:get_stack('x_bows:quiver_inv', 1)
local st_meta = quiver_stack:get_meta()
quiver_id = st_meta:get_string('quiver_id') quiver_id = st_meta:get_string('quiver_id')
local detached_inv = self:get_or_create_detached_inv( local detached_inv = self:get_or_create_detached_inv(
@ -1478,7 +1481,7 @@ function XBowsQuiver.get_itemstack_arrow_from_quiver(self, player)
local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name()) local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name())
if is_allowed_ammunition then if is_allowed_ammunition then
quiver_name = st:get_name() quiver_name = quiver_stack:get_name()
found_arrow_stack = qst:take_item() found_arrow_stack = qst:take_item()
found_arrow_stack_idx = j found_arrow_stack_idx = j
@ -1490,16 +1493,65 @@ function XBowsQuiver.get_itemstack_arrow_from_quiver(self, player)
end end
end end
end end
end
if found_arrow_stack then if found_arrow_stack then
---show HUD - quiver inventory ---show HUD - quiver inventory
self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx) self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx)
end
end
break ---check arrows inventory slot
end
end ---find matching quiver item in players inventory with the open formspec name
end -- if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then
-- local inv_list = player_inv:get_list('main')
-- for i, st in ipairs(inv_list) do
-- if not st:is_empty() and st:get_name() == 'x_bows:quiver' then
-- local st_meta = st:get_meta()
-- local player_name = player:get_player_name()
-- quiver_id = st_meta:get_string('quiver_id')
-- local detached_inv = self:get_or_create_detached_inv(
-- quiver_id,
-- player_name,
-- st_meta:get_string('quiver_items')
-- )
-- if not detached_inv:is_empty('main') then
-- local detached_inv_list = detached_inv:get_list('main')
-- ---find arrows inside quiver inventory
-- for j, qst in ipairs(detached_inv_list) do
-- ---save copy of inv list before we take the item
-- table.insert(prev_detached_inv_list, detached_inv:get_stack('main', j))
-- if not qst:is_empty() and not found_arrow_stack then
-- local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name())
-- if is_allowed_ammunition then
-- quiver_name = st:get_name()
-- found_arrow_stack = qst:take_item()
-- found_arrow_stack_idx = j
-- if not self:is_creative(player_name) then
-- detached_inv:set_list('main', detached_inv_list)
-- self:save(detached_inv, player, true)
-- end
-- end
-- end
-- end
-- end
-- end
-- if found_arrow_stack then
-- ---show HUD - quiver inventory
-- self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx)
-- break
-- end
-- end
-- end
return { return {
found_arrow_stack = found_arrow_stack, found_arrow_stack = found_arrow_stack,

View File

@ -48,8 +48,35 @@ minetest.register_on_joinplayer(function(player)
st_meta:get_string('quiver_items') st_meta:get_string('quiver_items')
) )
end end
player_api.set_model(player, 'x_bows_character.b3d')
player_api.set_textures(player, {
'character.png',
'x_bows_quiver_mesh.png'
})
end) end)
---player api
player_api.register_model('x_bows_character.b3d', {
animation_speed = 30,
textures = {"character.png"},
animations = {
-- Standard animations.
stand = {x = 0, y = 79},
lay = {x = 162, y = 166, eye_height = 0.3, override_local = true,
collisionbox = {-0.6, 0.0, -0.6, 0.6, 0.3, 0.6}},
walk = {x = 168, y = 187},
mine = {x = 189, y = 198},
walk_mine = {x = 200, y = 219},
sit = {x = 81, y = 160, eye_height = 0.8, override_local = true,
collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.0, 0.3}}
},
collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3},
stepheight = 0.6,
eye_height = 1.47
})
---formspec callbacks ---formspec callbacks
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info) minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
---arrow inventory ---arrow inventory

BIN
models/x_bows_character.b3d Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 235 B

After

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 B