64 lines
2.5 KiB
C#
64 lines
2.5 KiB
C#
using RPGCore.Stats.ObjectModules.StatsObjectModule.Events;
|
|
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="StatResourceDefinitionSO"/>.
|
|
/// </summary>
|
|
public sealed class StatResource
|
|
{
|
|
public StatResourceDefinitionSO definition { get; internal set; }
|
|
public StatValue statValue { get; internal set; }
|
|
|
|
/// <summary>
|
|
/// This value represent current resource of this stat. Always between <see cref="StatValue.baseValue"/> and <see cref="StatValue.value"/>
|
|
/// </summary>
|
|
public int resource { get; private set; }
|
|
|
|
internal StatResource(StatResourceDefinitionSO definition, int defaultValue, StatValue statValue)
|
|
{
|
|
this.definition = definition;
|
|
resource = defaultValue;
|
|
this.statValue = statValue;
|
|
}
|
|
|
|
/// <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="StatValue.value"/>.
|
|
/// </summary>
|
|
/// <param name="amount">New amount</param>
|
|
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
|
|
});
|
|
}
|
|
|
|
/// <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) * statValue.value));
|
|
}
|
|
}
|
|
} |