From c34b9bc16fc943846c5dc47a882aaaf732de381f Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Wed, 27 Aug 2025 00:14:41 +0200 Subject: [PATCH] Refactor ChargeProgressBar and SkillManager; update skill handling and improve component interactions --- objects/entities/brick_player.tscn | 3 +- scripts/SkillManager.cs | 16 ++++++++- scripts/UI/ChargeProgressBar.cs | 40 +++++++++++++++-------- scripts/components/BrickThrowComponent.cs | 5 ++- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/objects/entities/brick_player.tscn b/objects/entities/brick_player.tscn index 35a3d35..409fa56 100644 --- a/objects/entities/brick_player.tscn +++ b/objects/entities/brick_player.tscn @@ -247,11 +247,12 @@ bus = &"sfx" stream = ExtResource("32_x2b7c") bus = &"sfx" -[node name="ChargingBarLayer" parent="." instance=ExtResource("28_3f5nm")] +[node name="ChargingBarLayer" parent="." node_paths=PackedStringArray("_skillManager") instance=ExtResource("28_3f5nm")] offset_left = -17.0 offset_top = -30.0 offset_right = 23.0 offset_bottom = -20.0 +_skillManager = NodePath("../SkillManager") [node name="HitParticles" parent="." instance=ExtResource("28_jh5m0")] process_material = SubResource("ParticleProcessMaterial_lgb3u") diff --git a/scripts/SkillManager.cs b/scripts/SkillManager.cs index a417d26..a108a99 100644 --- a/scripts/SkillManager.cs +++ b/scripts/SkillManager.cs @@ -1,6 +1,7 @@ using Godot; using Godot.Collections; using Mr.BrickAdventures.Autoloads; +using Mr.BrickAdventures.scripts.components; using Mr.BrickAdventures.scripts.interfaces; using Mr.BrickAdventures.scripts.Resources; @@ -10,9 +11,12 @@ public partial class SkillManager : Node { private GameManager _gameManager; [Export] public Array AvailableSkills { get; set; } = []; - + public Dictionary ActiveComponents { get; private set; } = new(); + [Signal] + public delegate void ActiveThrowSkillChangedEventHandler(BrickThrowComponent throwComponent); + public override void _Ready() { _gameManager = GetNode("/root/GameManager"); @@ -58,12 +62,22 @@ public partial class SkillManager : Node Owner.AddChild(instance); ActiveComponents[skillData.Name] = instance; + + if (instance is BrickThrowComponent btc) + { + EmitSignalActiveThrowSkillChanged(btc); + } } public void RemoveSkill(string skillName) { if (!ActiveComponents.TryGetValue(skillName, out var component)) return; + + if (component.AsGodotObject() is BrickThrowComponent) + { + EmitSignalActiveThrowSkillChanged(null); + } var inst = (Node)component; if (inst is ISkill skill) diff --git a/scripts/UI/ChargeProgressBar.cs b/scripts/UI/ChargeProgressBar.cs index 6b3eedc..aab9776 100644 --- a/scripts/UI/ChargeProgressBar.cs +++ b/scripts/UI/ChargeProgressBar.cs @@ -1,4 +1,5 @@ using Godot; +using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.scripts.components; using Mr.BrickAdventures.scripts.Resources; @@ -7,25 +8,36 @@ namespace Mr.BrickAdventures.scripts.UI; public partial class ChargeProgressBar : ProgressBar { [Export] public ProgressBar ProgressBar { get; set; } - [Export] public BrickThrowComponent ThrowComponent { get; set; } - + [Export] private SkillManager _skillManager; + + private BrickThrowComponent _throwComponent; private ChargeThrowInputResource _throwInput; public override void _Ready() { - Owner.ChildEnteredTree += OnNodeEntered; - Owner.TreeExiting += OnOwnerExiting; - ProgressBar.Hide(); + + if (_skillManager == null) + { + return; + } + + _skillManager.ActiveThrowSkillChanged += OnActiveThrowSkillChanged; + + SetupDependencies(); + } + + private void OnActiveThrowSkillChanged(BrickThrowComponent throwComponent) + { + OnOwnerExiting(); + + if (throwComponent == null) return; + + _throwComponent = throwComponent; + _throwComponent.TreeExiting += OnOwnerExiting; SetupDependencies(); } - private void OnNodeEntered(Node node) - { - if (node is not BrickThrowComponent throwComponent || ThrowComponent != null) return; - ThrowComponent = throwComponent; - SetupDependencies(); - } private void OnOwnerExiting() { if (_throwInput != null) @@ -35,18 +47,18 @@ public partial class ChargeProgressBar : ProgressBar _throwInput.ChargeUpdated -= OnChargeUpdated; _throwInput = null; } - ThrowComponent = null; + _throwComponent = null; } private void SetupDependencies() { - if (ThrowComponent == null || ProgressBar == null) + if (_throwComponent == null || ProgressBar == null) { return; } - if (ThrowComponent.ThrowInputBehavior is ChargeThrowInputResource throwInput) + if (_throwComponent.ThrowInputBehavior is ChargeThrowInputResource throwInput) { _throwInput = throwInput; } diff --git a/scripts/components/BrickThrowComponent.cs b/scripts/components/BrickThrowComponent.cs index 180c73f..df5189b 100644 --- a/scripts/components/BrickThrowComponent.cs +++ b/scripts/components/BrickThrowComponent.cs @@ -86,7 +86,9 @@ public partial class BrickThrowComponent : Node, ISkill { PlayerController = owner as PlayerController; _skillData = data; - + + ThrowInputBehavior = (ThrowInputResource)ThrowInputBehavior?.Duplicate(); + if (PlayerController == null) { GD.PushError("BrickThrowComponent: Owner is not a PlayerController."); @@ -101,6 +103,7 @@ public partial class BrickThrowComponent : Node, ISkill public void Activate() { if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested += ThrowBrick; + SetProcessInput(true); } public void Deactivate()