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

feat: assignment lab09

parent 9fd2f101
No related merge requests found
Pipeline #2845 failed with stages
......@@ -3,10 +3,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.vsb.fei.java2</groupId>
<artifactId>java2-lab05-v1</artifactId>
<artifactId>java2-lab09-v1</artifactId>
<version>0.0.1-SNAPHOST</version>
<name>java2-lab05-v1</name>
<name>java2-lab09-v1</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
NetworkNinja;122
VirtualVoyager;198
TweetTrendsetter;109
WebWizard;262
ScreenSage;95
CodeCommander;276
FunctionFanatic;288
DataDetective;112
ScreenSage;181
PixelArtist;242
RenderRogue;62
siast;101
ByteBishop;53
PixelProdigy;196
PixelArtist;70
AppAlchemist;106
ScriptSensei;283
......@@ -27,7 +27,7 @@ public class Setting {
@Default
private double normalBulletSpeed = 30;
@Default
private int numberOfUfos = 3;
private int numberOfUfos = 10;
@Default
private double ufoMinPercentageHeight = 0.3;
@Default
......
package lab.game;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Bullet extends WorldEntity implements Collisionable{
private static final double SIZE = 20;
protected final Point2D acceleration;
protected Point2D velocity;
protected final MyPoint acceleration;
protected MyPoint velocity;
public Bullet(World world, Point2D position, Point2D velocity, Point2D acceleration) {
public Bullet(World world, MyPoint position, MyPoint velocity, MyPoint acceleration) {
super(world, position);
this.velocity = velocity;
this.acceleration = acceleration;
......@@ -44,7 +44,7 @@ public class Bullet extends WorldEntity implements Collisionable{
}
public void setVelocity(Point2D velocity) {
public void setVelocity(MyPoint velocity) {
this.velocity = velocity;
}
......
......@@ -3,23 +3,19 @@ package lab.game;
import java.util.ArrayList;
import java.util.List;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated extends Bullet {
private static final double SIZE = 40;
private final Point2D initVelocity;
private Cannon cannon;
private static Image image = new Image(BulletAnimated.class.getResourceAsStream("fireball-transparent.gif"));
private List<HitListener> hitListeners = new ArrayList<>();
public BulletAnimated(World world, Cannon cannon, Point2D position, Point2D velocity, Point2D acceleration) {
public BulletAnimated(World world, Cannon cannon, MyPoint position, MyPoint velocity, MyPoint acceleration) {
super(world, position, velocity, acceleration);
this.initVelocity = velocity;
this.cannon = cannon;
}
......@@ -39,7 +35,7 @@ public class BulletAnimated extends Bullet {
public void reload() {
position = cannon.getPosition();
velocity = new Point2D(0, 0);
velocity = new MyPoint(0, 0);
}
public boolean addHitListener(HitListener e) {
......
package lab.game;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Transform;
public class Cannon extends WorldEntity{
public class Cannon extends WorldEntity {
private static final double LENGTH = 60;
private static final double WIDTH = 15;
private double angle;
private double angleDelta = -25;
public Cannon(World world, Point2D position, double angle) {
public Cannon(World world, MyPoint position, double angle) {
super(world, position);
this.angle = angle;
}
......@@ -27,10 +25,7 @@ public class Cannon extends WorldEntity{
@Override
public void simulate(double deltaT) {
// angle += angleDelta * deltaT;
// if (angle >= 0 || angle <= -90) {
// angleDelta = -angleDelta;
// }
/* nothing to do */
}
public double getAngle() {
......@@ -40,5 +35,5 @@ public class Cannon extends WorldEntity{
public void setAngle(double angle) {
this.angle = -angle;
}
}
......@@ -6,13 +6,11 @@ import javafx.scene.canvas.GraphicsContext;
public class DrawingThread extends AnimationTimer {
private final Canvas canvas;
private final GraphicsContext gc;
private final World world;
private long lastTime;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
world = new World(canvas.getWidth(), canvas.getHeight());
lastTime = System.nanoTime();
......
package lab.game;
import java.io.Serial;
import java.io.Serializable;
public class MyPoint implements Serializable {
@Serial
private static final long serialVersionUID = 5131948780897974323L;
public double x;
public double y;
public MyPoint(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public MyPoint multiply(double multiplier) {
return new MyPoint(x * multiplier, y * multiplier);
}
public MyPoint add(MyPoint other) {
return new MyPoint(x + other.x, y + other.y);
}
}
......@@ -6,7 +6,6 @@ import java.util.Random;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
......@@ -17,23 +16,23 @@ public class Ufo extends WorldEntity implements Collisionable {
private static Logger log = LogManager.getLogger(Ufo.class);
private static final Random RANDOM = new Random();
private Image image;
private Point2D velocity;
private static Image image;
private MyPoint velocity;
public Ufo(World world) {
this(world,
new Point2D(RANDOM.nextDouble(world.getWidth()),
new MyPoint(RANDOM.nextDouble(world.getWidth()),
RANDOM.nextDouble(0, world.getHeight() * Setting.getInstance().getUfoMinPercentageHeight())),
new Point2D(RANDOM.nextDouble(Setting.getInstance().getUfoMinSpeed(),
new MyPoint(RANDOM.nextDouble(Setting.getInstance().getUfoMinSpeed(),
Setting.getInstance().getUfoMaxSpeed()), 0));
}
public Ufo(World world, Point2D position, Point2D velocity) {
public Ufo(World world, MyPoint position, MyPoint velocity) {
super(world, position);
this.velocity = velocity;
}
private Image getImage() {
private static Image getImage() {
if (image == null) {
image = new Image(Ufo.class.getResourceAsStream("ufo-small.gif"));
}
......@@ -54,9 +53,9 @@ public class Ufo extends WorldEntity implements Collisionable {
@Override
public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT));
position = new Point2D(position.getX() % world.getWidth(), position.getY());
position = new MyPoint(position.getX() % world.getWidth(), position.getY());
if (position.getX() < -getImage().getWidth()) {
position = new Point2D(world.getWidth(), position.getY());
position = new MyPoint(world.getWidth(), position.getY());
}
log.trace("Ufo position: {}", position);
}
......@@ -80,6 +79,6 @@ public class Ufo extends WorldEntity implements Collisionable {
}
}
record UfoDestroyLog(LocalDateTime time, Point2D position) {}
record UfoDestroyLog(LocalDateTime time, MyPoint position) {}
}
......@@ -5,7 +5,6 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import lab.Setting;
import lab.game.Ufo.UfoDestroyLog;
......@@ -14,7 +13,7 @@ import lombok.extern.log4j.Log4j2;
@Log4j2
public class World {
public static final Point2D GRAVITY = new Point2D(0, Setting.getInstance().getGravity());
public static final MyPoint GRAVITY = new MyPoint(0, Setting.getInstance().getGravity());
private final double width;
private final double height;
......@@ -24,24 +23,40 @@ public class World {
private Collection<DrawableSimulable> entitiesToAdd = new LinkedList<>();
private List<UfoDestroyLog> destroyLogs = new LinkedList<>();
private boolean viewMode;
private Cannon cannon;
// private BulletAnimated bulletAnimated;
public World(double width, double height) {
this.width = width;
this.height = height;
entities = new ArrayList<>();
cannon = new Cannon(this, new Point2D(0, height - 20), -45);
cannon = new Cannon(this, new MyPoint(0, height - 20), -45);
entities.add(cannon);
entities.add(new Bullet(this, new Point2D(0, height),
new Point2D(Setting.getInstance().getNormalBulletSpeed(), -Setting.getInstance().getNormalBulletSpeed()),
GRAVITY));
entities.add(new Bullet(this, new MyPoint(0, height), new MyPoint(Setting.getInstance().getNormalBulletSpeed(),
-Setting.getInstance().getNormalBulletSpeed()), GRAVITY));
for (int i = 0; i < Setting.getInstance().getNumberOfUfos(); i++) {
entities.add(new Ufo(this));
}
}
public void setViewMode(boolean viewMode) {
this.viewMode = viewMode;
if (!viewMode) {
startServer();
} else {
connectToServer();
}
}
public void startServer() {
// TODO
}
public void connectToServer() {
// TODO
}
public void add(UfoDestroyLog ufoDestroyLog) {
destroyLogs.add(ufoDestroyLog);
}
......@@ -63,25 +78,30 @@ public class World {
}
public void simulate(double deltaT) {
if (viewMode) {
return;
}
for (DrawableSimulable entity : entities) {
entity.simulate(deltaT);
}
detectCollision();
entities.removeAll(entitiesToRemove);
entities.addAll(entitiesToAdd);
entitiesToAdd.clear();
entitiesToRemove.clear();
}
private void detectCollision() {
for (int i = 0; i < entities.size(); i++) {
if (entities.get(i) instanceof Collisionable c1) {
for (int j = i + 1; j < entities.size(); j++) {
if (entities.get(j) instanceof Collisionable c2) {
if (c1.intersect(c2.getBoundingBox())) {
c1.hitBy(c2);
c2.hitBy(c1);
}
if (entities.get(j) instanceof Collisionable c2 && c1.intersect(c2.getBoundingBox())) {
c1.hitBy(c2);
c2.hitBy(c1);
}
}
}
}
entities.removeAll(entitiesToRemove);
entities.addAll(entitiesToAdd);
entitiesToAdd.clear();
entitiesToRemove.clear();
}
public double getWidth() {
......@@ -89,10 +109,16 @@ public class World {
}
public void add(DrawableSimulable entity) {
if (viewMode) {
return;
}
entitiesToAdd.add(entity);
}
public void remove(DrawableSimulable entity) {
if (viewMode) {
return;
}
entitiesToRemove.add(entity);
}
......@@ -104,9 +130,4 @@ public class World {
public Cannon getCannon() {
return cannon;
}
// public BulletAnimated getBulletAnimated() {
// return bulletAnimated;
// }
//
}
\ No newline at end of file
package lab.game;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public abstract class WorldEntity implements DrawableSimulable{
public abstract class WorldEntity implements DrawableSimulable {
protected final World world;
protected Point2D position;
protected MyPoint position;
protected WorldEntity(World world, Point2D position) {
protected WorldEntity(World world, MyPoint position) {
this.world = world;
this.position = position;
}
......@@ -19,12 +18,11 @@ public abstract class WorldEntity implements DrawableSimulable{
drawInternal(gc);
gc.restore();
}
public abstract void drawInternal(GraphicsContext gc);
public Point2D getPosition() {
public MyPoint getPosition() {
return position;
}
}
package lab.gui;
import java.sql.SQLException;
import org.h2.tools.Server;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import lab.Setting;
import lab.storage.JpaConnector;
import lab.storage.FileStorage;
import lombok.extern.log4j.Log4j2;
/**
......@@ -25,33 +24,25 @@ public class App extends Application {
private GameController gameController;
private boolean viewMode;
public static void main(String[] args) {
log.info("Application lauched");
Setting.configure(Setting.builder().scoreStorageInterface(new JpaConnector()).build());
startH2WebServerToInspectDb();
Setting.configure(Setting.builder().scoreStorageInterface(new FileStorage()).build());
launch(args);
}
private static void startH2WebServerToInspectDb() {
//Start HTTP server for access H2 DB for look inside
try {
Server server = Server.createWebServer();
log.info(server.getURL());
server.start();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void start(Stage primaryStage) {
try {
// Construct a main window with a canvas.
Alert alert = new Alert(AlertType.CONFIRMATION, "View Mode?", ButtonType.YES, ButtonType.NO);
alert.showAndWait().ifPresent(button -> viewMode = ButtonType.YES.equals(button));
FXMLLoader gameLoader = new FXMLLoader(getClass().getResource("/lab/gui/gameWindow.fxml"));
Parent root = gameLoader.load();
gameController = gameLoader.getController();
gameController.setViewMode(viewMode);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Java 2 - 2nd laboratory");
......
......@@ -9,7 +9,6 @@ import org.apache.logging.log4j.Logger;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
......@@ -25,6 +24,7 @@ import lab.data.Level;
import lab.data.Score;
import lab.game.BulletAnimated;
import lab.game.DrawingThread;
import lab.game.MyPoint;
import lab.game.World;
public class GameController {
......@@ -72,10 +72,10 @@ public class GameController {
@FXML
void fire(ActionEvent event) {
double angle = timer.getWorld().getCannon().getAngle();
double angleRad = Math.toRadians(angle);
double angleValue = timer.getWorld().getCannon().getAngle();
double angleRad = Math.toRadians(angleValue);
double speedValue = speed.getValue();
Point2D velocity = new Point2D(Math.cos(angleRad) * speedValue, Math.sin(angleRad) * speedValue);
MyPoint velocity = new MyPoint(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);
......@@ -120,7 +120,7 @@ public class GameController {
@FXML
void keyReleased(KeyEvent event) {
/*nothing to do*/
}
private void updateScoreTable(List<Score> scores) {
......@@ -164,6 +164,11 @@ public class GameController {
canvas.requestFocus();
}
public void setViewMode(boolean viewMode) {
timer.getWorld().setViewMode(viewMode);
}
private void initStorage() {
Setting.getInstance().getScoreStorageInterface().init();
scores.getItems().addAll(Setting.getInstance().getScoreStorageInterface().getAll());
......
......@@ -31,6 +31,7 @@ public class JpaConnector implements ScoreStorageInterface {
@Override
public void init() {
/*nothing to do*/
}
@Override
......
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