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

lab07-solution-wednesday-9_00

parent 2ec9cc77
No related merge requests found
......@@ -4,7 +4,6 @@ import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated implements DrawableSimulable, Collisionable{
private static final double STRENGTH_CANNON_COEFICIENT = 4.;
......@@ -56,7 +55,6 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
if (!fired) {
return;
}
double timeStep = deltaT * 1000;
if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle();
double strenghtOfCannon = cannon.getStrength() * STRENGTH_CANNON_COEFICIENT/100.;
......@@ -69,7 +67,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * dragCoefficient * Math.pow(speed.getX(), 2),
-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * 0.47 * Math.pow(speed.getY(), 2));
Point2D acceleration = new Point2D(-airResistanceforce.getX() * mass,
(-Constants.GRAVITATIONAL_ACCELERATION/30 + airResistanceforce.getY()) * mass);
(-Constants.GRAVITATIONAL_ACCELERATION/20 + airResistanceforce.getY()) * mass);
speed = speed.add(acceleration.multiply(deltaT));
System.out.println("speed"+ speed);
}
......@@ -114,5 +112,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
accelerate = true;
speed = initialSpeed;
}
public void setHitListener(HitListener hitListenerImpl) {
hitListener = hitListenerImpl;
}
}
......@@ -32,21 +32,10 @@ public class GameController {
}
public void startGame() {
this.world = new World(canvas.getWidth(), canvas.getHeight());
this.world = new World(canvas.getWidth(), canvas.getHeight());
this.world.setGameListener(new GameListenerImpl());
//Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimer() {
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
};
animationTimer = new AnimationTimerImpl();
angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue());
......@@ -80,4 +69,33 @@ public class GameController {
, Number oldValue, Number newValue) {
world.setCannonStrength(newValue.doubleValue());
}
private final class AnimationTimerImpl extends AnimationTimer {
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
}
private class GameListenerImpl implements GameListener {
@Override
public void stateChanged(int shoots, int hits) {
GameController.this.shoots.setText("" + shoots);
GameController.this.hits.setText("" + hits);
}
@Override
public void gameOver() {
stopGame();
}
}
}
......@@ -29,6 +29,7 @@ public class World {
entities = new DrawableSimulable[2 + NUMBER_OF_DRAGONS];
entities[0] = cannon;
entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
((BulletAnimated)entities[1]).setHitListener(this::hitOfBullet);
Random rnd = new Random();
for (int i = 2; i < entities.length; i++) {
......@@ -113,8 +114,21 @@ public class World {
ba.fire();
shoots++;
gameListener.stateChanged(shoots, hits);
if (shoots > 10) {
gameListener.gameOver();
}
}
}
}
public void setGameListener(GameListener gameListenerImpl) {
this.gameListener = gameListenerImpl;
}
private void hitOfBullet() {
hits++;
gameListener.stateChanged(shoots, hits);
}
}
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