.
This commit is contained in:
+9
-15
@@ -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}";
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://gymu1u4w882o
|
||||
Reference in New Issue
Block a user