PlayerInventoryUI: Organizing the equipment and bag tabs. Creating a bag mechanic. #34
This commit is contained in:
@@ -15,6 +15,9 @@ core.register_on_joinplayer(function(player)
|
|||||||
inv:set_size('equip_legs', 2)
|
inv:set_size('equip_legs', 2)
|
||||||
inv:set_size('equip_feet', 2)
|
inv:set_size('equip_feet', 2)
|
||||||
inv:set_size('listring_equip', 1) -- Slot for correct listring.
|
inv:set_size('listring_equip', 1) -- Slot for correct listring.
|
||||||
|
inv:set_size('equip_back', 1)
|
||||||
|
inv:set_size('equip_left_thigh', 1)
|
||||||
|
inv:set_size('equip_right_thigh', 1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
core.register_chatcommand('show_inv',
|
core.register_chatcommand('show_inv',
|
||||||
|
|||||||
@@ -555,6 +555,111 @@ core.register_allow_player_inventory_action(function(player, action, inventory,
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---Returns the name of the inventory list that will contain the items in the bag.
|
||||||
|
---@param equipList string
|
||||||
|
---@return string
|
||||||
|
local function getBagInventoryListName(equipList)
|
||||||
|
local bagInventoryList = string.match(equipList, 'equip_(%S+)')
|
||||||
|
assert(bagInventoryList ~= nil)
|
||||||
|
|
||||||
|
return 'inventory_'..bagInventoryList
|
||||||
|
end
|
||||||
|
|
||||||
|
---Creates a corresponding inventory list by getting it from the item's metadata.
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@param equipList string
|
||||||
|
---@param bagItem ItemStack
|
||||||
|
local function onBagEquipped(inventory, equipList, bagItem)
|
||||||
|
local bagInventoryListName = getBagInventoryListName(equipList)
|
||||||
|
local bagInventoryList = Smc.getItemInventoryList(bagItem) or {}
|
||||||
|
|
||||||
|
inventory:set_size(bagInventoryListName, Smc.getItemInventoryListSize(bagItem))
|
||||||
|
inventory:set_list(bagInventoryListName, bagInventoryList)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Saves the inventory list corresponding to the equipped bag to the item's metadata.
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@param equipList string
|
||||||
|
---@param bagItem ItemStack
|
||||||
|
local function onBagUnequipped(inventory, equipList, bagItem)
|
||||||
|
local bagInventoryListName = getBagInventoryListName(equipList)
|
||||||
|
local bagInventoryList = inventory:get_list(bagInventoryListName)
|
||||||
|
|
||||||
|
Smc.setItemInventoryList(bagItem, bagInventoryList)
|
||||||
|
|
||||||
|
inventory:set_size(bagInventoryListName, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks if a slot is a bag slot by matching the inventory list name.
|
||||||
|
---@param equipGroup string
|
||||||
|
---@return boolean
|
||||||
|
local function isBagSlot(equipGroup)
|
||||||
|
if equipGroup == 'equip_back' or equipGroup == 'equip_left_thigh' or equipGroup == 'equip_right_thigh' then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Handling the movement of bags slots in the player's inventory.
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@param inventoryInfo table
|
||||||
|
local function onBagSlotMove(inventory, inventoryInfo)
|
||||||
|
-- We get the copy items currently at the destination and source positions.
|
||||||
|
local newItem = inventory:get_stack(inventoryInfo.to_list, inventoryInfo.to_index)
|
||||||
|
local oldItem = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
|
||||||
|
|
||||||
|
-- Handling the case where both slots are bag slots.
|
||||||
|
if isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then
|
||||||
|
-- If the target slot is empty (we put a bag from one slot to another),
|
||||||
|
-- then we unequip the bag and equip it again
|
||||||
|
if oldItem:is_empty() then
|
||||||
|
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
|
||||||
|
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
|
||||||
|
-- If a slot is occupied, we swap bags.
|
||||||
|
-- We unequip them, then equip them in their correct slots.
|
||||||
|
else
|
||||||
|
onBagUnequipped(inventory, inventoryInfo.to_list, oldItem)
|
||||||
|
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
|
||||||
|
onBagEquipped(inventory, inventoryInfo.from_list, oldItem)
|
||||||
|
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Handling the case where only the target slot is a bag slot
|
||||||
|
-- (equipping a bag from other inventory slots).
|
||||||
|
elseif isBagSlot(inventoryInfo.to_list) and not isBagSlot(inventoryInfo.from_list) then
|
||||||
|
-- If the target slot is empty (we put a bag from one slot to another),
|
||||||
|
-- then we just equip the bag.
|
||||||
|
if oldItem:is_empty() then
|
||||||
|
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
|
||||||
|
-- Else we swap the bags.
|
||||||
|
-- We unequip the old bag and equip the new one.
|
||||||
|
else
|
||||||
|
onBagUnequipped(inventory, inventoryInfo.to_list, oldItem)
|
||||||
|
onBagEquipped(inventory, inventoryInfo.to_list, newItem)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Handling the case where a bag is moved from a bag slot to a non-bag slot.
|
||||||
|
elseif not isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then
|
||||||
|
-- If the target slot is empty,
|
||||||
|
-- then we just unequip the bag.
|
||||||
|
if oldItem:is_empty() then
|
||||||
|
onBagUnequipped(inventory, inventoryInfo.from_list, newItem)
|
||||||
|
end
|
||||||
|
-- The case where the slot is occupied should not be handling here.
|
||||||
|
-- It is handling in the previous else-if block.
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We are updating the item stacks in the inventory,
|
||||||
|
-- since we were previously working with copies
|
||||||
|
-- (we need to save the items with new metadata).
|
||||||
|
inventory:set_stack(inventoryInfo.to_list, inventoryInfo.to_index, newItem)
|
||||||
|
inventory:set_stack(inventoryInfo.from_list, inventoryInfo.from_index, oldItem)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: скорее всего будет проблема со сваппингом сумок из сундуков
|
||||||
|
-- Возможно стоит запретить свапать сумки
|
||||||
|
|
||||||
-- Update the interface if the player has moved something from the equipment slots
|
-- Update the interface if the player has moved something from the equipment slots
|
||||||
-- (this enables and disables the slot overlay).
|
-- (this enables and disables the slot overlay).
|
||||||
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
|
||||||
@@ -562,6 +667,7 @@ core.register_on_player_inventory_action(function(player, action, inventory, inv
|
|||||||
if action == 'move' then
|
if action == 'move' then
|
||||||
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+')
|
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+')
|
||||||
if equipGroup ~= nil then
|
if equipGroup ~= nil then
|
||||||
|
onBagSlotMove(inventory, inventoryInfo)
|
||||||
PlayerInventoryUI:showForm(playerName)
|
PlayerInventoryUI:showForm(playerName)
|
||||||
end
|
end
|
||||||
elseif action == 'put' then
|
elseif action == 'put' then
|
||||||
|
|||||||
@@ -6,10 +6,76 @@ local Formspec = Smc.requireFormspec()
|
|||||||
---@return Formspec
|
---@return Formspec
|
||||||
local function getBagTab(self, playerName, tabType)
|
local function getBagTab(self, playerName, tabType)
|
||||||
local BagTab = Formspec:new()
|
local BagTab = Formspec:new()
|
||||||
|
local BagLists = Formspec:new()
|
||||||
|
local player = core.get_player_by_name(playerName)
|
||||||
|
assert(player ~= nil)
|
||||||
|
local inventory = player:get_inventory()
|
||||||
|
local inventoryBackSize = inventory:get_size('inventory_back') or 0
|
||||||
|
local inventoryLeftThighSize = inventory:get_size('inventory_left_thigh') or 0
|
||||||
|
local inventoryRightThighSize = inventory:get_size('inventory_right_thigh') or 0
|
||||||
|
|
||||||
|
local containerHeight = 0.0
|
||||||
|
|
||||||
|
if inventoryBackSize > 0 then
|
||||||
|
local bag = inventory:get_stack('equip_back', 1)
|
||||||
|
|
||||||
|
if not bag:is_empty() then
|
||||||
|
local background = Smc.getItemInventoryListBackground(bag)
|
||||||
|
local lines = math.ceil(inventoryBackSize/8)
|
||||||
|
|
||||||
|
BagLists
|
||||||
|
:image(0.5, 0, 9.875, (lines)*1.25+1, background)
|
||||||
|
:itemImage(0.5625, 0.0625, 1, 1, bag:get_name())
|
||||||
|
:label(1.7625, 0.5, bag:get_definition()._title or '')
|
||||||
|
:list('current_player', 'inventory_back', 0.5625, 1.25, 8, 4)
|
||||||
|
|
||||||
|
containerHeight = containerHeight + lines * 1.25 + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if inventoryLeftThighSize > 0 then
|
||||||
|
local bag = inventory:get_stack('equip_left_thigh', 1)
|
||||||
|
|
||||||
|
if not bag:is_empty() then
|
||||||
|
local background = Smc.getItemInventoryListBackground(bag)
|
||||||
|
local lines = math.ceil(inventoryLeftThighSize/8)
|
||||||
|
|
||||||
|
BagLists
|
||||||
|
:image(0.5, containerHeight, 9.875, (lines)*1.25+1, background)
|
||||||
|
:itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name())
|
||||||
|
:label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '')
|
||||||
|
:list('current_player', 'inventory_left_thigh', 0.5625, containerHeight + 1.25, 8, 4)
|
||||||
|
|
||||||
|
containerHeight = containerHeight + lines * 1.25 + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if inventoryRightThighSize > 0 then
|
||||||
|
local bag = inventory:get_stack('equip_right_thigh', 1)
|
||||||
|
|
||||||
|
if not bag:is_empty() then
|
||||||
|
local background = Smc.getItemInventoryListBackground(bag)
|
||||||
|
local lines = math.ceil(inventoryRightThighSize/8)
|
||||||
|
|
||||||
|
BagLists
|
||||||
|
:image(0.5, containerHeight, 9.875, (lines)*1.25+1, background)
|
||||||
|
:itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name())
|
||||||
|
:label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '')
|
||||||
|
:list('current_player', 'inventory_right_thigh', 0.5625, containerHeight + 1.25, 8, 4)
|
||||||
|
|
||||||
|
containerHeight = containerHeight + lines * 1.25 + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
BagTab
|
BagTab
|
||||||
:image(0, 0, 11, 9, 'player_inventory_tab_background.png')
|
:image(0, 0, 11, 9, 'player_inventory_tab_background.png')
|
||||||
:label(2,2, 'BAG PANE')
|
:scrollContainer(BagLists, 0, 0.5, 10.5, 8, 'bag_tab_scrollbar', 'vertical')
|
||||||
|
|
||||||
|
if containerHeight > 8 then
|
||||||
|
BagTab
|
||||||
|
:scrollbarOptions({min = 0, max = math.round((containerHeight-8)*10+10)})
|
||||||
|
:scrollbar(10.5, 0.5, 0.25, 8, 'vertical', 'bag_tab_scrollbar')
|
||||||
|
end
|
||||||
|
|
||||||
return BagTab
|
return BagTab
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,12 +15,17 @@ local function getEquipTab(self, playerName, tabType)
|
|||||||
EquipTab
|
EquipTab
|
||||||
:image(0, 0, 11, 9, 'player_inventory_equip_tab_background.png')
|
:image(0, 0, 11, 9, 'player_inventory_equip_tab_background.png')
|
||||||
-- Equipment slots:
|
-- Equipment slots:
|
||||||
:label(equipListsX, 0.75, 'cloth')
|
:list('current_player', 'equip_head', 56/16, 45/16, 1, 1)
|
||||||
:label(equipListsX+1.25, 0.75, 'armor')
|
:list('current_player', 'equip_head', 44/16, 33/16, 1, 1, 1)
|
||||||
:list('current_player', 'equip_head', equipListsX, 1, 2, 1)
|
:list('current_player', 'equip_torso', 104/16, 45/16, 1, 1)
|
||||||
:list('current_player', 'equip_torso', equipListsX, 2.25, 2, 1)
|
:list('current_player', 'equip_torso', 116/16, 33/16, 1, 1, 1)
|
||||||
:list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1)
|
:list('current_player', 'equip_legs', 56/16, 65/16, 1, 1)
|
||||||
:list('current_player', 'equip_feet', equipListsX, 4.75, 2, 1)
|
:list('current_player', 'equip_legs', 44/16, 77/16, 1, 1, 1)
|
||||||
|
:list('current_player', 'equip_feet', 104/16, 65/16, 1, 1)
|
||||||
|
:list('current_player', 'equip_feet', 116/16, 77/16, 1, 1, 1)
|
||||||
|
:list('current_player', 'equip_left_thigh', 60/16, 97/16, 1, 1)
|
||||||
|
:list('current_player', 'equip_back', 80/16, 97/16, 1, 1)
|
||||||
|
:list('current_player', 'equip_right_thigh', 100/16, 97/16, 1, 1)
|
||||||
-- Hidden equipment slot for correct listing
|
-- Hidden equipment slot for correct listing
|
||||||
-- (distribute equipment across its slots while holding shift):
|
-- (distribute equipment across its slots while holding shift):
|
||||||
:list('current_player', 'listring_equip', 0, 0, 1, 1)
|
:list('current_player', 'listring_equip', 0, 0, 1, 1)
|
||||||
@@ -46,16 +51,16 @@ local function getEquipTab(self, playerName, tabType)
|
|||||||
-- If the equipment slots are empty, then we apply an overlay.
|
-- If the equipment slots are empty, then we apply an overlay.
|
||||||
-- TODO: оверлей одежды
|
-- TODO: оверлей одежды
|
||||||
if inv:get_stack('equip_head', 2):is_empty() then
|
if inv:get_stack('equip_head', 2):is_empty() then
|
||||||
EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0))
|
EquipTab:image(44/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,0))
|
||||||
end
|
end
|
||||||
if inv:get_stack('equip_torso', 2):is_empty() then
|
if inv:get_stack('equip_torso', 2):is_empty() then
|
||||||
EquipTab:image(equipListsX+1.25, 2.25, 1, 1, slotsOverlayTileSheet:t(0,1))
|
EquipTab:image(116/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,1))
|
||||||
end
|
end
|
||||||
if inv:get_stack('equip_legs', 2):is_empty() then
|
if inv:get_stack('equip_legs', 2):is_empty() then
|
||||||
EquipTab:image(equipListsX+1.25, 3.5, 1, 1, slotsOverlayTileSheet:t(0,2))
|
EquipTab:image(44/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,2))
|
||||||
end
|
end
|
||||||
if inv:get_stack('equip_feet', 2):is_empty() then
|
if inv:get_stack('equip_feet', 2):is_empty() then
|
||||||
EquipTab:image(equipListsX+1.25, 4.75, 1, 1, slotsOverlayTileSheet:t(0,3))
|
EquipTab:image(116/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,3))
|
||||||
end
|
end
|
||||||
|
|
||||||
return EquipTab
|
return EquipTab
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 100 KiB After Width: | Height: | Size: 100 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Reference in New Issue
Block a user