refactor: Moved spawn logic to the presenter
This commit is contained in:
parent
859d34e3bb
commit
dd9640fc39
4 changed files with 9 additions and 67 deletions
|
@ -7,9 +7,6 @@
|
||||||
|
|
||||||
public float GrowPercentage { private set; get; }
|
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 spawnRate, int growIterations) : this(0, spawnRate, growIterations) { }
|
||||||
|
|
||||||
public Model(int score, int spawnRate, int growIterations) {
|
public Model(int score, int spawnRate, int growIterations) {
|
||||||
|
@ -20,14 +17,13 @@
|
||||||
|
|
||||||
public void AddScore() {
|
public void AddScore() {
|
||||||
Score++;
|
Score++;
|
||||||
NeedsToAnimate = NeedsToSpawn = false;
|
|
||||||
|
|
||||||
if (Score % GrowIterations == 0) {
|
if (Score % GrowIterations == 0) {
|
||||||
GrowPercentage = Score / (float)(SpawnRate * GrowIterations);
|
float relativeScore = Score % (GrowIterations * SpawnRate);
|
||||||
NeedsToAnimate = true;
|
if (relativeScore == 0 && Score != 0)
|
||||||
}
|
relativeScore = GrowIterations * SpawnRate;
|
||||||
if (Score % (SpawnRate * GrowIterations) == 0)
|
GrowPercentage = relativeScore / (SpawnRate * GrowIterations);
|
||||||
NeedsToSpawn = true;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,6 +6,8 @@ namespace Presenter {
|
||||||
private readonly IExpressionInput _view;
|
private readonly IExpressionInput _view;
|
||||||
private readonly IRoseSpawner _spawner;
|
private readonly IRoseSpawner _spawner;
|
||||||
|
|
||||||
|
private bool CanSpawn => _model.Score % (_model.GrowIterations * _model.SpawnRate) == 0;
|
||||||
|
|
||||||
public ExpressionClick(Model model, IExpressionInput view, IRoseSpawner spawner) {
|
public ExpressionClick(Model model, IExpressionInput view, IRoseSpawner spawner) {
|
||||||
_model = model;
|
_model = model;
|
||||||
_view = view;
|
_view = view;
|
||||||
|
@ -16,9 +18,9 @@ namespace Presenter {
|
||||||
_model.AddScore();
|
_model.AddScore();
|
||||||
_view.UpdateView(_model.Score);
|
_view.UpdateView(_model.Score);
|
||||||
|
|
||||||
if (_model.NeedsToSpawn) {
|
if (CanSpawn)
|
||||||
_spawner.SpawnRose();
|
_spawner.SpawnRose();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,60 +3,6 @@ using NUnit.Framework;
|
||||||
|
|
||||||
namespace Tests {
|
namespace Tests {
|
||||||
public class ModelTests {
|
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]
|
[Test]
|
||||||
public void OneIteration_UpdatesGrowPercentage() {
|
public void OneIteration_UpdatesGrowPercentage() {
|
||||||
Model sut = new(5, 5);
|
Model sut = new(5, 5);
|
||||||
|
|
|
@ -50,9 +50,7 @@ namespace View.UI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateView(int score) {
|
public void UpdateView(int score) {
|
||||||
int frameDifference = score % _model.SpawnRate;
|
animator.PlayUntil(_model.GrowPercentage);
|
||||||
animator.PlayUntil(frameDifference / (float)_model.SpawnRate);
|
|
||||||
|
|
||||||
text.text = score.ToString();
|
text.text = score.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue