diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java index 30f7cdbaafcc901c4f8e2f6421dc8b838329014a..c8deeffecfa583112be32435adb9fa8957c12e01 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 e3aef872f6bf27650890daadaa0618d580cde57f..02b9a3208dfb390973e21995e808951864d11bae 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 70c5a995165f4f220acae8443f0fb104ded7860e..d0248ce9c00ceb71d5bc80d5787d26bebb2a1ae7 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 d55ef2415049987e14fca2179f336cc8cbcd5e97..8266a75810c023d1514326c66b36e3fb66a67faa 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