diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index b778b081eeefd8ff848ed3eda0a07757d39df95c..0340e675b26ec6711d33219722cca4118043b84b 100644 --- a/src/main/java/lab/BulletAnimated.java +++ b/src/main/java/lab/BulletAnimated.java @@ -15,7 +15,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ private double mass = 2; private double strenghtOfCannon = 100; private double cannonLength = 100; - private boolean accelerate = true; + private boolean accelerate = false; private boolean hitToGround = false; private double crossSectionalArea; @@ -49,6 +49,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ } public void simulate(double deltaT) { + if (!accelerate && start == position) { + return; + } if (accelerate && start.distance(position) < cannonLength) { double cannonAngle = cannon.getAngle(); speed = speed @@ -70,7 +73,8 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ position = new Point2D(position.getX(), size / 2); } } else { - reload(); + fire(); + } } @@ -90,6 +94,15 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ position = start; speed = initialSpeed; hitToGround = false; + } + + public void setCanonPower(double value) { + strenghtOfCannon = value; + + } + + public void fire() { + reload(); accelerate = true; } diff --git a/src/main/java/lab/Cannon.java b/src/main/java/lab/Cannon.java index eb3f7894ff90af890ddf6ff1fda29560ed9e6ae1..8e73a1bc348509110d2345ae831d6ff1610a2b38 100644 --- a/src/main/java/lab/Cannon.java +++ b/src/main/java/lab/Cannon.java @@ -7,7 +7,7 @@ import javafx.scene.transform.Affine; public class Cannon implements DrawableSimulable { - private int direction=-1; + //private int direction=-1; private double angle = 0; private Point2D position; private Point2D size; @@ -24,12 +24,13 @@ public class Cannon implements DrawableSimulable { } public void simulate(double timeStep) { - angle = angle + direction*0.8; + /*angle = angle + direction*0.8; if(angle <=-90 || angle >= 0) { direction*=-1; - } + }*/ } + public void draw(GraphicsContext gc) { gc.save(); Point2D worldPosition = world.getCanvasPoint(position); @@ -46,4 +47,9 @@ public class Cannon implements DrawableSimulable { public double getAngle() { return (angle * -1) / 180 * Math.PI; } + + public void setAngle(double value) { + angle = value; + + } } diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 538557076a8345ca2c92ffc8fcdf4b450c7aebc8..b36a006721505f9b0951a13fb79c1f1e11799af3 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -18,6 +18,11 @@ public class GameController { //Draw scene on a separate thread to avoid blocking UI. animationTimer = new DrawingThread(canvas, world); animationTimer.start(); + + + world.setCannonAngle(-45d); + world.setCannonPower(100.); + world.fireBullet(); } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index dd53bc3857b678cb9c898b7cfccc58b6978fe10b..d9305889eca9750c629f52b66189bebf8b8ed9b0 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -78,4 +78,29 @@ public class World { this.height = height; } + public void setCannonAngle(double value) { + for (DrawableSimulable entity: entities) { + if (entity instanceof Cannon cannon) { + cannon.setAngle(value); + } + } + + } + + public void setCannonPower(double value) { + for (DrawableSimulable entity: entities) { + if (entity instanceof BulletAnimated bullet) { + bullet.setCanonPower(value); + } + } + } + + public void fireBullet() { + for (DrawableSimulable entity: entities) { + if (entity instanceof BulletAnimated bullet) { + bullet.fire(); + } + } + } + }