feat: Implement a comprehensive global event bus with new event handlers and integrate player health and collection events.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -11,22 +12,22 @@ public partial class HealthComponent : Node2D
|
||||
[Export] public float MaxHealth { get; set; } = 1.0f;
|
||||
[Export] public AudioStreamPlayer2D HurtSfx { get; set; }
|
||||
[Export] public AudioStreamPlayer2D HealSfx { get; set; }
|
||||
|
||||
|
||||
[Signal] public delegate void HealthChangedEventHandler(float delta, float totalHealth);
|
||||
[Signal] public delegate void DeathEventHandler();
|
||||
|
||||
|
||||
private FloatingTextManager _floatingTextManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
}
|
||||
|
||||
|
||||
public void SetHealth(float newValue)
|
||||
{
|
||||
_ = ApplyHealthChange(newValue);
|
||||
}
|
||||
|
||||
|
||||
public void IncreaseHealth(float delta)
|
||||
{
|
||||
_ = ApplyHealthChange(Health + delta);
|
||||
@@ -46,7 +47,7 @@ public partial class HealthComponent : Node2D
|
||||
|
||||
if (delta == 0.0f)
|
||||
return;
|
||||
|
||||
|
||||
if (delta < 0.0f)
|
||||
_floatingTextManager?.ShowDamage(Mathf.Abs(delta), GlobalPosition);
|
||||
else
|
||||
@@ -64,16 +65,27 @@ public partial class HealthComponent : Node2D
|
||||
await HurtSfx.ToSignal(HurtSfx, AudioStreamPlayer2D.SignalName.Finished);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Health = newHealth;
|
||||
|
||||
if (Health <= 0f)
|
||||
{
|
||||
EmitSignalDeath();
|
||||
// Emit global event if this is the player
|
||||
if (Owner is PlayerController)
|
||||
EventBus.EmitPlayerDied(GlobalPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalHealthChanged(delta, Health);
|
||||
// Emit global events if this is the player
|
||||
if (Owner is PlayerController)
|
||||
{
|
||||
if (delta < 0f)
|
||||
EventBus.EmitPlayerDamaged(Mathf.Abs(delta), Health, GlobalPosition);
|
||||
else
|
||||
EventBus.EmitPlayerHealed(delta, Health, GlobalPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user