using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; namespace KairoEngine.Stats { [System.Serializable] public class StatModifiersController { [ListDrawerSettings(HideAddButton = false, HideRemoveButton = false, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)] [PropertyOrder(1), LabelText("Modifiers"), OnCollectionChanged("Calculate")] public List list = new List(); [HideInInspector] public int value = 0; [HideInInspector] public int minValue = 0; [HideInInspector] public int maxValue = 0; [System.NonSerialized] public Stat stat; public string pretyValue() { if(value > 0) return $"+{value}"; else if (value == 0) return " 0 "; else return $"{value}"; } public string pretyCount() { if(list.Count > 99) return $" {list.Count} "; else if (list.Count > 9) return $" {list.Count} "; else return $" {list.Count} "; } public StatModifiersController(Stat stat) { this.stat = stat; } public void Add(StatModifier statModifier) { list.Add(statModifier); Calculate(); } public void Remove(StatModifier statModifier) { for (int i = 0; i < list.Count; i++) { if(statModifier.Equals(list[i])) { list.Remove(list[i]); Calculate(); return; } } } public bool HasModifier(StatModifier statModifier) { for (int i = 0; i < list.Count; i++) { if(statModifier.Equals(list[i])) return true; } return false; } public void Calculate() { if(stat == null) return; value = CalculateModifier("", stat.GetValueRaw()); minValue = CalculateModifier("min", stat.GetMinValueRaw()); maxValue = CalculateModifier("max", stat.GetMaxValueRaw()); //Debug.Log($"{stat.title} Modifiers: value={value} minValue={minValue} maxValue={maxValue}"); } private int CalculateModifier(string valueType, int statValue) { int values = 0; int percentage = 0; for (int i = 0; i < list.Count; i++) { if(list[i].endOp == "%" && list[i].valueType == valueType) { if(list[i].op == "+") percentage += list[i].value; else percentage -= list[i].value; } else if(list[i].endOp == "" && list[i].valueType == valueType) { if(list[i].op == "+") values += list[i].value; else values -= list[i].value; } } if(percentage != 0) percentage = ((statValue + values) * percentage) /100; return percentage + values; } } }