Refactor ChargeProgressBar and SkillManager; update skill handling and improve component interactions

This commit is contained in:
2025-08-27 00:14:41 +02:00
parent 421a31917d
commit c34b9bc16f
4 changed files with 47 additions and 17 deletions

View File

@@ -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")

View File

@@ -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<SkillData> AvailableSkills { get; set; } = [];
public Dictionary ActiveComponents { get; private set; } = new();
[Signal]
public delegate void ActiveThrowSkillChangedEventHandler(BrickThrowComponent throwComponent);
public override void _Ready()
{
_gameManager = GetNode<GameManager>("/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)

View File

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

View File

@@ -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()