first commit
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.Core
|
||||
{
|
||||
public interface IDamageable
|
||||
{
|
||||
Transform Transform { get; }
|
||||
bool IsAlive { get; }
|
||||
void TakeDamage(float amount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3f3e434b30daf3198c7069ec1b9dc34
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.Core
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class ResourceStat
|
||||
{
|
||||
[SerializeField] private float maxValue = 100f;
|
||||
[SerializeField] private float currentValue = 100f;
|
||||
|
||||
public event Action<float, float> Changed;
|
||||
|
||||
public float Current => currentValue;
|
||||
public float Max => maxValue;
|
||||
public float Normalized => maxValue <= 0f ? 0f : currentValue / maxValue;
|
||||
|
||||
public ResourceStat(float maxValue)
|
||||
{
|
||||
this.maxValue = Mathf.Max(1f, maxValue);
|
||||
currentValue = this.maxValue;
|
||||
}
|
||||
|
||||
public void Initialize(float value)
|
||||
{
|
||||
maxValue = Mathf.Max(1f, value);
|
||||
currentValue = maxValue;
|
||||
Changed?.Invoke(currentValue, maxValue);
|
||||
}
|
||||
|
||||
public bool TrySpend(float amount)
|
||||
{
|
||||
if (amount <= 0f)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (currentValue < amount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SetCurrent(currentValue - amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Add(float amount)
|
||||
{
|
||||
if (amount <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SetCurrent(currentValue + amount);
|
||||
}
|
||||
|
||||
public void Subtract(float amount)
|
||||
{
|
||||
if (amount <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SetCurrent(currentValue - amount);
|
||||
}
|
||||
|
||||
private void SetCurrent(float value)
|
||||
{
|
||||
currentValue = Mathf.Clamp(value, 0f, maxValue);
|
||||
Changed?.Invoke(currentValue, maxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b3c84173aeb6f4b08a1a9e547f70a17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user