123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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<StockpileGroup> groups = new List<StockpileGroup>();
- [HideIf("@initialized")] public List<StockpilePreset> resourceTypes = new List<StockpilePreset>();
- 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<Stockpile> 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<Stockpile> 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;
- }
- }
- }
- }
- }
- }
|