refactor: fix bugs and improve architecture

- Fix double-execution bug in LevelStateHandler (coins/skills were committed twice per level)
- Fix DamageComponent to track multiple targets via HashSet instead of single node
- Fix HealthComponent: update health immediately, decouple from PlayerController via signal wiring in PlayerController
- Remove dead loop in SkillManager.RemoveSkill; simplify O(n²) throw skill loop
- Replace GetNode<T>(path) with .Instance across managers; add Instance to StatisticsManager/SpeedRunManager
- GameManager.GetPlayer() now uses EventBus.PlayerSpawned instead of scanning all scene nodes
- UIManager.UiStack: remove [Export], use private List<Control>
- PlayerState: extract DefaultLives constant, simplify CreateDefault()
This commit is contained in:
2026-03-19 01:41:14 +01:00
parent 427bef6509
commit 321905e68e
11 changed files with 85 additions and 161 deletions

View File

@@ -1,6 +1,4 @@
using System.Threading.Tasks;
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.components;
@@ -20,27 +18,16 @@ public partial class HealthComponent : Node2D
public override void _Ready()
{
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
_floatingTextManager = FloatingTextManager.Instance;
}
public void SetHealth(float newValue)
{
_ = ApplyHealthChange(newValue);
}
public void IncreaseHealth(float delta)
{
_ = ApplyHealthChange(Health + delta);
}
public void DecreaseHealth(float delta)
{
_ = ApplyHealthChange(Health - delta);
}
public void SetHealth(float newValue) => ApplyHealthChange(newValue);
public void IncreaseHealth(float delta) => ApplyHealthChange(Health + delta);
public void DecreaseHealth(float delta) => ApplyHealthChange(Health - delta);
public float GetDelta(float newValue) => newValue - Health;
private async Task ApplyHealthChange(float newHealth, bool playSfx = true)
private void ApplyHealthChange(float newHealth, bool playSfx = true)
{
newHealth = Mathf.Clamp(newHealth, 0.0f, MaxHealth);
var delta = newHealth - Health;
@@ -53,39 +40,19 @@ public partial class HealthComponent : Node2D
else
_floatingTextManager?.ShowHeal(delta, GlobalPosition);
if (playSfx)
{
if (delta > 0f && HealSfx != null)
{
HealSfx.Play();
}
else if (delta < 0f && HurtSfx != null)
{
HurtSfx.Play();
await HurtSfx.ToSignal(HurtSfx, AudioStreamPlayer2D.SignalName.Finished);
}
}
Health = newHealth;
if (playSfx)
{
if (delta > 0f)
HealSfx?.Play();
else
HurtSfx?.Play();
}
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);
}
}
}
}