This commit is contained in:
redglow
2026-07-21 20:49:24 +02:00
parent c540b99eb6
commit e7dbb9cabb
31 changed files with 394 additions and 76 deletions
+9 -15
View File
@@ -6,34 +6,28 @@ using R3;
namespace GodotHostTest.Game.Score;
public partial class Score : Control, IScore
public partial class Score : Control
{
[Export] private AnimationPlayer _animationPlayer = null!;
[Inject] private ICurrentLevel _currentLevel = null!;
[Export] private Label _label = null!;
private int _score;
[Inject] private ITargetSpawner _targetSpawner = null!;
int IScore.Score => _score;
[Inject] private IScore _score = null!;
public override void _Ready()
{
_targetSpawner.TargetSpawned.Subscribe(OnTargetSpawned).AddTo(this);
base._Ready();
_score.Score.Subscribe(UpdateScore).AddTo(this);
_score.Score.Chunk(2, 1).Where(values => values.Length == 2 && values[1] < values[0])
.Subscribe(OnScoreDecreasing).AddTo(this);
}
private void OnTargetSpawned(ITarget target)
private void OnScoreDecreasing<T>(T _)
{
_animationPlayer.Play("wiggle");
target.Hit.Select(_ => 20).Merge(target.TimedOut.Select(_ => -5)).SubscribeOnce(delta =>
{
_score += delta;
UpdateScore();
}).AddTo(this);
}
private void UpdateScore()
private void UpdateScore(int newScore)
{
_label.Text = $"Score: {_score}";
_label.Text = $"Level {_currentLevel.LevelNumber} - Score: {newScore}";
}
}
+29
View File
@@ -0,0 +1,29 @@
using System;
using GodotHostTest.Interfaces;
using R3;
namespace GodotHostTest.Game.Score;
public class TotalScore : IScore, IDisposable
{
private readonly IDisposable _connectedObservableDisposable;
public TotalScore(ITargetEventBus targetEventBus)
{
var connectedObservable = targetEventBus.Hit.Select(_ => 20)
.Merge(targetEventBus.TimedOut.Select(_ => -5))
.Scan(0, (x1, x2) => x1 + x2)
.Prepend(0)
.Replay(1);
_connectedObservableDisposable = connectedObservable.Connect();
Score = connectedObservable.AsObservable();
}
public void Dispose()
{
_connectedObservableDisposable.Dispose();
GC.SuppressFinalize(this);
}
public Observable<int> Score { get; }
}
+1
View File
@@ -0,0 +1 @@
uid://gymu1u4w882o