using RPGCore.Stats.ObjectModules.StatsObjectModule.Events;
using UnityEngine;
namespace RPGCore.Stats.ObjectModules.StatsObjectModule
{
///
/// You should never instantiate this by yourself!
/// Instantiated automatically by for every available .
///
public sealed class StatResource
{
public StatResourceDefinitionSO definition { get; internal set; }
public StatValue statValue { get; internal set; }
///
/// This value represent current resource of this stat. Always between and
///
public int resource { get; private set; }
internal StatResource(StatResourceDefinitionSO definition, int defaultValue, StatValue statValue)
{
this.definition = definition;
resource = defaultValue;
this.statValue = statValue;
}
///
/// Changes (or simpler - uses) of this stat value by given amount.
/// It just calls with: +
///
/// Amount that will be added or subtracted from
public void ChangeResource(int amount)
{
SetResource(resource + amount);
}
///
/// Sets to given amount. Amount will be clamped between ZERO and .
///
/// New amount
public void SetResource(int amount)
{
var old = resource;
resource = Mathf.Clamp(amount, 0, statValue.value);
statValue.obj.events.Invoke(new StatResourceChangeEvent
{
delta = resource - old,
after = resource,
before = old,
resourceDefinition = definition,
target = statValue.obj
});
}
///
/// Sets to given percentage. Percentage will be clamped between ZERO and ONE.
///
/// New percentage
public void SetResource(float percentage)
{
SetResource(Mathf.CeilToInt(Mathf.Clamp01(percentage) * statValue.value));
}
}
}