Skip to content
Snippets Groups Projects
Commit 076cd319 authored by koz01's avatar koz01
Browse files

solution-2022_Mon_10-45

parent cf0c6b55
Branches
No related merge requests found
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);
......
......@@ -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;
}
}
......@@ -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;
}
}
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();
}
}
......@@ -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);
}
}
<?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>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment