diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 9ca616a2e3c3d03941e2a8d096141270039e94e9..7cbb7edce7a2ba81556018227f058a0ade935963 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 4519477afdda718ccaa1c3e62e8db1506eec2bef..f23c63467880c57d194187f72a3a60a700cc29c9 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 0000000000000000000000000000000000000000..b9ecc434355d5c065ee6d831d1ff3195f964c493 --- /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 b233dcdfa5a5a23c7b8b76ccb45c08c4a1cc53f5..bfc98b1db504afd95b42c5f652dfd4b15d68d4a0 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++) {