From 8292e2ad59e87093c4b04547adbbfa9eb9265163 Mon Sep 17 00:00:00 2001 From: koz01 <koz01@pccpit1v203-096-040.vsb.cz> Date: Wed, 27 Oct 2021 10:31:58 +0200 Subject: [PATCH] lab07-solution-wednesday-9_00 --- src/main/java/lab/BulletAnimated.java | 8 +++-- src/main/java/lab/GameController.java | 46 +++++++++++++++++++-------- src/main/java/lab/World.java | 14 ++++++++ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index d8b709e..06d6c27 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.; @@ -56,7 +55,6 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ if (!fired) { return; } - double timeStep = deltaT * 1000; if (accelerate && start.distance(position) < cannonLength) { double cannonAngle = cannon.getAngle(); double strenghtOfCannon = cannon.getStrength() * STRENGTH_CANNON_COEFICIENT/100.; @@ -69,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)); System.out.println("speed"+ speed); } @@ -114,5 +112,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 c7b578b..2a23c20 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 58d0489..9fb2e23 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); + } + } -- GitLab