Skip to content
Snippets Groups Projects
Verified Commit 97e94c4a authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Allow define position for dragons.

parent b96c34b7
Branches
No related merge requests found
Pipeline #158 failed with stages
in 0 seconds
package lab; package lab;
import java.util.Random;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.geometry.Point2D;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.stage.Stage; import javafx.stage.Stage;
...@@ -13,6 +16,7 @@ import javafx.stage.WindowEvent; ...@@ -13,6 +16,7 @@ import javafx.stage.WindowEvent;
*/ */
public class App extends Application { public class App extends Application {
private static final int DEFAULT_NUMBER_OF_DRAGONS = 2;
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
...@@ -32,10 +36,11 @@ public class App extends Application { ...@@ -32,10 +36,11 @@ public class App extends Application {
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false); primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 6th laboratory"); primaryStage.setTitle("Java 1");
primaryStage.show(); primaryStage.show();
controller = loader.getController(); controller = loader.getController();
controller.startGame(); Point2D[][] dragonsInit = getDragonsInit(scene.getWidth(), scene.getHeight());
controller.startGame(dragonsInit);
//Exit program when main window is closed //Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram); primaryStage.setOnCloseRequest(this::exitProgram);
} catch (Exception e) { } catch (Exception e) {
...@@ -45,6 +50,34 @@ public class App extends Application { ...@@ -45,6 +50,34 @@ public class App extends Application {
private Point2D[][] getDragonsInit(double width, double height) {
//ask whether would like to enter dragons
//return default
return getDefaultDragonsInit(width, height);
}
private Point2D[][] getDefaultDragonsInit(double width, double height) {
Random rnd = new Random();
Point2D[][] result = new Point2D[DEFAULT_NUMBER_OF_DRAGONS][];
for (int i = 0; i < result.length; i++) {
int x = rnd.nextInt((int) width);
int y = rnd.nextInt((int) height);
int vel_x = (rnd.nextInt(10) - 5) * 10;
int vel_y = (rnd.nextInt(10) - 5) * 10;
result[i] = new Point2D[2];
result[i][0] = new Point2D(x, y);
result[i][1] = new Point2D(vel_x, vel_y);
}
return result;
}
private void exitProgram(WindowEvent evt) { private void exitProgram(WindowEvent evt) {
controller.stopGame(); controller.stopGame();
System.exit(0); System.exit(0);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import javafx.animation.AnimationTimer; import javafx.animation.AnimationTimer;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
...@@ -31,8 +32,8 @@ public class GameController { ...@@ -31,8 +32,8 @@ public class GameController {
public GameController() { public GameController() {
} }
public void startGame() { public void startGame(Point2D[][] dragonInit) {
this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world = new World(canvas.getWidth(), canvas.getHeight(), dragonInit);
//Draw scene on a separate thread to avoid blocking UI. //Draw scene on a separate thread to avoid blocking UI.
animationTimer = new DrawingThread(canvas, world); animationTimer = new DrawingThread(canvas, world);
angleSlider.valueProperty().addListener(this::angleChanged); angleSlider.valueProperty().addListener(this::angleChanged);
......
package lab; package lab;
import java.util.Random;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public class World { public class World {
private final static int NUMBER_OF_DRAGONS = 2;
private double width; private double width;
private double height; private double height;
...@@ -20,22 +17,18 @@ public class World { ...@@ -20,22 +17,18 @@ public class World {
private DrawableSimulable []entities; private DrawableSimulable []entities;
public World(double width, double height) { public World(double width, double height, Point2D [][]dragonInit) {
super(); super();
this.width = width; this.width = width;
this.height = height; this.height = height;
Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
entities = new DrawableSimulable[2 + NUMBER_OF_DRAGONS]; entities = new DrawableSimulable[2 + dragonInit.length];
entities[0] = cannon; entities[0] = cannon;
entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40); entities[1] = 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++) { for (int i = 2; i < entities.length; i++) {
int x = rnd.nextInt((int) width);
int y = rnd.nextInt((int) height); entities[i] = new Dragon(this, dragonInit[i - 2][0], dragonInit[i - 2][1]);
int vel_x = (rnd.nextInt(10) - 5) * 10;
int vel_y = (rnd.nextInt(10) - 5) * 10;
entities[i] = new Dragon(this, new Point2D(x, y), new Point2D(vel_x, vel_y));
} }
} }
......
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