Files
MovementTests/scenes/ui/resourcebar/ResourceBar.cs
2026-05-02 11:19:56 +02:00

74 lines
2.1 KiB
C#

using Godot;
using System;
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Gamesmiths.Forge.Attributes;
using Gamesmiths.Forge.Core;
using Gamesmiths.Forge.Cues;
using Gamesmiths.Forge.Godot.Core;
using Gamesmiths.Forge.Tags;
[GlobalClass, Icon("res://assets/ui/IconGodotNode/control/icon_heart.png"), Meta(typeof(IAutoNode))]
public partial class ResourceBar : ProgressBar
{
public override void _Notification(int what) => this.Notify(what);
[Node("DamageCatchUp")] public required Timer DamageCatchUp { get; set; }
[Node("Damagebar")] public required ProgressBar DamageBar { get; set; }
[Export] public StyleBox? BarStyle;
private float _currentValue;
public float CurrentValue
{
get => _currentValue;
set => SetValue(value);
}
public void OnReady()
{
DamageCatchUp.Timeout += OnDamageCatchUp;
Visible = false;
if (BarStyle != null)
AddThemeStyleboxOverride("fill", BarStyle);
}
public void Init(EntityAttribute attribute)
{
_currentValue = attribute.BaseValue;
// Should be Max but it's bugged
// Therefore we just assume that the base value is the max value, i.e. entities spawn at full health
MaxValue = attribute.BaseValue; // MaxValue = attribute.Max;
Value = attribute.BaseValue;
DamageBar.MaxValue = attribute.Max;
DamageBar.Value = attribute.BaseValue;
attribute.OnValueChanged += (entityAttribute, i) => CurrentValue = entityAttribute.CurrentValue;
}
public void SetValue(float newValue)
{
var previousValue = _currentValue;
_currentValue = Mathf.Min(newValue, (float) MaxValue);
_currentValue = newValue;
Value = _currentValue;
Visible = _currentValue < MaxValue;
if (_currentValue < previousValue)
{
DamageCatchUp.Start();
}
else
{
DamageBar.Value = _currentValue;
}
}
public void OnDamageCatchUp()
{
DamageBar.Value = _currentValue;
}
}