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

solution

parent b96c34b7
Branches 2022_Wed_7-45
No related merge requests found
Pipeline #76 failed with stages
in 0 seconds
...@@ -32,6 +32,7 @@ public class GameController { ...@@ -32,6 +32,7 @@ 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());
//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);
...@@ -40,6 +41,14 @@ public class GameController { ...@@ -40,6 +41,14 @@ public class GameController {
strengthSlider.valueProperty().addListener(this::strenghtChanged); strengthSlider.valueProperty().addListener(this::strenghtChanged);
world.setCannonStrength(strengthSlider.getValue()); world.setCannonStrength(strengthSlider.getValue());
world.setGameListener(new EmptyGameListener() {
@Override
public void stateChanged(int shoots, int hits) {
GameController.this.setShoots(shoots);
GameController.this.setHits(hits);
}
});
animationTimer.start(); animationTimer.start();
} }
...@@ -63,4 +72,18 @@ public class GameController { ...@@ -63,4 +72,18 @@ public class GameController {
, Number oldValue, Number newValue) { , Number oldValue, Number newValue) {
world.setCannonStrength(newValue.doubleValue()); world.setCannonStrength(newValue.doubleValue());
} }
private void setHits(int hits) {
this.hits.setText(""+hits);
}
private void setShoots(int shoots) {
this.shoots.setText(""+shoots);
}
} }
package lab; package lab;
@FunctionalInterface
public interface HitListener { public interface HitListener {
void hit(); void hit();
} }
...@@ -21,13 +21,24 @@ public class World { ...@@ -21,13 +21,24 @@ public class World {
private DrawableSimulable []entities; private DrawableSimulable []entities;
public World(double width, double height) { public World(double width, double height) {
super();
this.width = width; this.width = width;
this.height = height; this.height = height;
Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
BulletAnimated bullet;
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] = bullet = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
bullet.setHitListener(this::hits);
/*new HitListener() {
@Override
public void hit() {
hits++;
gameListener.stateChanged(shoots, hits);
}
});*/
Random rnd = new Random(); Random rnd = new Random();
for (int i = 2; i < entities.length; i++) { for (int i = 2; i < entities.length; i++) {
...@@ -39,6 +50,11 @@ public class World { ...@@ -39,6 +50,11 @@ public class World {
} }
} }
private void hits() {
hits++;
gameListener.stateChanged(shoots, hits);
}
public Point2D getCanvasPoint(Point2D worldPoint) { public Point2D getCanvasPoint(Point2D worldPoint) {
return new Point2D(worldPoint.getX(), height - worldPoint.getY()); return new Point2D(worldPoint.getX(), height - worldPoint.getY());
} }
......
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