Files
UnrealLuaDemo/Script/lua/Tests/Gameplay/InventorySystem.lua
T

160 lines
4.8 KiB
Lua
Raw Normal View History

local ____exports = __module.env
____exports.InventorySystem = __TS__DefineClass("InventorySystem", __module)
local InventorySystem = ____exports.InventorySystem
InventorySystem.name = "InventorySystem"
function InventorySystem.prototype.____constructor(self, capacity)
self.defs = {}
self.slots = {}
self.capacity = capacity
end
function InventorySystem.prototype.RegisterItemDef(self, def)
self.defs[def.itemId] = def
end
function InventorySystem.prototype.AddItem(self, itemId, amount)
local def = self.defs[itemId]
if def == nil or amount <= 0 then
return amount
end
local remaining = amount
do
local i = 0
while i < self.capacity and remaining > 0 do
local slot = self.slots[i]
if slot ~= nil and slot.itemId == itemId and slot.count < def.maxStack then
local space = def.maxStack - slot.count
local add = math.min(space, remaining)
slot.count = slot.count + add
remaining = remaining - add
end
i = i + 1
end
end
do
local i = 0
while i < self.capacity and remaining > 0 do
if self.slots[i] == nil then
local add = math.min(def.maxStack, remaining)
self.slots[i] = {itemId = itemId, count = add}
remaining = remaining - add
end
i = i + 1
end
end
return remaining
end
function InventorySystem.prototype.ConsumeItem(self, itemId, amount)
if amount <= 0 or self:GetItemCount(itemId) < amount then
return false
end
local remaining = amount
do
local i = self.capacity - 1
while i >= 0 and remaining > 0 do
local slot = self.slots[i]
if slot ~= nil and slot.itemId == itemId then
local take = math.min(slot.count, remaining)
slot.count = slot.count - take
remaining = remaining - take
if slot.count <= 0 then
self.slots[i] = nil
end
end
i = i - 1
end
end
return true
end
function InventorySystem.prototype.SwapSlots(self, slotA, slotB)
if slotA < 0 or slotA >= self.capacity or slotB < 0 or slotB >= self.capacity then
return false
end
local tmp = self.slots[slotA]
self.slots[slotA] = self.slots[slotB]
self.slots[slotB] = tmp
return true
end
function InventorySystem.prototype.SplitSlot(self, fromSlot, toSlot, splitCount)
if fromSlot < 0 or fromSlot >= self.capacity or toSlot < 0 or toSlot >= self.capacity or fromSlot == toSlot then
return false
end
local src = self.slots[fromSlot]
if src == nil or self.slots[toSlot] ~= nil then
return false
end
if splitCount <= 0 or splitCount >= src.count then
return false
end
src.count = src.count - splitCount
self.slots[toSlot] = {itemId = src.itemId, count = splitCount}
return true
end
function InventorySystem.prototype.GetItemCount(self, itemId)
local total = 0
do
local i = 0
while i < self.capacity do
local slot = self.slots[i]
if slot ~= nil and slot.itemId == itemId then
total = total + slot.count
end
i = i + 1
end
end
return total
end
function InventorySystem.prototype.GetSlot(self, index)
return self.slots[index]
end
function InventorySystem.prototype.GetUsedSlotCount(self)
local used = 0
do
local i = 0
while i < self.capacity do
if self.slots[i] ~= nil then
used = used + 1
end
i = i + 1
end
end
return used
end
function InventorySystem.prototype.FilterByCategory(self, category)
local out = {}
do
local i = 0
while i < self.capacity do
local slot = self.slots[i]
if slot ~= nil then
local def = self.defs[slot.itemId]
if def ~= nil and def.category == category then
out[#out + 1] = slot
end
end
i = i + 1
end
end
return out
end
function InventorySystem.prototype.Serialize(self)
local copiedSlots = {}
do
local i = 0
while i < self.capacity do
local s = self.slots[i]
if s ~= nil then
copiedSlots[i] = {itemId = s.itemId, count = s.count}
end
i = i + 1
end
end
return {capacity = self.capacity, slots = copiedSlots}
end
function InventorySystem.prototype.Deserialize(self, data)
self.capacity = data.capacity
self.slots = {}
for idx, s in pairs(data.slots) do
self.slots[idx] = {itemId = s.itemId, count = s.count}
end
end
function __module.UpdateImports()
end