diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index 5523847e2994b8a0b78f3297dde3302cfaeea83f..dd27903d2b2011bef7c09c41f5b9ceda9000ce4b 100644 --- a/src/main/java/lab/BulletAnimated.java +++ b/src/main/java/lab/BulletAnimated.java @@ -4,7 +4,6 @@ import javafx.geometry.Point2D; import javafx.geometry.Rectangle2D; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; - public class BulletAnimated implements DrawableSimulable, Collisionable{ private static final double STRENGTH_CANNON_COEFICIENT = 4.; @@ -68,7 +67,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ -1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * dragCoefficient * Math.pow(speed.getX(), 2), -1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * 0.47 * Math.pow(speed.getY(), 2)); Point2D acceleration = new Point2D(-airResistanceforce.getX() * mass, - (-Constants.GRAVITATIONAL_ACCELERATION/30 + airResistanceforce.getY()) * mass); + (-Constants.GRAVITATIONAL_ACCELERATION/20 + airResistanceforce.getY()) * mass); speed = speed.add(acceleration.multiply(deltaT)); } if (!hitToGround) { @@ -112,5 +111,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ accelerate = true; speed = initialSpeed; } + + public void setHitListener(HitListener hitListenerImpl) { + hitListener = hitListenerImpl; + } } diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index c7b578b86edb768bfdc33c65e13c81466ecec266..2a23c20991e6689a9e4c3debe06e397623a1fe29 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 shoots, int hits) { + GameController.this.shoots.setText("" + shoots); + GameController.this.hits.setText("" + hits); + } + + @Override + public void gameOver() { + stopGame(); + } + + } } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index 58d0489d942a63c58979f2388ca217364380b005..9fb2e23b138fc91bc73455181e5080449975d01f 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::hitOfBullet); Random rnd = new Random(); for (int i = 2; i < entities.length; i++) { @@ -113,8 +114,21 @@ public class World { ba.fire(); shoots++; gameListener.stateChanged(shoots, hits); + if (shoots > 10) { + gameListener.gameOver(); + } } } } + public void setGameListener(GameListener gameListenerImpl) { + this.gameListener = gameListenerImpl; + + } + + private void hitOfBullet() { + hits++; + gameListener.stateChanged(shoots, hits); + } + }