From dfcc0b2057456ebfae6312bf0281e993eedbb0c3 Mon Sep 17 00:00:00 2001 From: koz01 <koz01@PCFEIB213-100.msad.vsb.cz> Date: Wed, 26 Oct 2022 10:18:21 +0200 Subject: [PATCH] Handling events from controls. --- src/main/java/lab/BulletAnimated.java | 14 +++++++++++++- src/main/java/lab/Cannon.java | 15 ++++++++------- src/main/java/lab/GameController.java | 11 +++++++++++ src/main/java/lab/World.java | 27 ++++++++++++++++++++++++++- src/main/resources/lab/GameView.fxml | 2 +- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index b778b08..93db9c1 100644 --- a/src/main/java/lab/BulletAnimated.java +++ b/src/main/java/lab/BulletAnimated.java @@ -15,7 +15,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ private double mass = 2; private double strenghtOfCannon = 100; private double cannonLength = 100; - private boolean accelerate = true; + private boolean accelerate; private boolean hitToGround = false; private double crossSectionalArea; @@ -49,6 +49,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ } public void simulate(double deltaT) { + if (!accelerate && position == start) { + return; + } if (accelerate && start.distance(position) < cannonLength) { double cannonAngle = cannon.getAngle(); speed = speed @@ -90,7 +93,16 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ position = start; speed = initialSpeed; hitToGround = false; + } + + public void fire() { + reload(); accelerate = true; } + + public void setForce(double doubleValue) { + strenghtOfCannon = doubleValue; + + } } diff --git a/src/main/java/lab/Cannon.java b/src/main/java/lab/Cannon.java index eb3f789..7df7892 100644 --- a/src/main/java/lab/Cannon.java +++ b/src/main/java/lab/Cannon.java @@ -7,7 +7,6 @@ import javafx.scene.transform.Affine; public class Cannon implements DrawableSimulable { - private int direction=-1; private double angle = 0; private Point2D position; private Point2D size; @@ -24,10 +23,7 @@ public class Cannon implements DrawableSimulable { } public void simulate(double timeStep) { - angle = angle + direction*0.8; - if(angle <=-90 || angle >= 0) { - direction*=-1; - } + } public void draw(GraphicsContext gc) { @@ -37,13 +33,18 @@ public class Cannon implements DrawableSimulable { gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2); gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40); gc.fillOval(worldPosition.getX()+size.getX(), worldPosition.getY()+size.getY()/2, 40, 40); - gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2))); + gc.setTransform(new Affine(Affine.rotate(-angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2))); gc.setFill(Color.BLACK); gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY()); gc.restore(); } public double getAngle() { - return (angle * -1) / 180 * Math.PI; + return angle / 180 * Math.PI; + } + + public void setAngle(double newValue) { + angle = newValue; + } } diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index ec3ebdc..240fb4c 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -29,7 +29,18 @@ public class GameController { this.world = new World(canvas.getWidth(), canvas.getHeight()); //Draw scene on a separate thread to avoid blocking UI. animationTimer = new DrawingThread(canvas, world); + + angleSlider.valueProperty().addListener( (obj, oldValue, newValue) -> world.setAngleOfCannon(newValue.doubleValue())); + forceSlider.valueProperty().addListener( (obj, oldValue, newValue) -> world.setForce(newValue.doubleValue())); + animationTimer.start(); + + } + + + @FXML + private void firePressed() { + world.fireBullet(); } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index dd53bc3..5cc00cb 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -7,7 +7,7 @@ import javafx.scene.canvas.GraphicsContext; public class World { - private final static int NUMBER_OF_DRAGONS = 5; + private final static int NUMBER_OF_DRAGONS = 1; private double width; private double height; @@ -78,4 +78,29 @@ public class World { this.height = height; } + public void fireBullet() { + for (DrawableSimulable obj: entities) { + if (obj instanceof BulletAnimated bullet) { + bullet.fire(); + } + } + + } + + public void setAngleOfCannon(double newValue) { + for (DrawableSimulable obj: entities) { + if (obj instanceof Cannon cannon) { + cannon.setAngle(newValue); + } + } + } + + public void setForce(double doubleValue) { + for (DrawableSimulable obj: entities) { + if (obj instanceof BulletAnimated bullet) { + bullet.setForce(doubleValue); + } + } + } + } diff --git a/src/main/resources/lab/GameView.fxml b/src/main/resources/lab/GameView.fxml index 8b34857..751562a 100644 --- a/src/main/resources/lab/GameView.fxml +++ b/src/main/resources/lab/GameView.fxml @@ -20,7 +20,7 @@ <Insets bottom="11.0" left="13.0" right="13.0" top="11.0" /> </padding> </Slider> - <Button fx:id="fireButton" mnemonicParsing="false" text="Fire" HBox.hgrow="ALWAYS"> + <Button fx:id="fireButton" mnemonicParsing="false" onAction="#firePressed" text="Fire" HBox.hgrow="ALWAYS"> <padding> <Insets bottom="11.0" left="13.0" right="13.0" top="11.0" /> </padding> -- GitLab