StockpileManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Stats;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.Stockpiles
  7. {
  8. [HideMonoScript]
  9. public class StockpileManager : MonoBehaviour
  10. {
  11. public static StockpileManager instance;
  12. public List<StockpileGroup> groups = new List<StockpileGroup>();
  13. [HideIf("@initialized")] public List<StockpilePreset> resourceTypes = new List<StockpilePreset>();
  14. private bool initialized = false;
  15. private void Awake()
  16. {
  17. if(instance == null) instance = this;
  18. else
  19. {
  20. Debug.LogError("A instance of StockpileManager already exists, destroying new one.", instance.gameObject);
  21. Destroy(this.gameObject);
  22. }
  23. }
  24. private void Start()
  25. {
  26. for (int i = 0; i < groups.Count; i++) groups[i].Initialize(resourceTypes);
  27. initialized = true;
  28. }
  29. public Stat GetStockpileStat(string owner, string stockpileName)
  30. {
  31. for (int i = 0; i < groups.Count; i++)
  32. {
  33. if(groups[i].owner == owner)
  34. {
  35. return groups[i].stats.GetStat(stockpileName);
  36. }
  37. }
  38. return null;
  39. }
  40. public int GetStockpileAmmount(string owner, string stockpileName)
  41. {
  42. var stat = GetStockpileStat(owner, stockpileName);
  43. return stat != null ? stat.value : 0;
  44. }
  45. public int GetStockpileCapacity(string owner, string stockpileName)
  46. {
  47. var stat = GetStockpileStat(owner, stockpileName);
  48. return stat != null ? stat.maxValue : 0;
  49. }
  50. public void AddToStockpileAmmout(string owner, string stockpileName, int ammount)
  51. {
  52. var stat = GetStockpileStat(owner, stockpileName);
  53. if(stat != null) stat.value += ammount;
  54. }
  55. public void AddToStockpileCapacity(string owner, string stockpileName, int capacity)
  56. {
  57. var stat = GetStockpileStat(owner, stockpileName);
  58. if(stat != null) stat.maxValue += capacity;
  59. }
  60. public bool HasResources(string stockpileOwner, List<Stockpile> costList)
  61. {
  62. for (int i = 0; i < costList.Count; i++)
  63. {
  64. var ammount = GetStockpileAmmount(stockpileOwner, costList[i].stockpileType.title);
  65. if(ammount < costList[i].ammount) return false;
  66. }
  67. return true;
  68. }
  69. public bool HasResource(string stockpileOwner, Stockpile cost)
  70. {
  71. var ammount = GetStockpileAmmount(stockpileOwner, cost.stockpileType.title);
  72. if(ammount < cost.ammount) return false;
  73. else return true;
  74. }
  75. public void RemoveResources(string stockpileOwner, List<Stockpile> costList)
  76. {
  77. for (int i = 0; i < costList.Count; i++)
  78. {
  79. var stat = GetStockpileStat(stockpileOwner, costList[i].stockpileType.title);
  80. if(stat != null) stat.value -= costList[i].ammount;
  81. }
  82. }
  83. public void SetStockpile(string owner, string stockpileName, int ammount, int capacity)
  84. {
  85. var stat = GetStockpileStat(owner, stockpileName);
  86. if(stat != null)
  87. {
  88. stat.maxValue = capacity;
  89. stat.value = ammount;
  90. }
  91. else Debug.LogError($"Could not find stockpile stat named {stockpileName} owned by {owner}");
  92. }
  93. public void ResetStockpiles(string owner)
  94. {
  95. for (int i = 0; i < groups.Count; i++)
  96. {
  97. if(groups[i].owner == owner)
  98. {
  99. for (int a = 0; a < groups[i].stats.stats.Count; a++)
  100. {
  101. groups[i].stats.stats[a].value = groups[i].stats.stats[a].minValue;
  102. groups[i].stats.stats[a].maxValue = groups[i].stats.stats[a].minValue;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }