diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index fc4398cb0a3c2271f82be28c0751ebcee307597e..bad14d3eb73e26602e8d6ec4685f8247714c5769 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -1,9 +1,11 @@ package lab; import javafx.application.Application; +import javafx.fxml.FXMLLoader; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; +import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import javafx.stage.WindowEvent; @@ -17,17 +19,13 @@ public class App extends Application { launch(args); } - private Canvas canvas; private GameController controller; @Override public void start(Stage primaryStage) { try { //Construct a main window with a canvas. - - - Group root = new Group(); - canvas = new Canvas(800, 600); - root.getChildren().add(canvas); + FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("GameView.fxml")); + BorderPane root = fxmlLoader.load(); Scene scene = new Scene(root); @@ -35,7 +33,7 @@ public class App extends Application { primaryStage.resizableProperty().set(false); primaryStage.setTitle("Java 1 - 5th laboratory"); primaryStage.show(); - controller = new GameController(canvas); + controller = fxmlLoader.getController(); controller.startGame(); //Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index b778b081eeefd8ff848ed3eda0a07757d39df95c..d3b9fba4a3648dbb757a141dbfd019c5da9edd5a 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 = false; private boolean hitToGround = false; private double crossSectionalArea; @@ -49,7 +49,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ } public void simulate(double deltaT) { - if (accelerate && start.distance(position) < cannonLength) { + if (speed == initialSpeed && !accelerate) { + return; + } else if (accelerate && start.distance(position) < cannonLength) { double cannonAngle = cannon.getAngle(); speed = speed .add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon) @@ -90,7 +92,16 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ position = start; speed = initialSpeed; hitToGround = false; + accelerate = false; + } + + public void fire() { 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 eb3f7894ff90af890ddf6ff1fda29560ed9e6ae1..7e326a47bcd36ac773912e9b78a31905ba04335a 100644 --- a/src/main/java/lab/Cannon.java +++ b/src/main/java/lab/Cannon.java @@ -24,10 +24,7 @@ public class Cannon implements DrawableSimulable { } public void simulate(double timeStep) { - angle = angle + direction*0.8; - if(angle <=-90 || angle >= 0) { - direction*=-1; - } + //do nothing } public void draw(GraphicsContext gc) { @@ -46,4 +43,8 @@ public class Cannon implements DrawableSimulable { public double getAngle() { return (angle * -1) / 180 * Math.PI; } + + public void setAngle(double value) { + angle = value; + } } diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 538557076a8345ca2c92ffc8fcdf4b450c7aebc8..eaadc748e67b7300aae9c46b1f1ee87ce78e5a45 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -1,23 +1,38 @@ package lab; import javafx.animation.AnimationTimer; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.fxml.FXML; import javafx.scene.canvas.Canvas; +import javafx.scene.control.Slider; public class GameController { private World world; + + @FXML private Canvas canvas; - private AnimationTimer animationTimer; - public GameController(Canvas canvas) { - this.canvas = canvas; - } + @FXML + private Slider angleSlider; + + @FXML + private Slider forceSlider; + + + + private AnimationTimer animationTimer; public void startGame() { - this.world = new World(canvas.getWidth(), canvas.getHeight()); + this.world = new World(canvas.getWidth(), canvas.getHeight()); + angleSlider.valueProperty().addListener( (val, oldValue, newValue) -> world.setAngle(newValue.doubleValue())); + forceSlider.valueProperty().addListener( (val, oldValue, newValue) -> world.setForce(newValue.doubleValue())); //Draw scene on a separate thread to avoid blocking UI. animationTimer = new DrawingThread(canvas, world); animationTimer.start(); + + } @@ -25,5 +40,9 @@ public class GameController { animationTimer.stop(); } + @FXML + private void firePressed() { + world.fire(); + } } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index dd53bc3857b678cb9c898b7cfccc58b6978fe10b..2807b68e21c5b3338bf56de89cdf2c8e0130d8b1 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -12,15 +12,17 @@ public class World { private double width; private double height; private DrawableSimulable []entities; + private BulletAnimated bullet; + private Cannon cannon; public World(double width, double height) { super(); this.width = width; this.height = height; - Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); + cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); 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); + entities[1] = bullet =new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40); Random rnd = new Random(); for (int i = 2; i < entities.length; i++) { @@ -78,4 +80,18 @@ public class World { this.height = height; } + public void fire() { + bullet.fire(); + } + + public void setAngle(double value) { + cannon.setAngle(value); + } + + public void setForce(double doubleValue) { + bullet.setForce(doubleValue); + } + + + } diff --git a/src/main/resources/lab/GameView.fxml b/src/main/resources/lab/GameView.fxml new file mode 100644 index 0000000000000000000000000000000000000000..4642d996b2802c57214d624ba38c80fd2afcf5b7 --- /dev/null +++ b/src/main/resources/lab/GameView.fxml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.geometry.Insets?> +<?import javafx.scene.canvas.Canvas?> +<?import javafx.scene.control.Button?> +<?import javafx.scene.control.Slider?> +<?import javafx.scene.layout.BorderPane?> +<?import javafx.scene.layout.HBox?> + + +<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController"> + <center> + <Canvas fx:id="canvas" height="400.0" width="600.0" BorderPane.alignment="CENTER" /> + </center> + <bottom> + <HBox BorderPane.alignment="CENTER"> + <children> + <Slider fx:id="angleSlider" max="0.0" min="-90.0" showTickLabels="true" showTickMarks="true" HBox.hgrow="ALWAYS"> + <HBox.margin> + <Insets top="11.0" /> + </HBox.margin> + </Slider> + <Button mnemonicParsing="false" onAction="#firePressed" text="Fire" HBox.hgrow="ALWAYS"> + <HBox.margin> + <Insets bottom="11.0" left="13.0" right="13.0" top="11.0" /> + </HBox.margin> + </Button> + <Slider fx:id="forceSlider" max="300.0" min="10.0" showTickLabels="true" showTickMarks="true" HBox.hgrow="ALWAYS"> + <HBox.margin> + <Insets top="11.0" /> + </HBox.margin> + </Slider> + </children> + </HBox> + </bottom> +</BorderPane>