diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index 729c7c08e4db9dd1f443481b30db610cb289b9dc..00c7b83f9127b1045351b480c269af0a5fd85133 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 c7b578b86edb768bfdc33c65e13c81466ecec266..2d08ec8dd08d5b45279325b8b039fcc27cf5c770 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 58d0489d942a63c58979f2388ca217364380b005..8599ff2126944fdf917c79757e183eddad931869 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(); + } + } + }