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

feat: lab03 assigment

parent 3fe2e190
No related merge requests found
Showing
with 81 additions and 82 deletions
...@@ -6,6 +6,7 @@ import javafx.geometry.Point2D; ...@@ -6,6 +6,7 @@ import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D; import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import lab.Setting;
public class Ufo extends WorldEntity implements Collisionable { public class Ufo extends WorldEntity implements Collisionable {
...@@ -14,8 +15,11 @@ public class Ufo extends WorldEntity implements Collisionable { ...@@ -14,8 +15,11 @@ public class Ufo extends WorldEntity implements Collisionable {
private Point2D velocity; private Point2D velocity;
public Ufo(World world) { public Ufo(World world) {
this(world, new Point2D(RANDOM.nextDouble(world.getWidth()), RANDOM.nextDouble(0, world.getHeight() * 0.3)), this(world,
new Point2D(RANDOM.nextDouble(70, 150), 0)); new Point2D(RANDOM.nextDouble(world.getWidth()),
RANDOM.nextDouble(0, world.getHeight() * Setting.getInstance().getUfoMinPercentageHeight())),
new Point2D(RANDOM.nextDouble(Setting.getInstance().getUfoMinSpeed(),
Setting.getInstance().getUfoMaxSpeed()), 0));
} }
public Ufo(World world, Point2D position, Point2D velocity) { public Ufo(World world, Point2D position, Point2D velocity) {
......
...@@ -2,24 +2,24 @@ package lab.game; ...@@ -2,24 +2,24 @@ package lab.game;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import lab.Setting;
public class World { public class World {
public static final Point2D GRAVITY = new Point2D(0, 9.81); public static final Point2D GRAVITY = new Point2D(0, Setting.getInstance().getGravity());
private final double width; private final double width;
private final double height; private final double height;
private List<DrawableSimulable> entities; private List<DrawableSimulable> entities;
private Collection<DrawableSimulable> entitiesToRemove = new LinkedList<DrawableSimulable>(); private Collection<DrawableSimulable> entitiesToRemove = new LinkedList<>();
private Collection<DrawableSimulable> entitiesToAdd = new LinkedList<DrawableSimulable>(); private Collection<DrawableSimulable> entitiesToAdd = new LinkedList<>();
private Cannon cannon; private Cannon cannon;
// private BulletAnimated bulletAnimated; // private BulletAnimated bulletAnimated;
...@@ -28,12 +28,11 @@ public class World { ...@@ -28,12 +28,11 @@ public class World {
this.height = height; this.height = height;
entities = new ArrayList<>(); entities = new ArrayList<>();
cannon = new Cannon(this, new Point2D(0, height - 20), -45); cannon = new Cannon(this, new Point2D(0, height - 20), -45);
// bulletAnimated = new BulletAnimated(this, cannon, new Point2D(0, height), new Point2D(50, -80),
// GRAVITY);
entities.add(cannon); entities.add(cannon);
entities.add(new Bullet(this, new Point2D(0, height), new Point2D(30, -30), new Point2D(0, 9.81))); entities.add(new Bullet(this, new Point2D(0, height),
// entities.add(bulletAnimated); new Point2D(Setting.getInstance().getNormalBulletSpeed(), -Setting.getInstance().getNormalBulletSpeed()),
for (int i = 0; i < 3; i++) { GRAVITY));
for (int i = 0; i < Setting.getInstance().getNumberOfUfos(); i++) {
entities.add(new Ufo(this)); entities.add(new Ufo(this));
} }
} }
...@@ -73,13 +72,14 @@ public class World { ...@@ -73,13 +72,14 @@ public class World {
public double getWidth() { public double getWidth() {
return width; return width;
} }
public void add(DrawableSimulable entity) { public void add(DrawableSimulable entity) {
entitiesToAdd.add(entity); entitiesToAdd.add(entity);
} }
public void remove(DrawableSimulable entity) { public void remove(DrawableSimulable entity) {
entitiesToRemove.add(entity); entitiesToRemove.add(entity);
} }
public double getHeight() { public double getHeight() {
......
...@@ -8,7 +8,7 @@ public abstract class WorldEntity implements DrawableSimulable{ ...@@ -8,7 +8,7 @@ public abstract class WorldEntity implements DrawableSimulable{
protected final World world; protected final World world;
protected Point2D position; protected Point2D position;
public WorldEntity(World world, Point2D position) { protected WorldEntity(World world, Point2D position) {
this.world = world; this.world = world;
this.position = position; this.position = position;
} }
......
...@@ -6,6 +6,7 @@ import javafx.scene.Parent; ...@@ -6,6 +6,7 @@ import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.WindowEvent; import javafx.stage.WindowEvent;
import lab.Setting;
/** /**
* Class <b>App</b> - extends class Application and it is an entry point of the * Class <b>App</b> - extends class Application and it is an entry point of the
...@@ -17,6 +18,7 @@ public class App extends Application { ...@@ -17,6 +18,7 @@ public class App extends Application {
private GameController gameController; private GameController gameController;
public static void main(String[] args) { public static void main(String[] args) {
Setting.configure(new Setting());
launch(args); launch(args);
} }
......
...@@ -2,7 +2,6 @@ package lab.gui; ...@@ -2,7 +2,6 @@ package lab.gui;
import java.util.List; import java.util.List;
import cz.vsb.fei.java2.lab02.common.Score;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
...@@ -13,21 +12,22 @@ import javafx.scene.control.Slider; ...@@ -13,21 +12,22 @@ import javafx.scene.control.Slider;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.PropertyValueFactory;
import lab.ScoreStorageFactory; import lab.Setting;
import lab.data.Score;
import lab.game.BulletAnimated; import lab.game.BulletAnimated;
import lab.game.DrawingThread; import lab.game.DrawingThread;
import lab.game.World; import lab.game.World;
public class GameController { public class GameController {
@FXML @FXML
private Button btnGenerateScore; private Button btnGenerateScore;
@FXML @FXML
private Button btnLoadAll; private Button btnLoadAll;
@FXML @FXML
private Button btnLoadFirstTen; private Button btnLoadFirstTen;
@FXML @FXML
private Slider angle; private Slider angle;
...@@ -39,60 +39,54 @@ public class GameController { ...@@ -39,60 +39,54 @@ public class GameController {
private Canvas canvas; private Canvas canvas;
@FXML @FXML
private TableView<Score> scores; private TableView<Score> scores;
@FXML @FXML
private TableColumn<Score, String> nickColumn; private TableColumn<Score, String> nickColumn;
@FXML
private TableColumn<Score, Integer> pointsColumn;
@FXML
private TableColumn<Score, Integer> pointsColumn;
private DrawingThread timer; private DrawingThread timer;
@FXML @FXML
private Label hits; private Label hits;
private int hitcount = 0; private int hitcount = 0;
@FXML @FXML
void fire(ActionEvent event) { void fire(ActionEvent event) {
double angle = timer.getWorld().getCannon().getAngle(); double angle = timer.getWorld().getCannon().getAngle();
double angleRad = Math.toRadians(angle); double angleRad = Math.toRadians(angle);
double speedValue = speed.getValue(); double speedValue = speed.getValue();
Point2D velocity = new Point2D( Point2D velocity = new Point2D(Math.cos(angleRad) * speedValue, Math.sin(angleRad) * speedValue);
Math.cos(angleRad)*speedValue, BulletAnimated bulletAnimated = new BulletAnimated(timer.getWorld(), timer.getWorld().getCannon(),
Math.sin(angleRad)*speedValue); timer.getWorld().getCannon().getPosition(), velocity, World.GRAVITY);
BulletAnimated bulletAnimated = new BulletAnimated(
timer.getWorld(),
timer.getWorld().getCannon(),
timer.getWorld().getCannon().getPosition(),
velocity, World.GRAVITY);
timer.getWorld().add(bulletAnimated); timer.getWorld().add(bulletAnimated);
bulletAnimated.addHitListener(this::increaseHits); bulletAnimated.addHitListener(this::increaseHits);
bulletAnimated.addHitListener( bulletAnimated.addHitListener(() -> System.out.println("au!!!!"));
() -> System.out.println("au!!!!"));
} }
@FXML @FXML
void btnGenerateScoreAction(ActionEvent event) { void btnGenerateScoreAction(ActionEvent event) {
Score score = Score.generate(); Score score = Score.generate();
this.scores.getItems().add(score); this.scores.getItems().add(score);
ScoreStorageFactory.getInstance().insertScore(score); Setting.getInstance().getScoreStorageInterface().insertScore(score);
} }
@FXML @FXML
void btnLoadAllAction(ActionEvent event) { void btnLoadAllAction(ActionEvent event) {
updateScoreTable(ScoreStorageFactory.getInstance().getAll()); updateScoreTable(Setting.getInstance().getScoreStorageInterface().getAll());
} }
@FXML @FXML
void btnLoadFirstTenAction(ActionEvent event) { void btnLoadFirstTenAction(ActionEvent event) {
updateScoreTable(ScoreStorageFactory.getInstance().getFirstTen()); updateScoreTable(Setting.getInstance().getScoreStorageInterface().getFirstTen());
} }
private void updateScoreTable(List<Score> scores) { private void updateScoreTable(List<Score> scores) {
this.scores.getItems().clear(); this.scores.getItems().clear();
this.scores.getItems().addAll(scores); this.scores.getItems().addAll(scores);
} }
private void updateHits() { private void updateHits() {
hits.setText(String.format("Hit count: %03d", hitcount)); hits.setText(String.format("Hit count: %03d", hitcount));
...@@ -102,27 +96,28 @@ public class GameController { ...@@ -102,27 +96,28 @@ public class GameController {
hitcount++; hitcount++;
updateHits(); updateHits();
} }
@FXML @FXML
void initialize() { void initialize() {
assert angle != null : "fx:id=\"angle\" was not injected: check your FXML file 'gameWindow.fxml'."; 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 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'."; assert speed != null : "fx:id=\"speed\" was not injected: check your FXML file 'gameWindow.fxml'.";
speed.setMin(Setting.getInstance().getBulletMinSpeed());
speed.setMax(Setting.getInstance().getBulletMaxSpeed());
timer = new DrawingThread(canvas); timer = new DrawingThread(canvas);
timer.start(); timer.start();
angle.valueProperty().addListener( angle.valueProperty().addListener(
(observable, oldValue, newValue) -> (observable, oldValue, newValue) -> timer.getWorld().getCannon().setAngle(newValue.doubleValue()));
timer.getWorld().getCannon().setAngle(newValue.doubleValue()));
nickColumn.setCellValueFactory(new PropertyValueFactory<>("name")); nickColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
pointsColumn.setCellValueFactory(new PropertyValueFactory<>("points")); pointsColumn.setCellValueFactory(new PropertyValueFactory<>("points"));
initStorage(); initStorage();
} }
private void initStorage() { private void initStorage() {
ScoreStorageFactory.getInstance().init(); Setting.getInstance().getScoreStorageInterface().init();
scores.getItems().addAll(ScoreStorageFactory.getInstance().getAll()); scores.getItems().addAll(Setting.getInstance().getScoreStorageInterface().getAll());
} }
public void stop() { public void stop() {
......
package cz.vsb.fei.java2.lab02.db; package lab.storage;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -9,8 +9,7 @@ import java.sql.Statement; ...@@ -9,8 +9,7 @@ import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cz.vsb.fei.java2.lab02.common.Score; import lab.data.Score;
import cz.vsb.fei.java2.lab02.common.ScoreStorageInterface;
public class DbConnector implements ScoreStorageInterface { public class DbConnector implements ScoreStorageInterface {
......
package cz.vsb.fei.file; package lab.storage;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
...@@ -10,8 +10,7 @@ import java.util.Comparator; ...@@ -10,8 +10,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import cz.vsb.fei.java2.lab02.common.Score; import lab.data.Score;
import cz.vsb.fei.java2.lab02.common.ScoreStorageInterface;
public class FileStorage implements ScoreStorageInterface { public class FileStorage implements ScoreStorageInterface {
......
package cz.vsb.fei.java2.lab02.common; package lab.storage;
import java.util.List; import java.util.List;
import lab.data.Score;
public interface ScoreStorageInterface { public interface ScoreStorageInterface {
List<Score> getAll(); List<Score> getAll();
......
import cz.vsb.fei.java2.lab02.common.ScoreStorageInterface; module cz.vsb.fei.java2.lab03_module {
module cz.vsb.fei.java2.lab02_module {
requires transitive javafx.controls; requires transitive javafx.controls;
requires javafx.fxml; requires javafx.fxml;
requires javafx.base; requires javafx.base;
requires cz.vsb.fei.java2.lab02.common_module; requires java.sql;
opens lab.gui to javafx.fxml; opens lab.gui to javafx.fxml;
opens lab.data to javafx.base;
exports lab.gui to javafx.fxml,javafx.graphics; exports lab.gui to javafx.fxml,javafx.graphics;
uses ScoreStorageInterface;
} }
\ No newline at end of file
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