Skip to content
Snippets Groups Projects
Commit dfcc0b20 authored by koz01's avatar koz01
Browse files

Handling events from controls.

parent 758815af
Branches 2022_Wed_9-00
No related merge requests found
Pipeline #74 failed with stages
in 0 seconds
...@@ -15,7 +15,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ ...@@ -15,7 +15,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
private double mass = 2; private double mass = 2;
private double strenghtOfCannon = 100; private double strenghtOfCannon = 100;
private double cannonLength = 100; private double cannonLength = 100;
private boolean accelerate = true; private boolean accelerate;
private boolean hitToGround = false; private boolean hitToGround = false;
private double crossSectionalArea; private double crossSectionalArea;
...@@ -49,6 +49,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ ...@@ -49,6 +49,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
} }
public void simulate(double deltaT) { public void simulate(double deltaT) {
if (!accelerate && position == start) {
return;
}
if (accelerate && start.distance(position) < cannonLength) { if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle(); double cannonAngle = cannon.getAngle();
speed = speed speed = speed
...@@ -90,7 +93,16 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ ...@@ -90,7 +93,16 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
position = start; position = start;
speed = initialSpeed; speed = initialSpeed;
hitToGround = false; hitToGround = false;
}
public void fire() {
reload();
accelerate = true; accelerate = true;
} }
public void setForce(double doubleValue) {
strenghtOfCannon = doubleValue;
}
} }
...@@ -7,7 +7,6 @@ import javafx.scene.transform.Affine; ...@@ -7,7 +7,6 @@ import javafx.scene.transform.Affine;
public class Cannon implements DrawableSimulable { public class Cannon implements DrawableSimulable {
private int direction=-1;
private double angle = 0; private double angle = 0;
private Point2D position; private Point2D position;
private Point2D size; private Point2D size;
...@@ -24,10 +23,7 @@ public class Cannon implements DrawableSimulable { ...@@ -24,10 +23,7 @@ public class Cannon implements DrawableSimulable {
} }
public void simulate(double timeStep) { public void simulate(double timeStep) {
angle = angle + direction*0.8;
if(angle <=-90 || angle >= 0) {
direction*=-1;
}
} }
public void draw(GraphicsContext gc) { public void draw(GraphicsContext gc) {
...@@ -37,13 +33,18 @@ public class Cannon implements DrawableSimulable { ...@@ -37,13 +33,18 @@ public class Cannon implements DrawableSimulable {
gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2); gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2);
gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40); gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40);
gc.fillOval(worldPosition.getX()+size.getX(), worldPosition.getY()+size.getY()/2, 40, 40); gc.fillOval(worldPosition.getX()+size.getX(), worldPosition.getY()+size.getY()/2, 40, 40);
gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2))); gc.setTransform(new Affine(Affine.rotate(-angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2)));
gc.setFill(Color.BLACK); gc.setFill(Color.BLACK);
gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY()); gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY());
gc.restore(); gc.restore();
} }
public double getAngle() { public double getAngle() {
return (angle * -1) / 180 * Math.PI; return angle / 180 * Math.PI;
}
public void setAngle(double newValue) {
angle = newValue;
} }
} }
...@@ -29,7 +29,18 @@ public class GameController { ...@@ -29,7 +29,18 @@ public class GameController {
this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world = new World(canvas.getWidth(), canvas.getHeight());
//Draw scene on a separate thread to avoid blocking UI. //Draw scene on a separate thread to avoid blocking UI.
animationTimer = new DrawingThread(canvas, world); animationTimer = new DrawingThread(canvas, world);
angleSlider.valueProperty().addListener( (obj, oldValue, newValue) -> world.setAngleOfCannon(newValue.doubleValue()));
forceSlider.valueProperty().addListener( (obj, oldValue, newValue) -> world.setForce(newValue.doubleValue()));
animationTimer.start(); animationTimer.start();
}
@FXML
private void firePressed() {
world.fireBullet();
} }
......
...@@ -7,7 +7,7 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -7,7 +7,7 @@ import javafx.scene.canvas.GraphicsContext;
public class World { public class World {
private final static int NUMBER_OF_DRAGONS = 5; private final static int NUMBER_OF_DRAGONS = 1;
private double width; private double width;
private double height; private double height;
...@@ -78,4 +78,29 @@ public class World { ...@@ -78,4 +78,29 @@ public class World {
this.height = height; this.height = height;
} }
public void fireBullet() {
for (DrawableSimulable obj: entities) {
if (obj instanceof BulletAnimated bullet) {
bullet.fire();
}
}
}
public void setAngleOfCannon(double newValue) {
for (DrawableSimulable obj: entities) {
if (obj instanceof Cannon cannon) {
cannon.setAngle(newValue);
}
}
}
public void setForce(double doubleValue) {
for (DrawableSimulable obj: entities) {
if (obj instanceof BulletAnimated bullet) {
bullet.setForce(doubleValue);
}
}
}
} }
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<Insets bottom="11.0" left="13.0" right="13.0" top="11.0" /> <Insets bottom="11.0" left="13.0" right="13.0" top="11.0" />
</padding> </padding>
</Slider> </Slider>
<Button fx:id="fireButton" mnemonicParsing="false" text="Fire" HBox.hgrow="ALWAYS"> <Button fx:id="fireButton" mnemonicParsing="false" onAction="#firePressed" text="Fire" HBox.hgrow="ALWAYS">
<padding> <padding>
<Insets bottom="11.0" left="13.0" right="13.0" top="11.0" /> <Insets bottom="11.0" left="13.0" right="13.0" top="11.0" />
</padding> </padding>
......
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