Files
godot-host-test/Game/Score.cs
T
2026-07-11 10:54:02 +02:00

37 lines
908 B
C#

using R3;
using Godot;
using GodotHostTest.GodotDI;
using GodotHostTest.Helpers;
using GodotHostTest.Interfaces;
namespace GodotHostTest.Game;
public partial class Score : Control
{
[Export] private AnimationPlayer _animationPlayer = null!;
[Export] private Label _label = null!;
[Inject] private readonly ITargetSpawner _targetSpawner = null!;
private int _score;
public override void _Ready()
{
_targetSpawner.TargetSpawned.Subscribe(OnTargetSpawned).AddTo(this);
base._Ready();
}
private void OnTargetSpawned(ITarget target)
{
_animationPlayer.Play("wiggle");
target.Hit.Select(_ => 20).Merge(target.TimedOut.Select(_ => -5)).SubscribeOnce(delta =>
{
_score += delta;
UpdateScore();
}).AddTo(this);
}
private void UpdateScore()
{
_label.Text = $"Score: {_score}";
}
}