123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using Sirenix.Serialization;
- namespace KairoEngine.Stats
- {
- [System.Serializable]
- public class StatsController : ISerializationCallbackReceiver
- {
- [OnValueChanged("Setup"), HideIf("@statGroup != null")]
- public StatGroup statGroup;
- [OnInspectorInit("Initialize")]
- [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)]
- public List<Stat> stats;
- [InlineProperty, HideLabel, ShowIf("@statusEffects.statusEffects.Count > 0")]
- public StatusEffectsController statusEffects;
- public StatGroup StatGroup
- {
- get => statGroup;
- set
- {
- statGroup = value;
- if(statGroup != null) Setup();
- }
- }
- public void Setup()
- {
- if(StatGroup == null) return;
- stats = new List<Stat>();
- foreach (var statTemplate in StatGroup.statTemplates)
- {
- Stat stat = new Stat(statTemplate, this);
- stats.Add(stat);
- }
- for (int a = 0; a < 4; a++)
- {
- for (int i = 0; i < stats.Count; i++) stats[i].Initialize();
- }
- UpdateStats();
- }
- public void Initialize()
- {
- if(statGroup == null) return;
- if(stats != null)
- {
- if(stats.Count == 0)
- {
- Setup();
- return;
- }
- }
- for (int i = 0; i < StatGroup.statTemplates.Count; i++)
- {
- bool found = false;
- for (int a = 0; a < stats.Count; a++)
- {
- if(stats[a].title == StatGroup.statTemplates[i].title) found = true;
- }
- if(found == false)
- {
- Stat stat = new Stat(StatGroup.statTemplates[i], this);
- stats.Add(stat);
- }
- }
- List<Stat> newStats = new List<Stat>();
- foreach(var stat in stats) newStats.Add(Stat.Deserialize(stat.Serialize(), this));
- stats = newStats;
- if(statusEffects != null) statusEffects.SetStatsController(this);
- UpdateStats();
- }
- public void UpdateStats()
- {
- foreach (var stat in stats) stat.Update();
- foreach (var stat in stats) stat.Update();
- }
- public int GetStatValue(string title)
- {
- int result = 0;
- Stat stat = GetStat(title);
- if(stat != null) result = stat.value;
- return result;
- }
- public Stat GetStat(string title)
- {
- for (int i = 0; i < stats.Count; i++)
- {
- if(stats[i].title == title) return stats[i];
- }
- return null;
- }
- public StatTemplate GetStatTemplate(string title)
- {
- if(StatGroup == null) return null;
- for (int i = 0; i < StatGroup.statTemplates.Count; i++)
- {
- if(StatGroup.statTemplates[i].title == title) return StatGroup.statTemplates[i];
- }
- return null;
- }
- public void OnBeforeSerialize()
- {
-
- }
- public void OnAfterDeserialize()
- {
-
- for (int i = 0; i < stats.Count; i++)
- {
- stats[i].controller = this;
- if(stats[i].modifiers != null) stats[i].modifiers.stat = stats[i];
- }
- if(statusEffects != null) statusEffects.controller = this;
- }
- }
- }
|