Files
TheVVaS-Assets/RPGCore.Stats/Runtime/ObjectModules/StatsObjectModule/StatValue.cs
T

65 lines
2.3 KiB
C#

using RPGCore.Core.Objects;
using RPGCore.Stats.ObjectModules.StatsObjectModule.Events;
using RPGCoreCommon.DynamicValues;
namespace RPGCore.Stats.ObjectModules.StatsObjectModule
{
/// <summary>
/// <b>You should never instantiate this by yourself!</b>
/// Instantiated automatically by <see cref="StatsModule"/> for every available <see cref="StatValueDefinitionSO"/>.
/// </summary>
public sealed class StatValue
{
public readonly StatValueDefinitionSO definition;
public readonly BaseObject obj;
public readonly DynamicValueSourceContext sources = new();
public readonly StatModifierContext modifiers = new();
/// <summary>
/// Base value defined in <see cref="StatsModule"/> attached to <see cref="BaseObject"/>.
/// Formula result will be added to this base value.
/// Default base value is ZERO.
/// </summary>
public int baseValue { get; private set; }
/// <summary>
/// This value is calculated by <see cref="DynamicValue"/> in <see cref="StatValueDefinitionSO"/>
/// </summary>
public int value { get; private set; }
internal StatValue(StatValueDefinitionSO definition, int baseValue, BaseObject obj)
{
this.definition = definition;
this.obj = obj;
this.baseValue = baseValue;
}
/// <summary>
/// Refreshes <see cref="value"/>, by calculating formula again.
/// </summary>
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
});
}
/// <summary>
/// Sets new <see cref="baseValue"/> and then do <see cref="Refresh"/> to recalculate whole <see cref="value"/>
/// </summary>
/// <param name="amount">New base value</param>
public void SetBase(int amount)
{
if (baseValue == amount) return;
baseValue = amount;
Refresh();
}
}
}