Skip to content
Snippets Groups Projects
Commit 4dafdeec authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

lab07-solution-monday-1415

parent d3f97461
No related merge requests found
Pipeline #84 failed with stages
in 0 seconds
...@@ -113,5 +113,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{ ...@@ -113,5 +113,9 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
accelerate = true; accelerate = true;
speed = initialSpeed; speed = initialSpeed;
} }
public void setHitListener(HitListener hitListenerImpl) {
this.hitListener = hitListenerImpl;
}
} }
...@@ -32,21 +32,10 @@ public class GameController { ...@@ -32,21 +32,10 @@ public class GameController {
} }
public void startGame() { 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. //Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimer() { animationTimer = new AnimationTimerImpl();
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
};
angleSlider.valueProperty().addListener(this::angleChanged); angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue()); world.setCannonAngle(angleSlider.getValue());
...@@ -80,4 +69,33 @@ public class GameController { ...@@ -80,4 +69,33 @@ public class GameController {
, Number oldValue, Number newValue) { , Number oldValue, Number newValue) {
world.setCannonStrength(newValue.doubleValue()); 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 aShoots, int aHits) {
GameController.this.shoots.setText("" + aShoots);
hits.setText("" + aHits);
}
@Override
public void gameOver() {
stopGame();
}
}
} }
...@@ -29,6 +29,7 @@ public class World { ...@@ -29,6 +29,7 @@ public class World {
entities = new DrawableSimulable[2 + NUMBER_OF_DRAGONS]; entities = new DrawableSimulable[2 + NUMBER_OF_DRAGONS];
entities[0] = cannon; entities[0] = cannon;
entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40); entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
((BulletAnimated)entities[1]).setHitListener(this::bulletHit);
Random rnd = new Random(); Random rnd = new Random();
for (int i = 2; i < entities.length; i++) { for (int i = 2; i < entities.length; i++) {
...@@ -117,4 +118,16 @@ public class World { ...@@ -117,4 +118,16 @@ public class World {
} }
} }
public void setGameListener(GameListener gameListener) {
this.gameListener = gameListener;
}
private void bulletHit() {
hits++;
gameListener.stateChanged(shoots, hits);
if (shoots > 10 ) {
gameListener.gameOver();
}
}
} }
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