Add SkillManager class and integrate with PlayerController and SkillUnlockerComponent (#10)

This commit is contained in:
2025-08-31 13:04:50 +02:00
committed by GitHub
parent 2d520a708f
commit bd40c797d4
11 changed files with 116 additions and 32 deletions

View File

@@ -1,152 +0,0 @@
using Godot;
using Godot.Collections;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.components;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts;
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");
ApplyUnlockedSkills();
}
public void AddSkill(SkillData skillData)
{
if (ActiveComponents.ContainsKey(skillData.Name))
return;
if (skillData.Type == SkillType.Throw)
{
var unlocked = _gameManager.GetUnlockedSkills();
foreach (var sd in unlocked)
{
SkillData data = null;
foreach (var s in AvailableSkills)
{
if (s == sd)
{
data = s;
break;
}
}
if (data is { Type: SkillType.Throw })
RemoveSkill(data.Name);
}
}
var instance = skillData.Node.Instantiate();
if (instance is ISkill skill)
{
skill.Initialize(Owner, skillData);
skill.Activate();
}
else
{
GD.PrintErr($"Skill scene for '{skillData.Name}' does not implement ISkill!");
instance.QueueFree();
return;
}
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)
{
skill.Deactivate();
}
if (IsInstanceValid(inst))
inst.QueueFree();
var skills = _gameManager.GetUnlockedSkills();
foreach (SkillData s in skills)
{
if (s.Name == skillName)
{
s.IsActive = false;
break;
}
}
ActiveComponents.Remove(skillName);
}
public void ApplyUnlockedSkills()
{
foreach (var sd in AvailableSkills)
{
if (_gameManager.IsSkillUnlocked(sd))
{
CallDeferred(MethodName.AddSkill, sd);
}
else
{
RemoveSkill(sd.Name);
}
}
}
public SkillData GetSkillByName(string skillName)
{
foreach (var sd in AvailableSkills)
if (sd.Name == skillName) return sd;
return null;
}
public void ActivateSkill(SkillData skill)
{
if (!ActiveComponents.ContainsKey(skill.Name))
{
AddSkill(skill);
skill.IsActive = true;
}
}
public void DeactivateSkill(SkillData skill)
{
if (ActiveComponents.ContainsKey(skill.Name))
{
RemoveSkill(skill.Name);
skill.IsActive = false;
}
}
public void ToggleSkillActivation(SkillData skill)
{
if (skill == null) return;
if (ActiveComponents.ContainsKey(skill.Name))
DeactivateSkill(skill);
else
ActivateSkill(skill);
}
}

View File

@@ -1 +0,0 @@
uid://di572axt0c3s8

View File

@@ -8,8 +8,8 @@ namespace Mr.BrickAdventures.scripts.UI;
public partial class ChargeProgressBar : ProgressBar
{
[Export] public ProgressBar ProgressBar { get; set; }
[Export] private SkillManager _skillManager;
private SkillManager _skillManager;
private BrickThrowComponent _throwComponent;
private ChargeThrowInputResource _throwInput;
@@ -17,8 +17,10 @@ public partial class ChargeProgressBar : ProgressBar
{
ProgressBar.Hide();
_skillManager = GetNodeOrNull<SkillManager>("/root/SkillManager");
if (_skillManager == null)
{
GD.PrintErr("ChargeProgressBar: SkillManager autoload not found.");
return;
}
@@ -26,12 +28,21 @@ public partial class ChargeProgressBar : ProgressBar
SetupDependencies();
}
public override void _ExitTree()
{
if (_skillManager != null && IsInstanceValid(_skillManager))
{
_skillManager.ActiveThrowSkillChanged -= OnActiveThrowSkillChanged;
}
OnOwnerExiting();
}
private void OnActiveThrowSkillChanged(BrickThrowComponent throwComponent)
{
OnOwnerExiting();
if (throwComponent == null) return;
if (throwComponent == null || !IsInstanceValid(throwComponent)) return;
_throwComponent = throwComponent;
_throwComponent.TreeExiting += OnOwnerExiting;

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Godot;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.components;
@@ -31,6 +32,9 @@ public partial class PlayerController : CharacterBody2D
public override void _Ready()
{
var skillManager = GetNodeOrNull<SkillManager>("/root/SkillManager");
skillManager?.RegisterPlayer(this);
_inputHandler = GetNode<PlayerInputHandler>("PlayerInputHandler");
foreach (var child in MovementAbilitiesContainer.GetChildren())
{

View File

@@ -8,7 +8,7 @@ namespace Mr.BrickAdventures.scripts.components;
public partial class SkillUnlockerComponent : Node
{
[Export] public SkillManager SkillManager { get; set; }
public SkillManager SkillManager { get; private set; }
[Signal]
public delegate void SkillUnlockedEventHandler(SkillData skill);
@@ -18,6 +18,7 @@ public partial class SkillUnlockerComponent : Node
public override void _Ready()
{
_gameManager = GetNode<GameManager>("/root/GameManager");
SkillManager = GetNode<SkillManager>("/root/SkillManager");
}
private bool HasEnoughCoins(int amount)