Refactor ChargeProgressBar and SkillManager; update skill handling and improve component interactions
This commit is contained in:
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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()
|
||||
|
Reference in New Issue
Block a user