From dd9640fc39fb0f4f06955dd92cd50803d8872f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:09:41 +0200 Subject: [PATCH] refactor: Moved spawn logic to the presenter --- Assets/Scripts/Domain/Model.cs | 12 ++--- Assets/Scripts/Presenter/ExpressionClick.cs | 6 ++- Assets/Scripts/Tests/ModelTests.cs | 54 --------------------- Assets/Scripts/View/UI/ExpressionInput.cs | 4 +- 4 files changed, 9 insertions(+), 67 deletions(-) diff --git a/Assets/Scripts/Domain/Model.cs b/Assets/Scripts/Domain/Model.cs index 0ca7ba3..beacda8 100644 --- a/Assets/Scripts/Domain/Model.cs +++ b/Assets/Scripts/Domain/Model.cs @@ -7,9 +7,6 @@ public float GrowPercentage { private set; get; } - public bool NeedsToSpawn { private set; get; } - public bool NeedsToAnimate { private set; get; } - public Model(int spawnRate, int growIterations) : this(0, spawnRate, growIterations) { } public Model(int score, int spawnRate, int growIterations) { @@ -20,14 +17,13 @@ public void AddScore() { Score++; - NeedsToAnimate = NeedsToSpawn = false; if (Score % GrowIterations == 0) { - GrowPercentage = Score / (float)(SpawnRate * GrowIterations); - NeedsToAnimate = true; + float relativeScore = Score % (GrowIterations * SpawnRate); + if (relativeScore == 0 && Score != 0) + relativeScore = GrowIterations * SpawnRate; + GrowPercentage = relativeScore / (SpawnRate * GrowIterations); } - if (Score % (SpawnRate * GrowIterations) == 0) - NeedsToSpawn = true; } } } \ No newline at end of file diff --git a/Assets/Scripts/Presenter/ExpressionClick.cs b/Assets/Scripts/Presenter/ExpressionClick.cs index 24c08bd..3817175 100644 --- a/Assets/Scripts/Presenter/ExpressionClick.cs +++ b/Assets/Scripts/Presenter/ExpressionClick.cs @@ -6,6 +6,8 @@ namespace Presenter { private readonly IExpressionInput _view; private readonly IRoseSpawner _spawner; + private bool CanSpawn => _model.Score % (_model.GrowIterations * _model.SpawnRate) == 0; + public ExpressionClick(Model model, IExpressionInput view, IRoseSpawner spawner) { _model = model; _view = view; @@ -16,9 +18,9 @@ namespace Presenter { _model.AddScore(); _view.UpdateView(_model.Score); - if (_model.NeedsToSpawn) { + if (CanSpawn) _spawner.SpawnRose(); - } } + } } \ No newline at end of file diff --git a/Assets/Scripts/Tests/ModelTests.cs b/Assets/Scripts/Tests/ModelTests.cs index afe1df4..e54733e 100644 --- a/Assets/Scripts/Tests/ModelTests.cs +++ b/Assets/Scripts/Tests/ModelTests.cs @@ -3,60 +3,6 @@ using NUnit.Framework; namespace Tests { public class ModelTests { - [Test] - public void NoPress_NoAnimation() { - Model sut = new(20, 5); - - Assert.IsFalse(sut.NeedsToAnimate); - } - - [Test] - public void NotEnoughPresses_NoAnimation() { - Model sut = new(20, 5); - - sut.AddScore(); - - Assert.IsFalse(sut.NeedsToAnimate); - } - - - [Test] - public void EnoughPresses_Animation() { - Model sut = new(20, 5); - - for (int i = 0; i < 5; i++) - sut.AddScore(); - - Assert.IsTrue(sut.NeedsToAnimate); - } - - [Test] - public void NoPress_NoSpawn() { - Model sut = new(20, 5); - - Assert.IsFalse(sut.NeedsToSpawn); - } - - [Test] - public void NotEnoughPresses_NoSpawn() { - Model sut = new(20, 5); - - sut.AddScore(); - - Assert.IsFalse(sut.NeedsToSpawn); - } - - [Test] - public void EnoughPresses_Spawn() { - Model sut = new(20, 5); - - for (int i = 0; i < 20 * 5; i++) { - sut.AddScore(); - } - - Assert.IsTrue(sut.NeedsToSpawn); - } - [Test] public void OneIteration_UpdatesGrowPercentage() { Model sut = new(5, 5); diff --git a/Assets/Scripts/View/UI/ExpressionInput.cs b/Assets/Scripts/View/UI/ExpressionInput.cs index 4c714fd..6956507 100644 --- a/Assets/Scripts/View/UI/ExpressionInput.cs +++ b/Assets/Scripts/View/UI/ExpressionInput.cs @@ -50,9 +50,7 @@ namespace View.UI { } public void UpdateView(int score) { - int frameDifference = score % _model.SpawnRate; - animator.PlayUntil(frameDifference / (float)_model.SpawnRate); - + animator.PlayUntil(_model.GrowPercentage); text.text = score.ToString(); } }