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

feat: lab03 assigment

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