using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Stats; using Sirenix.OdinInspector; namespace KairoEngine.Stockpiles { [HideMonoScript] public class StockpileManager : MonoBehaviour { public static StockpileManager instance; public List groups = new List(); [HideIf("@initialized")] public List resourceTypes = new List(); private bool initialized = false; private void Awake() { if(instance == null) instance = this; else { Debug.LogError("A instance of StockpileManager already exists, destroying new one.", instance.gameObject); Destroy(this.gameObject); } } private void Start() { for (int i = 0; i < groups.Count; i++) groups[i].Initialize(resourceTypes); initialized = true; } public Stat GetStockpileStat(string owner, string stockpileName) { for (int i = 0; i < groups.Count; i++) { if(groups[i].owner == owner) { return groups[i].stats.GetStat(stockpileName); } } return null; } public int GetStockpileAmmount(string owner, string stockpileName) { var stat = GetStockpileStat(owner, stockpileName); return stat != null ? stat.value : 0; } public int GetStockpileCapacity(string owner, string stockpileName) { var stat = GetStockpileStat(owner, stockpileName); return stat != null ? stat.maxValue : 0; } public void AddToStockpileAmmout(string owner, string stockpileName, int ammount) { var stat = GetStockpileStat(owner, stockpileName); if(stat != null) stat.value += ammount; } public void AddToStockpileCapacity(string owner, string stockpileName, int capacity) { var stat = GetStockpileStat(owner, stockpileName); if(stat != null) stat.maxValue += capacity; } public bool HasResources(string stockpileOwner, List costList) { for (int i = 0; i < costList.Count; i++) { var ammount = GetStockpileAmmount(stockpileOwner, costList[i].stockpileType.title); if(ammount < costList[i].ammount) return false; } return true; } public bool HasResource(string stockpileOwner, Stockpile cost) { var ammount = GetStockpileAmmount(stockpileOwner, cost.stockpileType.title); if(ammount < cost.ammount) return false; else return true; } public void RemoveResources(string stockpileOwner, List costList) { for (int i = 0; i < costList.Count; i++) { var stat = GetStockpileStat(stockpileOwner, costList[i].stockpileType.title); if(stat != null) stat.value -= costList[i].ammount; } } public void SetStockpile(string owner, string stockpileName, int ammount, int capacity) { var stat = GetStockpileStat(owner, stockpileName); if(stat != null) { stat.maxValue = capacity; stat.value = ammount; } else Debug.LogError($"Could not find stockpile stat named {stockpileName} owned by {owner}"); } public void ResetStockpiles(string owner) { for (int i = 0; i < groups.Count; i++) { if(groups[i].owner == owner) { for (int a = 0; a < groups[i].stats.stats.Count; a++) { groups[i].stats.stats[a].value = groups[i].stats.stats[a].minValue; groups[i].stats.stats[a].maxValue = groups[i].stats.stats[a].minValue; } } } } } }