From 4dafdeec5a2fe24b82c239b4c98edc5cd0c1f3a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Mon, 25 Oct 2021 15:41:39 +0200
Subject: [PATCH] lab07-solution-monday-1415

---
 src/main/java/lab/BulletAnimated.java |  4 +++
 src/main/java/lab/GameController.java | 46 +++++++++++++++++++--------
 src/main/java/lab/World.java          | 13 ++++++++
 3 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java
index 729c7c0..00c7b83 100644
--- a/src/main/java/lab/BulletAnimated.java
+++ b/src/main/java/lab/BulletAnimated.java
@@ -113,5 +113,9 @@ public class BulletAnimated  implements DrawableSimulable, Collisionable{
 		accelerate = true;
 		speed = initialSpeed;
 	}
+
+	public void setHitListener(HitListener hitListenerImpl) {
+		this.hitListener = hitListenerImpl;
+	}
 	
 }
diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java
index c7b578b..2d08ec8 100644
--- a/src/main/java/lab/GameController.java
+++ b/src/main/java/lab/GameController.java
@@ -32,21 +32,10 @@ public class GameController {
 	}
 	
 	public void startGame() {
-		this.world = new World(canvas.getWidth(), canvas.getHeight());	
+		this.world = new World(canvas.getWidth(), canvas.getHeight());
+		this.world.setGameListener(new GameListenerImpl());
 		//Draw scene on a separate thread to avoid blocking UI.
-		animationTimer = new AnimationTimer() {
-			private Long previous;
-			
-			@Override
-			public void handle(long now) {
-				if (previous == null) {
-					previous = now;
-				} else {
-					drawScene((now - previous)/1e9);
-					previous = now;
-				}
-			}
-		};
+		animationTimer = new AnimationTimerImpl();
 		angleSlider.valueProperty().addListener(this::angleChanged);
 		world.setCannonAngle(angleSlider.getValue());
 		
@@ -80,4 +69,33 @@ public class GameController {
 			, Number oldValue, Number newValue) {
 		world.setCannonStrength(newValue.doubleValue());
 	}
+	
+	private final class AnimationTimerImpl extends AnimationTimer {
+		private Long previous;
+
+		@Override
+		public void handle(long now) {
+			if (previous == null) {
+				previous = now;
+			} else {
+				drawScene((now - previous)/1e9);
+				previous = now;
+			}
+		}
+	}
+
+	private class GameListenerImpl implements GameListener {
+
+		@Override
+		public void stateChanged(int aShoots, int aHits) {
+			GameController.this.shoots.setText("" + aShoots);
+			hits.setText("" + aHits);
+		}
+
+		@Override
+		public void gameOver() {
+			stopGame();
+		}
+		
+	}
 }
diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java
index 58d0489..8599ff2 100644
--- a/src/main/java/lab/World.java
+++ b/src/main/java/lab/World.java
@@ -29,6 +29,7 @@ public class World {
 		entities = new DrawableSimulable[2 + NUMBER_OF_DRAGONS];
 		entities[0] = cannon;
 		entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
+		((BulletAnimated)entities[1]).setHitListener(this::bulletHit);
 		Random rnd = new Random();
 		
 		for (int i = 2; i < entities.length; i++) {
@@ -117,4 +118,16 @@ public class World {
 		}
 	}
 
+	public void setGameListener(GameListener gameListener) {
+		this.gameListener = gameListener;
+	}
+
+	private void bulletHit() {
+		hits++;
+		gameListener.stateChanged(shoots, hits);
+		if (shoots > 10 ) {
+			gameListener.gameOver();
+		}
+	}
+
 }
-- 
GitLab