using RPGCore.Core.Objects; using RPGCore.Stats.ObjectModules.StatsObjectModule.Events; using RPGCoreCommon.DynamicValues; namespace RPGCore.Stats.ObjectModules.StatsObjectModule { /// /// You should never instantiate this by yourself! /// Instantiated automatically by for every available . /// public sealed class StatValue { public readonly StatValueDefinitionSO definition; public readonly BaseObject obj; public readonly DynamicValueSourceContext sources = new(); public readonly StatModifierContext modifiers = new(); /// /// Base value defined in attached to . /// Formula result will be added to this base value. /// Default base value is ZERO. /// public int baseValue { get; private set; } /// /// This value is calculated by in /// public int value { get; private set; } internal StatValue(StatValueDefinitionSO definition, int baseValue, BaseObject obj) { this.definition = definition; this.obj = obj; this.baseValue = baseValue; } /// /// Refreshes , by calculating formula again. /// internal void Refresh() { var previous = value; value = definition.dynamicValue.GetValue(sources); if (previous == value) return; obj.events.Invoke(new StatValueChangeEvent { after = value, before = previous, valueDefinition = definition, target = obj }); } /// /// Sets new and then do to recalculate whole /// /// New base value public void SetBase(int amount) { if (baseValue == amount) return; baseValue = amount; Refresh(); } } }