Files
przygody-pana-cegly/scripts/components/BrickThrowComponent.cs
Gabriel Kaszewski 360f73ba24 feat: coins and skills move to the new domain (commit B of the swap)
atomic pair - purchasing couples them, so neither moves alone.

SkillData.Level is gone. It was an [Export] on a cached Resource: one
instance per process, so the level never persisted, leaked between save
slots, and made "unowned" indistinguishable from "level 1". Level now
comes from the Playthrough and is passed to ISkill.Initialize.

SkillManager reconciles from SkillSetChanged instead of being told by
each caller. The old shape was "record it, then tell SkillManager" as two
calls at four sites; AddSkill's early return when no player is registered
left the skill owned but inert. Reconciliation also tracks the level each
component was built at, so an upgrade rebuilds it rather than being
patched in from outside.

SkillUnlockerComponent collapses to one TryBuy - unlock and upgrade were
two methods each doing affordability, debit, level mutation and manager
notification in order.

The guard three components copy-pasted around Upgrades[Level - 1] - and
which BrickArmor omitted - now lives once in SkillComponentBase.

Hud reads coins from CoinsChanged instead of rebuilding translated
strings every frame. Lives still polls; it moves with the death path.
2026-07-28 20:13:24 +02:00

111 lines
2.8 KiB
C#

using Godot;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
[GlobalClass]
public partial class BrickThrowComponent : SkillComponentBase
{
[Export] public PackedScene BrickScene { get; set; }
[Export] public float FireRate { get; set; } = 1.0f;
[Export] public ThrowInputResource ThrowInputBehavior { get; set; }
private bool _canThrow = true;
private Timer _timer;
public override void _Ready()
{
SetupTimer();
_canThrow = true;
}
public override void _ExitTree()
{
if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested -= ThrowBrick;
if (_timer != null)
{
_timer.Timeout -= OnTimerTimeout;
_timer.QueueFree();
}
}
public override void _Input(InputEvent @event)
{
ThrowInputBehavior?.ProcessInput(@event);
}
public override void _Process(double delta)
{
ThrowInputBehavior?.Update(delta);
}
private void SetupTimer()
{
_timer = new Timer();
_timer.WaitTime = FireRate;
_timer.OneShot = false;
_timer.Autostart = false;
_timer.Timeout += OnTimerTimeout;
AddChild(_timer);
}
private void OnTimerTimeout()
{
_canThrow = true;
}
private void ThrowBrick(float powerMultiplier = 1f)
{
if (!_canThrow || Player == null || BrickScene == null)
return;
var instance = BrickScene.Instantiate<Node2D>();
var init = instance.GetNodeOrNull<ProjectileInitComponent>("ProjectileInitComponent");
if (init != null)
{
var @params = new ProjectileInitParams()
{
Position = Player.GlobalPosition,
Rotation = Player.Rotation,
Direction = Player.LastDirection,
PowerMultiplier = powerMultiplier,
};
init.Initialize(@params);
}
GetTree().CurrentScene.AddChild(instance);
_canThrow = false;
_timer.Start();
}
public override void Initialize(Node owner, SkillData data, int level)
{
base.Initialize(owner, data, level);
ThrowInputBehavior = (ThrowInputResource)ThrowInputBehavior?.Duplicate();
ApplyCurrentUpgrade();
}
public override void Activate()
{
if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested += ThrowBrick;
SetProcessInput(true);
}
public override void Deactivate()
{
if (ThrowInputBehavior != null) ThrowInputBehavior.ThrowRequested -= ThrowBrick;
}
public override void ApplyUpgrade(SkillUpgrade upgrade)
{
foreach (var property in upgrade.Properties)
{
Set(property.Key, property.Value);
}
}
}