StatsController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using Sirenix.Serialization;
  7. namespace KairoEngine.Stats
  8. {
  9. [System.Serializable]
  10. public class StatsController : ISerializationCallbackReceiver
  11. {
  12. [OnValueChanged("Setup"), HideIf("@statGroup != null")]
  13. public StatGroup statGroup;
  14. [OnInspectorInit("Initialize")]
  15. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)]
  16. public List<Stat> stats;
  17. [InlineProperty, HideLabel, ShowIf("@statusEffects.statusEffects.Count > 0")]
  18. public StatusEffectsController statusEffects;
  19. public StatGroup StatGroup
  20. {
  21. get => statGroup;
  22. set
  23. {
  24. statGroup = value;
  25. if(statGroup != null) Setup();
  26. }
  27. }
  28. public void Setup()
  29. {
  30. if(StatGroup == null) return;
  31. stats = new List<Stat>();
  32. foreach (var statTemplate in StatGroup.statTemplates)
  33. {
  34. Stat stat = new Stat(statTemplate, this);
  35. stats.Add(stat);
  36. }
  37. for (int a = 0; a < 4; a++)
  38. {
  39. for (int i = 0; i < stats.Count; i++) stats[i].Initialize();
  40. }
  41. UpdateStats();
  42. }
  43. public void Initialize()
  44. {
  45. if(statGroup == null) return;
  46. if(stats != null)
  47. {
  48. if(stats.Count == 0)
  49. {
  50. Setup();
  51. return;
  52. }
  53. }
  54. for (int i = 0; i < StatGroup.statTemplates.Count; i++)
  55. {
  56. bool found = false;
  57. for (int a = 0; a < stats.Count; a++)
  58. {
  59. if(stats[a].title == StatGroup.statTemplates[i].title) found = true;
  60. }
  61. if(found == false)
  62. {
  63. Stat stat = new Stat(StatGroup.statTemplates[i], this);
  64. stats.Add(stat);
  65. }
  66. }
  67. List<Stat> newStats = new List<Stat>();
  68. foreach(var stat in stats) newStats.Add(Stat.Deserialize(stat.Serialize(), this));
  69. stats = newStats;
  70. if(statusEffects != null) statusEffects.SetStatsController(this);
  71. UpdateStats();
  72. }
  73. public void UpdateStats()
  74. {
  75. foreach (var stat in stats) stat.Update();
  76. foreach (var stat in stats) stat.Update();
  77. }
  78. public int GetStatValue(string title)
  79. {
  80. int result = 0;
  81. Stat stat = GetStat(title);
  82. if(stat != null) result = stat.value;
  83. return result;
  84. }
  85. public Stat GetStat(string title)
  86. {
  87. for (int i = 0; i < stats.Count; i++)
  88. {
  89. if(stats[i].title == title) return stats[i];
  90. }
  91. return null;
  92. }
  93. public StatTemplate GetStatTemplate(string title)
  94. {
  95. if(StatGroup == null) return null;
  96. for (int i = 0; i < StatGroup.statTemplates.Count; i++)
  97. {
  98. if(StatGroup.statTemplates[i].title == title) return StatGroup.statTemplates[i];
  99. }
  100. return null;
  101. }
  102. public void OnBeforeSerialize()
  103. {
  104. }
  105. public void OnAfterDeserialize()
  106. {
  107. for (int i = 0; i < stats.Count; i++)
  108. {
  109. stats[i].controller = this;
  110. if(stats[i].modifiers != null) stats[i].modifiers.stat = stats[i];
  111. }
  112. if(statusEffects != null) statusEffects.controller = this;
  113. }
  114. }
  115. }