89 lines
No EOL
2.5 KiB
C#
89 lines
No EOL
2.5 KiB
C#
// Assets/ShiroginSDK/Runtime/Data/InventoryData.cs
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using ShiroginSDK.Runtime.Modules.Data.Scripts.Enums;
|
|
using ShiroginSDK.Runtime.Modules.Data.Scripts.Models;
|
|
using UnityEngine;
|
|
|
|
namespace ShiroginSDK.Runtime.Modules.Data.Scripts
|
|
{
|
|
[Serializable]
|
|
public class InventoryData : BaseData
|
|
{
|
|
public List<OwnedItem> Items = new();
|
|
public List<int> UnlockedCharacters = new();
|
|
protected override string PrefsKey => "INVENTORY_DATA";
|
|
|
|
private OwnedItem FindItem(string itemId)
|
|
{
|
|
return Items.Find(i => i.ItemId == itemId);
|
|
}
|
|
|
|
public int GetItemCount(string itemId)
|
|
{
|
|
return FindItem(itemId)?.Quantity ?? 0;
|
|
}
|
|
|
|
public bool HasItem(string itemId, int amount = 1)
|
|
{
|
|
return GetItemCount(itemId) >= amount;
|
|
}
|
|
|
|
public void AddItem(string itemId, ItemType type, int amount = 1)
|
|
{
|
|
if (amount <= 0) return;
|
|
var item = FindItem(itemId);
|
|
if (item == null)
|
|
Items.Add(new OwnedItem(itemId, type, amount));
|
|
else
|
|
item.Quantity += amount;
|
|
Save();
|
|
}
|
|
|
|
public bool RemoveItem(string itemId, int amount = 1)
|
|
{
|
|
if (amount <= 0) return false;
|
|
var item = FindItem(itemId);
|
|
if (item == null || item.Quantity < amount) return false;
|
|
|
|
item.Quantity -= amount;
|
|
if (item.Quantity <= 0)
|
|
Items.Remove(item);
|
|
|
|
Save();
|
|
return true;
|
|
}
|
|
|
|
public void SetItemCount(string itemId, ItemType type, int newCount)
|
|
{
|
|
newCount = Mathf.Max(0, newCount);
|
|
var item = FindItem(itemId);
|
|
if (item == null)
|
|
{
|
|
if (newCount > 0) Items.Add(new OwnedItem(itemId, type, newCount));
|
|
}
|
|
else
|
|
{
|
|
item.Quantity = newCount;
|
|
if (item.Quantity == 0) Items.Remove(item);
|
|
}
|
|
|
|
Save();
|
|
}
|
|
|
|
// Karakter
|
|
public bool UnlockCharacter(int characterId)
|
|
{
|
|
if (UnlockedCharacters.Contains(characterId)) return false;
|
|
UnlockedCharacters.Add(characterId);
|
|
Save();
|
|
return true;
|
|
}
|
|
|
|
public bool IsCharacterUnlocked(int characterId)
|
|
{
|
|
return UnlockedCharacters.Contains(characterId);
|
|
}
|
|
}
|
|
} |