50 lines
847 B
C#
50 lines
847 B
C#
using Godot;
|
|
|
|
namespace Slimepire.Gameplay.Components;
|
|
|
|
[GlobalClass]
|
|
public partial class EnergyStorage : Node
|
|
{
|
|
[Export]
|
|
public Label EnergyLabel = null!;
|
|
|
|
[Export]
|
|
public float MaxEnergy = 100;
|
|
|
|
public float Energy
|
|
{
|
|
get;
|
|
set
|
|
{
|
|
field = Mathf.Clamp(field + value, 0, MaxEnergy);
|
|
UpdateLabel();
|
|
}
|
|
}
|
|
|
|
public bool TryConsume(float amount)
|
|
{
|
|
if (Energy >= amount)
|
|
{
|
|
Energy = -amount;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
UpdateLabel();
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
// EnergyManager.Instance.RemoveEnergyStorage(this);
|
|
}
|
|
|
|
private void UpdateLabel()
|
|
{
|
|
EnergyLabel.Text = $"{Energy:0}/{MaxEnergy}";
|
|
}
|
|
}
|