From ae80689b7597defc291e9b4b9c541f1ab5f31f3c Mon Sep 17 00:00:00 2001
From: koz01 <koz01@PCFEIB113-26.msad.vsb.cz>
Date: Tue, 26 Sep 2023 12:16:35 +0200
Subject: [PATCH] solution 2023

---
 src/main/java/lab/App.java              | 26 ++++++++++++-
 src/main/java/lab/GameController.java   | 49 ++++++++++++++++++++++++-
 src/main/java/lab/GameListenerImpl.java | 29 +++++++++++++++
 src/main/java/lab/World.java            | 27 +++++++++++++-
 4 files changed, 127 insertions(+), 4 deletions(-)
 create mode 100644 src/main/java/lab/GameListenerImpl.java

diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java
index 9ca616a..7cbb7ed 100644
--- a/src/main/java/lab/App.java
+++ b/src/main/java/lab/App.java
@@ -1,6 +1,7 @@
 package lab;
 
 import java.util.Random;
+import java.util.Scanner;
 
 import javafx.application.Application;
 import javafx.fxml.FXMLLoader;
@@ -25,6 +26,7 @@ public class App extends Application {
 	@Override
 	public void start(Stage primaryStage) {
 		try {
+			
 			//Construct a main window with a canvas.  
 			
 			FXMLLoader loader = new FXMLLoader(this.getClass().getResource("GameView.fxml"));
@@ -52,11 +54,31 @@ public class App extends Application {
 
 	
 	private Point2D[][] getDragonsInit(double width, double height) {
-		
+		Scanner sc = new Scanner(System.in);
+		Point2D[][] result;
 		//ask whether would like to enter dragons
+		System.out.println("How many dragons do you want?");
+		//test whether integer is on input
+		if (sc.hasNextInt()) {
+			int dragonsNum = sc.nextInt();
+			result = new Point2D[dragonsNum][];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = new Point2D[2];
+				System.out.println("What is position of the dragon #" + (i + 1) + " [x, y]: ");
+				result[i][0] =  new Point2D(sc.nextDouble(), sc.nextDouble());
+				System.out.println("What is velocity of the dragon #" + (i + 1) + " [x, y]: ");
+				result[i][1] =  new Point2D(sc.nextDouble(), sc.nextDouble());
+			}
+		} else {
+			result = getDefaultDragonsInit(width, height);
+		}
+		return result;
+		
 		
+		//...
 		//return default
-		return getDefaultDragonsInit(width, height);
+		
+		//result = getDefaultDragonsInit(width, height);
 		
 	}
 
diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java
index 4519477..f23c634 100644
--- a/src/main/java/lab/GameController.java
+++ b/src/main/java/lab/GameController.java
@@ -33,7 +33,9 @@ public class GameController {
 	}
 	
 	public void startGame(Point2D[][] dragonInit) {
-		this.world = new World(canvas.getWidth(), canvas.getHeight(), dragonInit);	
+		GameListener gl = new GameListenerImpl();
+		this.world = new World(canvas.getWidth(), canvas.getHeight(), dragonInit);
+		this.world.setGameListener(gl);
 		//Draw scene on a separate thread to avoid blocking UI.
 		animationTimer = new DrawingThread(canvas, world);
 		angleSlider.valueProperty().addListener(this::angleChanged);
@@ -64,4 +66,49 @@ public class GameController {
 			, Number oldValue, Number newValue) {
 		world.setCannonStrength(newValue.doubleValue());
 	}
+	
+	class GameListenerImpl implements GameListener {
+
+		
+		@Override
+		public void stateChanged(int shoots, int hits) {
+			GameController.this.shoots.setText("" + shoots);
+			GameController.this.hits.setText("" + hits);
+
+		}
+
+		@Override
+		public void gameOver() {
+			// leave empty
+
+		}
+
+	}
+	
+	
+	static class GameListenerImplStatic implements GameListener {
+
+		final Label shoots;
+		
+		final Label hits;
+		
+		public GameListenerImplStatic(Label shoots, Label hits) {
+			this.shoots = shoots;
+			this.hits = hits;
+		}
+
+		@Override
+		public void stateChanged(int shoots, int hits) {
+			this.shoots.setText("" + shoots);
+			this.hits.setText("" + hits);
+
+		}
+
+		@Override
+		public void gameOver() {
+			// leave empty
+
+		}
+
+	}
 }
diff --git a/src/main/java/lab/GameListenerImpl.java b/src/main/java/lab/GameListenerImpl.java
new file mode 100644
index 0000000..b9ecc43
--- /dev/null
+++ b/src/main/java/lab/GameListenerImpl.java
@@ -0,0 +1,29 @@
+package lab;
+
+import javafx.scene.control.Label;
+
+public class GameListenerImpl implements GameListener {
+
+	final private Label shoots;
+	
+	final private Label hits;
+	
+	public GameListenerImpl(Label shoots, Label hits) {
+		this.shoots = shoots;
+		this.hits = hits;
+	}
+
+	@Override
+	public void stateChanged(int shoots, int hits) {
+		this.shoots.setText("" + shoots);
+		this.hits.setText("" + hits);
+
+	}
+
+	@Override
+	public void gameOver() {
+		// leave empty
+
+	}
+
+}
diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java
index b233dcd..bfc98b1 100644
--- a/src/main/java/lab/World.java
+++ b/src/main/java/lab/World.java
@@ -21,10 +21,35 @@ public class World {
 		super();
 		this.width = width;
 		this.height = height;
+/*		
+		class HitListenerImpl implements HitListener {
+
+			@Override
+			public void hit() {
+				hits++;
+				gameListener.stateChanged(shoots, hits);
+			}
+			
+		}
+	*/	
+		
+		
 		Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
 		entities = new DrawableSimulable[2 + dragonInit.length];
 		entities[0] = cannon;
-		entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
+		
+		//see ii
+		BulletAnimated temp;
+		entities[1] = temp = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
+		
+		//see it
+		temp.setHitListener(new HitListener() {
+			@Override
+			public void hit() {
+				hits++;
+				gameListener.stateChanged(shoots, hits);
+			}
+		});
 		
 		for (int i = 2; i < entities.length; i++) {
 		
-- 
GitLab