Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (4)
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>vsb-cs-java1</groupId> <groupId>vsb-cs-java1</groupId>
<artifactId>lab06</artifactId> <artifactId>efrei-lab03-scanner-nested</artifactId>
<version>0.0.1-SNAPHOST</version> <version>0.0.1-SNAPHOST</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
......
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);
} }
...@@ -29,13 +33,15 @@ public class App extends Application { ...@@ -29,13 +33,15 @@ public class App extends Application {
Scene scene = new Scene(root); Scene scene = new Scene(root);
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();
primaryStage.hide();
controller = loader.getController(); controller = loader.getController();
controller.startGame(); Point2D[][] dragonsInit = getDragonsInit(scene.getWidth(), scene.getHeight());
primaryStage.show();
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 +51,34 @@ public class App extends Application { ...@@ -45,6 +51,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));
} }
} }
......