SupaLidlGame/UI/BossBar.cs

80 lines
1.6 KiB
C#
Raw Normal View History

2023-07-23 11:05:01 -07:00
using Godot;
using SupaLidlGame.Characters;
using SupaLidlGame.Extensions;
2023-07-23 11:05:01 -07:00
namespace SupaLidlGame.UI;
public partial class BossBar : VBoxContainer
{
public TextureProgressBar ProgressBar { get; set; }
2023-07-23 11:05:01 -07:00
public Label BossNameLabel { get; set; }
private Events.EventBus _eventBus;
2023-07-23 11:05:01 -07:00
private Boss _boss;
public Boss Boss
{
get => _boss;
set
{
SetupBoss(value);
_boss = value;
}
}
public override void _Ready()
{
ProgressBar = GetNode<TextureProgressBar>("BarMargin/BossBar");
BossNameLabel = GetNode<Label>("LabelMargin/BossNameLabel");
_eventBus = this.GetEventBus();
_eventBus.RegisteredBoss += RegisterBoss;
_eventBus.DeregisteredBoss += DeregisterBoss;
}
private void RegisterBoss(Boss boss)
{
Boss = boss;
}
private void DeregisterBoss(Boss boss)
{
Boss = null;
2023-07-23 11:05:01 -07:00
}
2023-08-08 00:54:00 -07:00
private void OnBossHurt(Events.HurtArgs args)
2023-07-23 11:05:01 -07:00
{
ProgressBar.Value = args.NewHealth;
}
2023-08-08 00:54:00 -07:00
private void OnBossDeath(Events.HurtArgs args)
2023-07-23 11:05:01 -07:00
{
Visible = false;
Boss = null;
}
private void SetupBoss(Boss newBoss)
{
if (_boss is not null)
{
_boss.Hurt -= OnBossHurt;
_boss.Death -= OnBossDeath;
}
if (newBoss is not null)
{
newBoss.Hurt += OnBossHurt;
newBoss.Death += OnBossDeath;
2023-08-17 01:37:19 -07:00
ProgressBar.MaxValue = newBoss.MaxHealth;
2023-07-23 11:05:01 -07:00
ProgressBar.Value = newBoss.Health;
Visible = true;
}
else
{
Visible = false;
}
}
}