From cf4ad40e395b9157e7a5611cfef334024728b660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Fri, 20 Oct 2023 10:08:47 +0200 Subject: [PATCH] Prepare for UI --- src/main/java/lab/BulletAnimated.java | 17 +++++++++++++++-- src/main/java/lab/Cannon.java | 12 +++++++++--- src/main/java/lab/GameController.java | 5 +++++ src/main/java/lab/World.java | 25 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java index b778b08..0340e67 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 eb3f789..8e73a1b 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 5385570..b36a006 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 dd53bc3..d930588 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(); + } + } + } + } -- GitLab