From 2f3f2b9a121fc8f3f1909bd44aa0ac788b33f167 Mon Sep 17 00:00:00 2001 From: jez04 <david.jezek@post.cz> Date: Mon, 7 Oct 2024 00:50:33 +0200 Subject: [PATCH] feat: add world ref --- src/main/java/lab/Bullet.java | 4 +++- src/main/java/lab/BulletAnimated.java | 4 +++- src/main/java/lab/Cannon.java | 12 +++++++++--- src/main/java/lab/World.java | 18 +++++++++--------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java index 30f7cdb..c8deeff 100644 --- a/src/main/java/lab/Bullet.java +++ b/src/main/java/lab/Bullet.java @@ -9,10 +9,12 @@ public class Bullet { private final Point2D acceleration; + private final World world; private Point2D position; private Point2D velocity; - public Bullet(Point2D position, Point2D velocity, Point2D acceleration) { + public Bullet(World world, Point2D position, Point2D velocity, Point2D acceleration) { + this.world = world; this.position = position; this.velocity = velocity; this.acceleration = acceleration; diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index e3aef87..02b9a32 100644 --- a/src/main/java/lab/BulletAnimated.java +++ b/src/main/java/lab/BulletAnimated.java @@ -7,9 +7,11 @@ import javafx.scene.image.Image; public class BulletAnimated { private static final double SIZE = 40; + private final World world; private Image image = new Image(this.getClass().getResourceAsStream("fireball-transparent.gif")); - public BulletAnimated(Point2D position, Point2D velocity, Point2D acceleration) { + public BulletAnimated(World world, Point2D position, Point2D velocity, Point2D acceleration) { + this.world = world; this.position = position; this.velocity = velocity; this.acceleration = acceleration; diff --git a/src/main/java/lab/Cannon.java b/src/main/java/lab/Cannon.java index 70c5a99..d0248ce 100644 --- a/src/main/java/lab/Cannon.java +++ b/src/main/java/lab/Cannon.java @@ -10,17 +10,20 @@ public class Cannon { private static final double LENGTH = 60; private static final double WIDTH = 15; + private final World world; private Point2D position; private double angle; + private double angleDelta = 25; - public Cannon(Point2D position, double angle) { + public Cannon(World world, Point2D position, double angle) { + this.world = world; this.position = position; this.angle = angle; } public void draw(GraphicsContext gc) { gc.save(); - gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY()))); + gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY() + WIDTH / 2))); gc.setFill(Color.BROWN); gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH); gc.restore(); @@ -28,6 +31,9 @@ public class Cannon { public void simulate(double deltaT) { // do nothing yet - angle += 25 * deltaT; + angle += angleDelta * deltaT; + if (angle >= 90 || angle <= 0) { + angleDelta = -angleDelta; + } } } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index d55ef24..8266a75 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -16,9 +16,9 @@ public class World { public World(double width, double hight) { this.width = width; this.hight = hight; - this.bullet = new Bullet(new Point2D(0, 0), new Point2D(30, 30), new Point2D(0, -9.81)); - this.bullet2 = new BulletAnimated(new Point2D(0, 0), new Point2D(30, 70), new Point2D(0, -9.81)); - this.cannon = new Cannon(new Point2D(0, 0), 45); + bullet = new Bullet(this, new Point2D(0, 0), new Point2D(30, 30), new Point2D(0, -9.81)); + bullet2 = new BulletAnimated(this, new Point2D(0, 0), new Point2D(30, 70), new Point2D(0, -9.81)); + cannon = new Cannon(this, new Point2D(0, 0), 45); } public void draw(GraphicsContext gc) { @@ -28,15 +28,15 @@ public class World { // Change coordinate system to human like gc.scale(1, -1); gc.translate(0, -hight); - this.bullet.draw(gc); - this.bullet2.draw(gc); - this.cannon.draw(gc); + bullet.draw(gc); + bullet2.draw(gc); + cannon.draw(gc); gc.restore(); } public void simulate(double deltaT) { - this.bullet.simulate(deltaT); - this.bullet2.simulate(deltaT); - this.cannon.simulate(deltaT); + bullet.simulate(deltaT); + bullet2.simulate(deltaT); + cannon.simulate(deltaT); } } \ No newline at end of file -- GitLab