refactor: Replace hardcoded node paths with constants and remove ScoreComponent.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -9,7 +10,7 @@ namespace Mr.BrickAdventures.scripts.components;
|
||||
public partial class BrickShieldSkillComponent : Node, ISkill
|
||||
{
|
||||
[Export] public PackedScene ShieldScene { get; set; }
|
||||
|
||||
|
||||
private PlayerController _player;
|
||||
private Node2D _shieldInstance;
|
||||
private SkillData _skillData;
|
||||
@@ -19,8 +20,8 @@ public partial class BrickShieldSkillComponent : Node, ISkill
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_skillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_skillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
}
|
||||
|
||||
public void Initialize(Node owner, SkillData data)
|
||||
@@ -59,7 +60,7 @@ public partial class BrickShieldSkillComponent : Node, ISkill
|
||||
_shieldHealth.MaxHealth = (float)newHealth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void OnShieldDestroyed()
|
||||
{
|
||||
if (_gameManager != null && _skillData != null && _skillManager != null)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
|
||||
@@ -12,25 +13,25 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
[Export] public AudioStreamPlayer2D OpenDoorSfx { get; set; }
|
||||
[Export] public int OpenedDoorFrame { get; set; } = 0;
|
||||
[Export] public string AchievementId = "level_complete_1";
|
||||
|
||||
|
||||
[Signal] public delegate void ExitTriggeredEventHandler();
|
||||
|
||||
|
||||
private GameManager _gameManager;
|
||||
private AchievementManager _achievementManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_achievementManager = GetNode<AchievementManager>("/root/AchievementManager");
|
||||
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_achievementManager = GetNode<AchievementManager>(Constants.AchievementManagerPath);
|
||||
|
||||
BodyEntered += OnExitAreaBodyEntered;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void OnExitAreaBodyEntered(Node2D body)
|
||||
{
|
||||
if (Locked) return;
|
||||
|
||||
|
||||
EmitSignalExitTriggered();
|
||||
_achievementManager.UnlockAchievement(AchievementId);
|
||||
_gameManager.UnlockLevel((int)_gameManager.PlayerState["current_level"] + 1);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -15,25 +16,25 @@ public partial class LeverComponent : Node
|
||||
|
||||
[Signal]
|
||||
public delegate void ActivatedEventHandler();
|
||||
|
||||
|
||||
private FloatingTextManager _floatingTextManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
|
||||
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
|
||||
if (Area == null)
|
||||
{
|
||||
GD.PushError("LeverComponent: Area is not set.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (Sprite == null)
|
||||
{
|
||||
GD.PushError("LeverComponent: Sprite is not set.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Area.BodyEntered += OnBodyEntered;
|
||||
Area.AreaEntered += OnAreaEntered;
|
||||
}
|
||||
@@ -58,7 +59,7 @@ public partial class LeverComponent : Node
|
||||
await timer.ToSignal(timer, Timer.SignalName.Timeout);
|
||||
Sprite.Frame = StartAnimationIndex;
|
||||
}
|
||||
|
||||
|
||||
private void HandleTriggerLogic(Node2D obj)
|
||||
{
|
||||
var triggerLever = obj.GetNodeOrNull<TriggerLeverComponent>("TriggerLeverComponent");
|
||||
|
||||
@@ -2,6 +2,7 @@ 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;
|
||||
@@ -10,7 +11,7 @@ namespace Mr.BrickAdventures.scripts.components;
|
||||
public partial class PlayerController : CharacterBody2D
|
||||
{
|
||||
[Export] private Node MovementAbilitiesContainer { get; set; }
|
||||
|
||||
|
||||
[ExportGroup("Movement Ability Scenes")]
|
||||
[Export] public PackedScene GroundMovementScene { get; set; }
|
||||
[Export] public PackedScene JumpMovementScene { get; set; }
|
||||
@@ -19,23 +20,23 @@ public partial class PlayerController : CharacterBody2D
|
||||
[Export] public PackedScene SpaceshipMovementScene { get; set; }
|
||||
[Export] public PackedScene WallJumpScene { get; set; }
|
||||
[Export] public PackedScene GridMovementScene { get; set; }
|
||||
|
||||
|
||||
[Signal] public delegate void JumpInitiatedEventHandler();
|
||||
[Signal] public delegate void MovementAbilitiesChangedEventHandler();
|
||||
|
||||
|
||||
public Vector2 LastDirection { get; private set; } = Vector2.Right;
|
||||
public Vector2 PreviousVelocity { get; private set; } = Vector2.Zero;
|
||||
|
||||
|
||||
private List<MovementAbility> _abilities = [];
|
||||
private PlayerInputHandler _inputHandler;
|
||||
|
||||
|
||||
public IReadOnlyList<MovementAbility> GetActiveAbilities() => _abilities;
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var skillManager = GetNodeOrNull<SkillManager>("/root/SkillManager");
|
||||
var skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
|
||||
skillManager?.RegisterPlayer(this);
|
||||
|
||||
|
||||
_inputHandler = GetNode<PlayerInputHandler>("PlayerInputHandler");
|
||||
foreach (var child in MovementAbilitiesContainer.GetChildren())
|
||||
{
|
||||
@@ -45,20 +46,20 @@ public partial class PlayerController : CharacterBody2D
|
||||
_abilities.Add(ability);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_ = ConnectJumpAndGravityAbilities();
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var velocity = Velocity;
|
||||
|
||||
|
||||
foreach (var ability in _abilities)
|
||||
{
|
||||
velocity = ability.ProcessMovement(velocity, delta);
|
||||
}
|
||||
|
||||
|
||||
if (_inputHandler.MoveDirection.X != 0)
|
||||
{
|
||||
LastDirection = new Vector2(_inputHandler.MoveDirection.X > 0 ? 1 : -1, 0);
|
||||
@@ -68,14 +69,14 @@ public partial class PlayerController : CharacterBody2D
|
||||
Velocity = velocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
|
||||
public void AddAbility(MovementAbility ability)
|
||||
{
|
||||
MovementAbilitiesContainer.AddChild(ability);
|
||||
ability.Initialize(this);
|
||||
_abilities.Add(ability);
|
||||
}
|
||||
|
||||
|
||||
public void ClearMovementAbilities()
|
||||
{
|
||||
foreach (var ability in _abilities)
|
||||
@@ -84,7 +85,7 @@ public partial class PlayerController : CharacterBody2D
|
||||
}
|
||||
_abilities.Clear();
|
||||
}
|
||||
|
||||
|
||||
public void RemoveAbility<T>() where T : MovementAbility
|
||||
{
|
||||
for (var i = _abilities.Count - 1; i >= 0; i--)
|
||||
@@ -102,20 +103,20 @@ public partial class PlayerController : CharacterBody2D
|
||||
public void SetPlatformMovement()
|
||||
{
|
||||
ClearMovementAbilities();
|
||||
|
||||
|
||||
if (GroundMovementScene != null) AddAbility(GroundMovementScene.Instantiate<MovementAbility>());
|
||||
if (JumpMovementScene != null) AddAbility(JumpMovementScene.Instantiate<MovementAbility>());
|
||||
if (GravityScene != null) AddAbility(GravityScene.Instantiate<MovementAbility>());
|
||||
if (OneWayPlatformScene != null) AddAbility(OneWayPlatformScene.Instantiate<MovementAbility>());
|
||||
|
||||
|
||||
_ = ConnectJumpAndGravityAbilities();
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
|
||||
public void SetSpaceshipMovement()
|
||||
{
|
||||
ClearMovementAbilities();
|
||||
|
||||
|
||||
if (SpaceshipMovementScene != null) AddAbility(SpaceshipMovementScene.Instantiate<MovementAbility>());
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
@@ -130,10 +131,10 @@ public partial class PlayerController : CharacterBody2D
|
||||
private async Task ConnectJumpAndGravityAbilities()
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
|
||||
|
||||
var jumpAbility = _abilities.OfType<VariableJumpAbility>().FirstOrDefault();
|
||||
var gravityAbility = _abilities.OfType<GravityAbility>().FirstOrDefault();
|
||||
|
||||
|
||||
if (jumpAbility != null && gravityAbility != null)
|
||||
{
|
||||
gravityAbility.AscendGravity = jumpAbility.AscendGravity;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -10,12 +11,12 @@ public partial class PlayerDeathComponent : Node2D
|
||||
[Export] public PackedScene DeathEffect { get; set; }
|
||||
[Export] public HealthComponent HealthComponent { get; set; }
|
||||
[Export] public Vector2 EffectScale { get; set; } = new Vector2(1.5f, 1.5f);
|
||||
|
||||
|
||||
private GameManager _gameManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
HealthComponent.Death += OnDeath;
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ public partial class PlayerDeathComponent : Node2D
|
||||
effect.GlobalPosition = GlobalPosition;
|
||||
effect.Scale = EffectScale;
|
||||
}
|
||||
|
||||
|
||||
_gameManager.RemoveLives(1);
|
||||
_gameManager.ResetCurrentSessionState();
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ScoreComponent : Node
|
||||
{
|
||||
private GameManager _gameManager;
|
||||
private const string CoinsGroupName = "coins";
|
||||
|
||||
public override async void _Ready()
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
if (_gameManager == null)
|
||||
{
|
||||
GD.PrintErr("GameManager not found in the scene tree.");
|
||||
return;
|
||||
}
|
||||
|
||||
var coins = GetTree().GetNodesInGroup(CoinsGroupName);
|
||||
foreach (var coin in coins)
|
||||
{
|
||||
var c = coin.GetNodeOrNull<CollectableComponent>("CollectableComponent");
|
||||
if (c != null)
|
||||
{
|
||||
c.Collected += OnCollected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollected(float amount, CollectableType type, Node2D body)
|
||||
{
|
||||
if (type != CollectableType.Coin) return;
|
||||
|
||||
var coinAmount = (int)amount;
|
||||
var currentCoins = (int)_gameManager.CurrentSessionState["coins_collected"];
|
||||
_gameManager.CurrentSessionState["coins_collected"] = currentCoins + coinAmount;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://ccqb8kd5m0eh7
|
||||
@@ -1,5 +1,6 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -18,8 +19,8 @@ public partial class SkillUnlockerComponent : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
SkillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
SkillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
}
|
||||
|
||||
private bool HasEnoughCoins(int amount)
|
||||
|
||||
Reference in New Issue
Block a user