Skip to content
Snippets Groups Projects
Commit e4537fa7 authored by jez04's avatar jez04
Browse files

feat: :tada: solution lab05

parent 83a8755f
No related merge requests found
Showing with 253 additions and 33 deletions
......@@ -2,3 +2,4 @@
.settings/
.project
.classpath
.idea/
package lab;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -16,6 +19,9 @@ import javafx.stage.WindowEvent;
public class App extends Application {
private GameController gameController;
private Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
......@@ -23,12 +29,8 @@ public class App extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Construct a main window with a canvas.
FXMLLoader gameLoader = new FXMLLoader(getClass().getResource("/lab/gameWindow.fxml"));
Parent root = gameLoader.load();
GameController gameController = gameLoader.getController();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
this.primaryStage = primaryStage;
switchToMenu();
primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 1th laboratory");
primaryStage.show();
......@@ -39,9 +41,34 @@ public class App extends Application {
}
}
public void switchToGame(String name, int numberOfMonsters) throws IOException {
// Construct a main window with a canvas.
FXMLLoader gameLoader = new FXMLLoader(getClass().getResource("/lab/gameWindow.fxml"));
Parent root = gameLoader.load();
gameController = gameLoader.getController();
Scene scene = new Scene(root);
URL cssUrl = getClass().getResource("application.css");
scene.getStylesheets().add(cssUrl.toString());
primaryStage.setScene(scene);
gameController.startGame(name, numberOfMonsters);
}
private void switchToMenu() throws IOException {
// Construct a main window with a canvas.
FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("/lab/mainScreen.fxml"));
Parent root = menuLoader.load();
MainScreenController menuController = menuLoader.getController();
menuController.setApp(this);
Scene scene = new Scene(root);
URL cssUrl = getClass().getResource("application.css");
scene.getStylesheets().add(cssUrl.toString());
primaryStage.setScene(scene);
}
@Override
public void stop() throws Exception {
gameController.stop();
if(gameController != null) {
gameController.stop();
}
super.stop();
}
......
package lab;
public enum Difficult {
EASY(2), MEDIUM(5), HARD(10);
private final int numberOfMonsters;
private Difficult(int numberOfMonsters) {
this.numberOfMonsters = numberOfMonsters;
}
public int getNumberOfMonsters() {
return numberOfMonsters;
}
}
......@@ -6,15 +6,13 @@ import javafx.scene.canvas.GraphicsContext;
public class DrawingThread extends AnimationTimer {
private final Canvas canvas;
private final GraphicsContext gc;
private Level level;
private long lastTime;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
public DrawingThread(Canvas canvas, Level level) {
this.level = level;
this.gc = canvas.getGraphicsContext2D();
this.level = new Level(canvas.getWidth(), canvas.getHeight());
lastTime = System.nanoTime();
}
......@@ -28,8 +26,4 @@ public class DrawingThread extends AnimationTimer {
lastTime = now;
}
public Level getLevel() {
return level;
}
}
package lab;
import javafx.animation.AnimationTimer;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
public class GameController {
private AnimationTimer timer;
private Level level;
@FXML
private Slider angle;
private DrawingThread timer;
@FXML
private Canvas canvas;
@FXML
private Slider speed;
@FXML
private Label playerName;
@FXML
void spawn(ActionEvent event) {
timer.getLevel().getPlayer().spawn();
level.getPlayer().spawn();
}
......@@ -32,26 +37,31 @@ public class GameController {
assert angle != null : "fx:id=\"angle\" was not injected: check your FXML file 'gameWindow.fxml'.";
assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'gameWindow.fxml'.";
assert speed != null : "fx:id=\"speed\" was not injected: check your FXML file 'gameWindow.fxml'.";
timer = new DrawingThread(canvas);
timer.start();
angle.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
timer.getLevel().getPlayer().setAngle(newValue.doubleValue());
level.getPlayer().setAngle(newValue.doubleValue());
}
});
speed.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
timer.getLevel().getPlayer().setSpeed(newValue.doubleValue());
level.getPlayer().setSpeed(newValue.doubleValue());
}
});
}
public void startGame(String name, int numberOfMonsters) {
playerName.setText(name);
level = new Level(canvas.getWidth(), canvas.getHeight(), numberOfMonsters);
timer = new DrawingThread(canvas, level);
timer.start();
}
public void stop() {
timer.stop();
}
}
......@@ -12,11 +12,11 @@ public class Level {
private DrawableSimulable[] entities;
private Player player;
public Level(double width, double height) {
public Level(double width, double height, int monsterCount) {
this.width = width;
this.height = height;
player = new Player(this, new Point2D(20, 250), new Point2D(100, -20));
entities = new DrawableSimulable[9];
entities = new DrawableSimulable[monsterCount+4];
entities[0] = new NicerObstacle(this, new Point2D(20, 150));
entities[1] = new Obstacle(this, new Point2D(300, 200), new Dimension2D(80, 40));
entities[2] = new Obstacle(this);
......
package lab;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
/**
*
*/
public class MainScreenController {
@FXML
private ToggleGroup difficult;
@FXML
private ToggleButton easy;
@FXML
private ToggleButton hard;
@FXML
private ToggleButton medium;
@FXML
private TextField name;
private App app;
@FXML
void play(ActionEvent event) throws IOException {
Difficult dif = (Difficult)difficult.getSelectedToggle().getProperties().get(Difficult.class);
app.switchToGame(name.getText(), dif.getNumberOfMonsters());
}
public void setApp(App app) {
this.app = app;
}
@FXML
void initialize() {
assert difficult != null : "fx:id=\"difficult\" was not injected: check your FXML file 'mainScreen.fxml'.";
assert name != null : "fx:id=\"name\" was not injected: check your FXML file 'mainScreen.fxml'.";
easy.getProperties().put(Difficult.class, Difficult.EASY);
medium.getProperties().put(Difficult.class, Difficult.MEDIUM);
hard.getProperties().put(Difficult.class, Difficult.HARD);
}
}
......@@ -38,6 +38,9 @@ public class Monster extends WorldEntity implements Collisionable {
public void simulate(double delay) {
position = position.add(speed.multiply(delay / 1_000_000_000));
position = new Point2D(position.getX(), position.getY() % level.getHeight());
if(position.getY() + image.getHeight() < 0) {
position = new Point2D(position.getX(), level.getHeight());
}
}
@Override
......
......@@ -12,7 +12,7 @@ import javafx.scene.transform.Rotate;
public class Player extends WorldEntity implements Collisionable {
private static final Random RANDOM = new Random();
private Point2D speed;
private double speedSize;
private double speedSize = 50;
private double angle;
public Player(Level level, Point2D position, Point2D speed) {
......
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
\ No newline at end of file
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
Label {
-fx-font-size: 20.0px;
-fx-padding: 10.0px;
-fx-text-fill: white;
-fx-font-weight: 900;
}
#name{
-fx-font-weight: 900;
-fx-font-size: 20.0px;
-fx-padding: 10.0px;
-fx-background-color: RGBA(255.0,0.0,0.0,0.5);
-fx-background-insets:5.0px;
}
#panel {
-fx-background-image:url("game-background.jpg");
-fx-background-size: stretch;
}
#menuPanel {
-fx-background-image:url("red-monster-menu-background.jpg");
-fx-background-size: stretch;
}
#playButton {
-fx-background-color: RGBA(255.0,0.0,0.0,0.5);
}
#playButton:hover {
-fx-background-color: RGBA(255.0,0.0,0.0,0.5);
-fx-text-fill: white;
}
.difficultButton .radio{
visibility: hidden;
}
.difficultButton .dot{
-fx-background-color: red;
}
.difficultButton {
-fx-background-color: RGBA(100.0,0.0,0.0,0.8);
-fx-border-color: RGBA(100.0,0.0,0.0,0.8);
-fx-border-width:3px;
-fx-border-radius:30.0px;
-fx-text-fill: white;
-fx-font-size: 20.0px;
-fx-background-radius:30.0px;
}
.difficultButton:hover{
-fx-effect: dropshadow(gaussian, rgba(255, 255, 0, 0.7), 50.0, 0.7, 0.0, 0.0);
-fx-background-color : RGBA(50,0,0,0.8);
}
.difficultButton:selected{
-fx-background-color : RGBA(100,0,0,0.9);
-fx-border-color:RGBA(255,255,0,0.9);
}
src/main/resources/lab/game-background.jpg

322 KiB

......@@ -4,17 +4,18 @@
<?import javafx.scene.Cursor?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="459.0" prefWidth="824.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController">
<BorderPane fx:id="panel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="459.0" prefWidth="824.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController">
<bottom>
<HBox alignment="TOP_CENTER" prefHeight="66.0" prefWidth="824.0" BorderPane.alignment="CENTER">
<children>
<Slider fx:id="speed" majorTickUnit="15.0" max="200.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" min="50.0" minorTickCount="5" prefHeight="68.0" prefWidth="736.0" showTickLabels="true" showTickMarks="true" HBox.hgrow="ALWAYS" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#spawn" prefHeight="46.0" prefWidth="147.0" style="-fx-background-color: blue;" text="Spawn" textAlignment="CENTER" textFill="WHITE" textOverrun="CLIP">
<Slider fx:id="speed" majorTickUnit="15.0" max="200.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" min="50.0" minorTickCount="5" prefHeight="68.0" prefWidth="736.0" showTickLabels="true" showTickMarks="true" value="50.0" HBox.hgrow="ALWAYS" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#spawn" prefHeight="46.0" prefWidth="147.0" style="-fx-background-image: url(&quot;https://swi.cs.vsb.cz/dam/jcr:95f56bef-2d5f-44f6-a091-9b81fa065128/LochNess.gif&quot;);" text="Spawn" textAlignment="CENTER" textFill="WHITE" textOverrun="CLIP">
<font>
<Font name="System Bold" size="22.0" />
</font>
......@@ -39,6 +40,9 @@
</Canvas>
</center>
<left>
<Slider fx:id="angle" blockIncrement="25.0" majorTickUnit="30.0" max="360.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minorTickCount="5" orientation="VERTICAL" showTickLabels="true" showTickMarks="true" BorderPane.alignment="CENTER" />
<Slider fx:id="angle" blockIncrement="25.0" majorTickUnit="30.0" max="360.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minorTickCount="5" orientation="VERTICAL" showTickLabels="true" showTickMarks="true" value="300.0" BorderPane.alignment="CENTER" />
</left>
<top>
<Label fx:id="playerName" text="Label" textAlignment="CENTER" BorderPane.alignment="CENTER" />
</top>
</BorderPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<BorderPane fx:id="menuPanel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.MainScreenController">
<top>
<HBox prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label text="Player name:" />
<TextField fx:id="name" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" promptText="type player name like Java Duke" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</top>
<bottom>
<Button fx:id="playButton" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#play" text="Play" BorderPane.alignment="CENTER">
<font>
<Font name="System Bold" size="51.0" />
</font></Button>
</bottom>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<RadioButton fx:id="easy" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" styleClass="difficultButton" text="Easy" HBox.hgrow="ALWAYS">
<toggleGroup>
<ToggleGroup fx:id="difficult" />
</toggleGroup>
<HBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</HBox.margin>
</RadioButton>
<RadioButton fx:id="medium" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" selected="true" styleClass="difficultButton" text="Medium" toggleGroup="$difficult" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</HBox.margin>
</RadioButton>
<RadioButton fx:id="hard" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" styleClass="difficultButton" text="Hard" toggleGroup="$difficult" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</HBox.margin>
</RadioButton>
</children>
</HBox>
</center>
</BorderPane>
src/main/resources/lab/red-monster-menu-background.jpg

125 KiB

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