Files
2026-04-25 23:37:10 +02:00

109 lines
4.2 KiB
C#

using RPGCore.Core.Objects;
using RPGCore.ObjectModules.EventObjectModule;
using RPGCore.Stats.ObjectModules.StatsObjectModule.Events;
using RPGCoreCommon.DynamicValues;
using UnityEngine;
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="StatDefinitionSO"/>.
/// </summary>
public sealed class StatValue
{
public readonly StatDefinitionSO definition;
private readonly BaseObject _parent;
public readonly DynamicValueSourceContext sourceContext = 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="StatDefinitionSO"/> and can have two meanings:<br/>
/// 1. If <see cref="StatType.Value"/> - just simple value stat<br/>
/// 2. If <see cref="StatType.Resource"/> - this is maximum value for that resource
/// </summary>
public int value { get; private set; }
/// <summary>
/// Usable only when <see cref="StatDefinitionSO"/>.<see cref="StatType"/> is <see cref="StatType.Resource"/>.<br/>
/// This value represent current resource of this stat. Always between <see cref="baseValue"/> and <see cref="value"/>
/// </summary>
public int resource { get; private set; }
internal StatValue(StatDefinitionSO definition, int baseValue, BaseObject parent)
{
this.definition = definition;
_parent = parent;
this.baseValue = baseValue;
}
/// <summary>
/// Refreshes <see cref="value"/>, by calculating formula again.
/// </summary>
internal void Refresh()
{
var previous = value;
value = baseValue + definition.dynamicValue.GetValue(sourceContext);
_parent.events.Invoke(new StatValueChangeEvent
{
after = value,
before = previous,
statDefinition = definition,
target = _parent
});
}
/// <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)
{
baseValue = amount;
Refresh();
}
/// <summary>
/// Changes (or simpler - uses) <see cref="resource"/> of this stat value by given amount.
/// It just calls <see cref="SetResource(int)"/> with: <see cref="resource"/> + <see cref="amount"/>
/// </summary>
/// <param name="amount">Amount that will be added or subtracted from <see cref="resource"/></param>
public void ChangeResource(int amount)
{
SetResource(resource + amount);
}
/// <summary>
/// Sets <see cref="resource"/> to given amount. Amount will be clamped between ZERO and <see cref="value"/>.
/// </summary>
/// <param name="amount">New amount</param>
public void SetResource(int amount)
{
var old = resource;
resource = Mathf.Clamp(amount, 0, value);
_parent.events.Invoke(new StatResourceChangeEvent
{
delta = resource - old,
after = resource,
before = old,
statDefinition = definition,
target = _parent
});
}
/// <summary>
/// Sets <see cref="resource"/> to given percentage. Percentage will be clamped between ZERO and ONE.
/// </summary>
/// <param name="percentage">New percentage</param>
public void SetResource(float percentage)
{
SetResource(Mathf.CeilToInt(Mathf.Clamp01(percentage) * value));
}
}
}