Add skill upgrade system and refactor skill components for enhanced functionality; update resource paths and configurations

This commit is contained in:
2025-08-26 23:48:59 +02:00
parent afca70e6c6
commit 55c3ae212b
20 changed files with 212 additions and 76 deletions

View File

@@ -8,11 +8,12 @@ public partial class SkillData : Resource
{
[Export] public string Name { get; set; } = "New Skill";
[Export] public string Description { get; set; } = "New Skill";
[Export] public int Cost { get; set; } = 0;
[Export] public Texture2D Icon { get; set; }
[Export] public bool IsActive { get; set; } = false;
[Export] public int Level { get; set; } = 1;
[Export] public int MaxLevel { get; set; } = 1;
[Export] public SkillType Type { get; set; } = SkillType.Throw;
[Export] public PackedScene Node { get; set; }
[Export] public Array<SkillUpgrade> Upgrades { get; set; } = [];
public int MaxLevel => Upgrades.Count;
}

View File

@@ -0,0 +1,12 @@
using Godot;
using Godot.Collections;
namespace Mr.BrickAdventures.scripts.Resources;
[GlobalClass]
public partial class SkillUpgrade : Resource
{
[Export] public int Cost { get; set; } = 50;
[Export(PropertyHint.MultilineText)] public string Description { get; set; } = "Upgrade Description";
[Export] public Dictionary<string, Variant> Properties { get; set; } = new();
}

View File

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

View File

@@ -46,7 +46,7 @@ public partial class SkillManager : Node
var instance = skillData.Node.Instantiate();
if (instance is ISkill skill)
{
skill.Initialize(Owner);
skill.Initialize(Owner, skillData);
skill.Activate();
}
else

View File

@@ -35,12 +35,12 @@ public partial class Marketplace : Control
var unlockedSkills = _gameManager.GetUnlockedSkills();
foreach (var skill in unlockedSkills) CreateSkillButton(skill);
SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocker;
SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocked;
}
public override void _ExitTree()
{
SkillUnlockerComponent.SkillUnlocked -= OnSkillUnlocker;
SkillUnlockerComponent.SkillUnlocked -= OnSkillUnlocked;
}
public override void _Input(InputEvent @event)
@@ -59,21 +59,24 @@ public partial class Marketplace : Control
}
}
private string GetButtonText(SkillData skill)
private void OnSkillUnlocked(SkillData skill)
{
return $"{Tr(skill.Name)} {skill.Cost}";
}
private void OnSkillUnlocker(SkillData skill)
{
if (_skillButtons.Count == 0) CreateSkillButton(skill);
var buttonExists = false;
foreach (var existingButton in _skillButtons)
{
if (existingButton.Name == skill.Name)
{
buttonExists = true;
break;
}
}
if (!buttonExists) CreateSkillButton(skill);
foreach (var btn in _skillButtons)
{
if (btn.Data.IsActive)
btn.Activate();
else
btn.Deactivate();
if (btn.Data.IsActive) btn.Activate();
else btn.Deactivate();
}
}
@@ -93,7 +96,6 @@ public partial class Marketplace : Control
private void CreateUpgradeButton(SkillData skill)
{
var button = MarketplaceButtonScene.Instantiate<MarketplaceButton>();
button.Text = GetButtonText(skill);
button.Data = skill;
button.Icon = skill.Icon;
button.Pressed += () => OnUpgradeButtonPressed(skill);
@@ -123,18 +125,6 @@ public partial class Marketplace : Control
}
}
private void RemoveButton(SkillData skill)
{
foreach (var node in ToUnlockGrid.GetChildren())
{
var child = (Button)node;
if (child.Text != GetButtonText(skill)) continue;
child.QueueFree();
break;
}
}
private void OnSkillButtonPressed(SkillButton button)
{
SkillUnlockerComponent.SkillManager.ToggleSkillActivation(button.Data);

View File

@@ -18,45 +18,73 @@ public partial class MarketplaceButton : Button
public override void _Ready()
{
_gameManager = GetNode<GameManager>("/root/GameManager");
Setup();
var player = _gameManager.Player;
var skillUnlockerComponent = player?.GetNodeOrNull<SkillUnlockerComponent>("SkillUnlockerComponent");
if (skillUnlockerComponent == null) return;
if (player == null) return;
skillUnlockerComponent.SkillUnlocked += OnSkillUnlock;
_skillUnlockerComponent = player.GetNodeOrNull<SkillUnlockerComponent>("SkillUnlockerComponent");
if (_skillUnlockerComponent != null)
{
_skillUnlockerComponent.SkillUnlocked += OnSkillStateChanged;
}
UpdateButtonState();
}
public override void _ExitTree()
{
_skillUnlockerComponent.SkillUnlocked -= OnSkillUnlock;
if (_skillUnlockerComponent != null)
{
_skillUnlockerComponent.SkillUnlocked -= OnSkillStateChanged;
}
}
private void OnSkillStateChanged(SkillData skill)
{
if (skill.Name == Data.Name)
{
UpdateButtonState();
}
}
private void Setup()
private void UpdateButtonState()
{
if (Data == null) return;
if (Data == null || Data.Upgrades.Count == 0)
{
Visible = false;
return;
}
var isUnlocked = _gameManager.IsSkillUnlocked(Data);
for (var i = 0; i < SkillLevelContainer.GetChildCount(); i++)
{
SkillLevelContainer.GetChild(i).QueueFree();
}
for (var i = 0; i < Data.MaxLevel; i++)
{
var icon = new TextureRect()
{
Texture = i < Data.Level ? UnlockedSkillIcon : LockedSkillIcon,
Texture = (isUnlocked && i < Data.Level) ? UnlockedSkillIcon : LockedSkillIcon,
ExpandMode = TextureRect.ExpandModeEnum.FitWidthProportional
};
SkillLevelContainer.AddChild(icon);
}
}
private void OnSkillUnlock(SkillData skill)
{
if (skill.Name != Data.Name) return;
for (var i = 0; i < Data.MaxLevel; i++)
if (!isUnlocked)
{
var icon = SkillLevelContainer.GetChildOrNull<TextureRect>(i);
if (icon == null) continue;
icon.Texture = i < Data.Level ? UnlockedSkillIcon : LockedSkillIcon;
Disabled = i >= Data.Level;
Text = $"{Tr(Data.Name)} ({Data.Upgrades[0].Cost})";
Disabled = false;
}
else if (Data.Level < Data.MaxLevel)
{
Text = $"{Tr(Data.Name)} ({Data.Upgrades[Data.Level].Cost})";
Disabled = false;
}
else
{
Text = $"{Tr(Data.Name)} (MAX)";
Disabled = true;
}
}
}

View File

@@ -13,6 +13,7 @@ public partial class BrickThrowComponent : Node, ISkill
private bool _canThrow = true;
private Timer _timer;
private SkillData _skillData;
public override void _Ready()
{
@@ -81,13 +82,20 @@ public partial class BrickThrowComponent : Node, ISkill
_timer.Start();
}
public void Initialize(Node owner)
public void Initialize(Node owner, SkillData data)
{
PlayerController = owner as PlayerController;
_skillData = data;
if (PlayerController == null)
{
GD.PushError("BrickThrowComponent: Owner is not a PlayerController.");
}
if (_skillData.Level > 0 && _skillData.Upgrades.Count >= _skillData.Level)
{
ApplyUpgrade(_skillData.Upgrades[_skillData.Level - 1]);
}
}
public void Activate()
@@ -99,4 +107,12 @@ public partial class BrickThrowComponent : Node, ISkill
{
if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested -= ThrowBrick;
}
public void ApplyUpgrade(SkillUpgrade upgrade)
{
foreach (var property in upgrade.Properties)
{
Set(property.Key, property.Value);
}
}
}

View File

@@ -2,6 +2,7 @@ using System;
using Godot;
using Godot.Collections;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
@@ -12,6 +13,7 @@ public partial class MagneticSkillComponent : Node, ISkill
private Array<Node2D> _collectablesToPickUp = [];
private Node2D _owner;
private SkillData _skillData;
public override void _Process(double delta)
{
@@ -70,9 +72,11 @@ public partial class MagneticSkillComponent : Node, ISkill
collectable.GlobalPosition += direction.Normalized() * speed;
}
public void Initialize(Node owner)
public void Initialize(Node owner, SkillData data)
{
_owner = owner as Node2D;
_skillData = data;
if (_owner == null)
{
GD.PushWarning("MagneticSkillComponent: Owner is not a Node2D.");
@@ -91,6 +95,11 @@ public partial class MagneticSkillComponent : Node, ISkill
}
}
}
if (_skillData.Level > 0 && _skillData.Upgrades.Count >= _skillData.Level)
{
ApplyUpgrade(_skillData.Upgrades[_skillData.Level - 1]);
}
}
public void Activate()
@@ -112,4 +121,12 @@ public partial class MagneticSkillComponent : Node, ISkill
MagneticArea.BodyEntered -= OnBodyEntered;
MagneticArea.AreaEntered -= OnAreaEntered;
}
public void ApplyUpgrade(SkillUpgrade upgrade)
{
foreach (var property in upgrade.Properties)
{
Set(property.Key, property.Value);
}
}
}

View File

@@ -1,6 +1,7 @@
using Godot;
using Godot.Collections;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
@@ -28,11 +29,11 @@ public partial class SkillUnlockerComponent : Node
{
if (_gameManager == null) return false;
if (_gameManager.IsSkillUnlocked(skill)) return false;
if (!HasEnoughCoins(skill.Cost)) return false;
if (!HasEnoughCoins(skill.Upgrades[0].Cost)) return false;
skill.Level = 1;
skill.IsActive = true;
_gameManager.RemoveCoins(skill.Cost);
_gameManager.RemoveCoins(skill.Upgrades[0].Cost);
var skillsUnlocked = (Array<SkillData>)_gameManager.CurrentSessionState["skills_unlocked"];
skillsUnlocked.Add(skill);
@@ -59,11 +60,19 @@ public partial class SkillUnlockerComponent : Node
{
if (_gameManager == null) return false;
if (!_gameManager.IsSkillUnlocked(skill)) return false;
if (!HasEnoughCoins(skill.Cost)) return false;
if (skill.Level >= skill.MaxLevel) return false;
if (!HasEnoughCoins(skill.Upgrades[skill.Level].Cost)) return false;
_gameManager.RemoveCoins(skill.Cost);
_gameManager.RemoveCoins(skill.Upgrades[skill.Level].Cost);
skill.Level++;
if (SkillManager.ActiveComponents.TryGetValue(skill.Name, out Variant componentVariant))
{
var component = componentVariant.AsGodotObject();
if (component is ISkill skillInstance)
{
skillInstance.ApplyUpgrade(skill.Upgrades[skill.Level - 1]);
}
}
EmitSignalSkillUnlocked(skill);
return true;
}

View File

@@ -1,11 +1,13 @@
using Godot;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.interfaces;
public interface ISkill
{
void Initialize(Node owner);
void Initialize(Node owner, SkillData data);
void Activate();
void Deactivate();
void ApplyUpgrade(SkillUpgrade upgrade);
}