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

@@ -11,7 +11,7 @@ public partial class DamageComponent : Node
[Export] public StatusEffectDataResource StatusEffectData { get; set; }
[Export] public Timer DamageTimer { get; set; }
private Node _currentTarget = null;
private readonly System.Collections.Generic.HashSet<Node> _currentTargets = new();
private KnockbackComponent _knockbackComponent = null;
[Signal] public delegate void EffectInflictedEventHandler(Node2D target, StatusEffectDataResource effect);
@@ -35,10 +35,11 @@ public partial class DamageComponent : Node
public override void _Process(double delta)
{
if (_currentTarget == null) return;
if (_currentTargets.Count == 0) return;
if (DamageTimer != null) return;
ProcessEntityAndApplyDamage(_currentTarget as Node2D);
foreach (var target in _currentTargets)
ProcessEntityAndApplyDamage(target as Node2D);
}
public void DealDamage(HealthComponent target) => target.DecreaseHealth(Damage);
@@ -56,28 +57,28 @@ public partial class DamageComponent : Node
private void OnAreaBodyExited(Node2D body)
{
if (body != _currentTarget) return;
_currentTarget = null;
DamageTimer?.Stop();
_currentTargets.Remove(body);
if (_currentTargets.Count == 0)
DamageTimer?.Stop();
}
private void OnAreaBodyEntered(Node2D body)
{
_currentTarget = body;
_currentTargets.Add(body);
if (!CheckIfProcessingIsOn())
return;
DamageTimer?.Start();
if (_currentTargets.Count == 1)
DamageTimer?.Start();
ProcessEntityAndApplyDamage(body);
}
private void OnDamageTimerTimeout()
{
if (_currentTarget == null) return;
ProcessEntityAndApplyDamage(_currentTarget as Node2D);
foreach (var target in _currentTargets)
ProcessEntityAndApplyDamage(target as Node2D);
}
private void ProcessEntityAndApplyDamage(Node2D body)

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);
}
}
}
}

View File

@@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.components;
@@ -34,8 +33,19 @@ public partial class PlayerController : CharacterBody2D
public override void _Ready()
{
var skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
skillManager?.RegisterPlayer(this);
SkillManager.Instance?.RegisterPlayer(this);
// Wire HealthComponent signals to global EventBus events
var health = GetNodeOrNull<HealthComponent>("HealthComponent");
if (health != null)
{
health.Death += () => EventBus.EmitPlayerDied(GlobalPosition);
health.HealthChanged += (delta, total) =>
{
if (delta < 0f) EventBus.EmitPlayerDamaged(Mathf.Abs(delta), total, GlobalPosition);
else EventBus.EmitPlayerHealed(delta, total, GlobalPosition);
};
}
_inputHandler = GetNode<PlayerInputHandler>("PlayerInputHandler");
foreach (var child in MovementAbilitiesContainer.GetChildren())
@@ -49,6 +59,7 @@ public partial class PlayerController : CharacterBody2D
_ = ConnectJumpAndGravityAbilities();
EmitSignalMovementAbilitiesChanged();
EventBus.EmitPlayerSpawned(this);
}
public override void _PhysicsProcess(double delta)