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 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; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | @ -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(); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 	} | ||||
| } | ||||
|  | @ -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); | ||||
|  |  | |||
|  | @ -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(); | ||||
| 		} | ||||
| 	} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Gerard Gascón
						Gerard Gascón