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:
@@ -23,17 +23,7 @@ public partial class LevelStateHandler : Node
|
||||
|
||||
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
|
||||
{
|
||||
var store = GameStateStore.Instance;
|
||||
if (store == null) return;
|
||||
|
||||
// Mark level complete and unlock next
|
||||
store.MarkLevelComplete(levelIndex);
|
||||
|
||||
// Commit session data to persistent state
|
||||
store.CommitSessionCoins();
|
||||
store.CommitSessionSkills();
|
||||
|
||||
// Reset session for next level
|
||||
store.ResetSession();
|
||||
// State mutations (commit coins/skills, reset session) are handled by GameManager.OnLevelComplete
|
||||
// before this event fires. This handler is reserved for future level-specific side-effects.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.Events;
|
||||
@@ -14,7 +13,7 @@ public partial class StatisticsEventHandler : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_statisticsManager = GetNode<StatisticsManager>(Constants.StatisticsManagerPath);
|
||||
_statisticsManager = StatisticsManager.Instance;
|
||||
|
||||
// Subscribe to events
|
||||
EventBus.Instance.CoinCollected += OnCoinCollected;
|
||||
|
||||
@@ -9,6 +9,8 @@ namespace Mr.BrickAdventures.scripts.State;
|
||||
/// </summary>
|
||||
public class PlayerState
|
||||
{
|
||||
private const int DefaultLives = 3;
|
||||
|
||||
/// <summary>
|
||||
/// Saved coins (not including current session).
|
||||
/// </summary>
|
||||
@@ -17,7 +19,7 @@ public class PlayerState
|
||||
/// <summary>
|
||||
/// Remaining lives.
|
||||
/// </summary>
|
||||
public int Lives { get; set; } = 3;
|
||||
public int Lives { get; set; } = DefaultLives;
|
||||
|
||||
/// <summary>
|
||||
/// Indices of completed levels.
|
||||
@@ -45,17 +47,9 @@ public class PlayerState
|
||||
public List<string> UnlockedAchievements { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fresh default player state.
|
||||
/// Creates a fresh default player state (same as <c>new PlayerState()</c>).
|
||||
/// </summary>
|
||||
public static PlayerState CreateDefault() => new()
|
||||
{
|
||||
Coins = 0,
|
||||
Lives = 3,
|
||||
CompletedLevels = new List<int>(),
|
||||
UnlockedLevels = new List<int> { 0 },
|
||||
UnlockedSkills = new List<SkillData>(),
|
||||
Statistics = new Dictionary<string, int>()
|
||||
};
|
||||
public static PlayerState CreateDefault() => new();
|
||||
|
||||
/// <summary>
|
||||
/// Resets this state to default values.
|
||||
@@ -63,7 +57,7 @@ public class PlayerState
|
||||
public void Reset()
|
||||
{
|
||||
Coins = 0;
|
||||
Lives = 3;
|
||||
Lives = DefaultLives;
|
||||
CompletedLevels.Clear();
|
||||
UnlockedLevels.Clear();
|
||||
UnlockedLevels.Add(0);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user