181 lines
5.4 KiB
C#
181 lines
5.4 KiB
C#
using System.Collections.Generic;
|
|
using game;
|
|
|
|
public class Inventory : IPersistent
|
|
{
|
|
private List<InventoryAmountItem> _purchased = new List<InventoryAmountItem>();
|
|
private List<InventoryAmountItem> _equipped = new List<InventoryAmountItem>();
|
|
|
|
public bool IsPurchasedItem(uint itemId) => ListContainsId(_purchased, itemId);
|
|
public bool IsEquippedItem(uint itemId) => ListContainsId(_equipped, itemId);
|
|
|
|
private bool IsPurchasedItem(uint itemId, out InventoryAmountItem item) =>
|
|
ListContainsId(_purchased, itemId, out item);
|
|
|
|
private bool IsEquippedItem(uint itemId, out InventoryAmountItem item) =>
|
|
ListContainsId(_equipped, itemId, out item);
|
|
|
|
private bool ListContainsId(List<InventoryAmountItem> list, uint id) =>
|
|
ListContainsId(list, id, out InventoryAmountItem _);
|
|
|
|
private bool ListContainsId(List<InventoryAmountItem> list, uint id, out InventoryAmountItem result)
|
|
{
|
|
foreach (var item in list)
|
|
{
|
|
if (item.ID == id)
|
|
{
|
|
result = item;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
result = null;
|
|
return false;
|
|
}
|
|
|
|
public uint RemoveConsumableItem(EnumConsumable type, uint amount)
|
|
{
|
|
uint coinsAmount = GetItemAmount(EnumHelper.IdFromConsumableType(type));
|
|
uint remainingCoins = coinsAmount > amount ? coinsAmount - amount : 0;
|
|
|
|
SetOrCreatePurchasedItem(EnumHelper.IdFromConsumableType(type), remainingCoins);
|
|
return remainingCoins;
|
|
}
|
|
|
|
public uint AddConsumableItem(EnumConsumable type, uint amount)
|
|
{
|
|
uint coinsAmount = GetItemAmount(EnumHelper.IdFromConsumableType(type));
|
|
SetOrCreatePurchasedItem(EnumHelper.IdFromConsumableType(type), coinsAmount + amount);
|
|
return coinsAmount + amount;
|
|
}
|
|
|
|
public uint GetConsumableItemAmount(EnumConsumable type)
|
|
{
|
|
return GetItemAmount(EnumHelper.IdFromConsumableType(type));
|
|
}
|
|
|
|
private uint GetItemAmount(uint itemId)
|
|
{
|
|
uint amount = 0;
|
|
foreach (InventoryAmountItem item in _purchased)
|
|
{
|
|
if (item.Item.id == itemId)
|
|
amount += item.Amount;
|
|
}
|
|
|
|
return amount;
|
|
}
|
|
|
|
private void SetOrCreatePurchasedItem(uint itemID, uint amount)
|
|
{
|
|
for (int i = 0; i < _purchased.Count; i++)
|
|
{
|
|
if (_purchased[i].ID == itemID)
|
|
{
|
|
_purchased[i].Amount = amount;
|
|
return;
|
|
}
|
|
}
|
|
|
|
_purchased.Add(new InventoryAmountItem(itemID, amount));
|
|
}
|
|
|
|
public bool TryEquipItem(uint itemId)
|
|
{
|
|
if (IsEquippedItem(itemId))
|
|
return false;
|
|
|
|
if (IsPurchasedItem(itemId, out InventoryAmountItem result))
|
|
{
|
|
if (result.Item.slotType == EnumSlotType.NONE)
|
|
return false;
|
|
|
|
Equip(result);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void AddPurchaseItem(uint itemID, uint amount)
|
|
{
|
|
for (int i = 0; i < _purchased.Count; i++)
|
|
{
|
|
if (_purchased[i].Item.id == itemID)
|
|
{
|
|
_purchased[i].Amount += amount;
|
|
return;
|
|
}
|
|
}
|
|
|
|
_purchased.Add(new InventoryAmountItem(itemID, amount));
|
|
}
|
|
|
|
private void Equip(InventoryAmountItem item)
|
|
{
|
|
for (int i = 0; i < _equipped.Count; i++)
|
|
{
|
|
if (_equipped[i].Item.slotType == item.Item.slotType)
|
|
{
|
|
_equipped[i] = item;
|
|
return;
|
|
}
|
|
}
|
|
|
|
_equipped.Add(item);
|
|
}
|
|
|
|
private static List<InventoryAmountItem> FromSaveData(List<DataItem> items)
|
|
{
|
|
List<InventoryAmountItem> saveData = new List<InventoryAmountItem>(items.Count);
|
|
foreach (var item in items)
|
|
saveData.Add(InventoryAmountItem.FromSaveData(item));
|
|
|
|
return saveData;
|
|
}
|
|
|
|
private static List<DataItem> ToSaveData(List<InventoryAmountItem> items)
|
|
{
|
|
List<DataItem> saveData = new List<DataItem>(items.Count);
|
|
foreach (var item in items)
|
|
saveData.Add(item.ToSaveData());
|
|
|
|
return saveData;
|
|
}
|
|
|
|
public void Load(DataGame data)
|
|
{
|
|
_purchased = FromSaveData(data.inventory.purchased);
|
|
_equipped = FromSaveData(data.inventory.equipped);
|
|
}
|
|
|
|
public void Save(DataGame data)
|
|
{
|
|
data.inventory.purchased = ToSaveData(_purchased);
|
|
data.inventory.equipped = ToSaveData(_equipped);
|
|
}
|
|
|
|
private class InventoryAmountItem
|
|
{
|
|
public uint ID => Item.id;
|
|
public uint Amount { get; set; }
|
|
public ConfInventoryItem Item => _item;
|
|
|
|
private readonly ConfInventoryItem _item;
|
|
|
|
public InventoryAmountItem(uint id, uint amount)
|
|
{
|
|
Amount = amount;
|
|
_item = G.Instance.Configs.Get<ConfInventoryItem>(id);
|
|
}
|
|
|
|
public DataItem ToSaveData() => ConvertToSaveData(this);
|
|
|
|
public static InventoryAmountItem FromSaveData(DataItem saveData) => new InventoryAmountItem(saveData.protoId, saveData.amount);
|
|
public static DataItem ConvertToSaveData(InventoryAmountItem item)
|
|
{
|
|
DataItem saveData = new DataItem() {amount = item.Amount, protoId = item.ID};
|
|
return saveData;
|
|
}
|
|
}
|
|
}
|