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

solution 2023

parent 3ef6fe0e
Branches 2023_solution
No related merge requests found
package lab;
import java.util.Random;
import java.util.Scanner;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
......@@ -25,6 +26,7 @@ public class App extends Application {
@Override
public void start(Stage primaryStage) {
try {
//Construct a main window with a canvas.
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("GameView.fxml"));
......@@ -52,11 +54,31 @@ public class App extends Application {
private Point2D[][] getDragonsInit(double width, double height) {
Scanner sc = new Scanner(System.in);
Point2D[][] result;
//ask whether would like to enter dragons
System.out.println("How many dragons do you want?");
//test whether integer is on input
if (sc.hasNextInt()) {
int dragonsNum = sc.nextInt();
result = new Point2D[dragonsNum][];
for (int i = 0; i < result.length; i++) {
result[i] = new Point2D[2];
System.out.println("What is position of the dragon #" + (i + 1) + " [x, y]: ");
result[i][0] = new Point2D(sc.nextDouble(), sc.nextDouble());
System.out.println("What is velocity of the dragon #" + (i + 1) + " [x, y]: ");
result[i][1] = new Point2D(sc.nextDouble(), sc.nextDouble());
}
} else {
result = getDefaultDragonsInit(width, height);
}
return result;
//...
//return default
return getDefaultDragonsInit(width, height);
//result = getDefaultDragonsInit(width, height);
}
......
......@@ -33,7 +33,9 @@ public class GameController {
}
public void startGame(Point2D[][] dragonInit) {
this.world = new World(canvas.getWidth(), canvas.getHeight(), dragonInit);
GameListener gl = new GameListenerImpl();
this.world = new World(canvas.getWidth(), canvas.getHeight(), dragonInit);
this.world.setGameListener(gl);
//Draw scene on a separate thread to avoid blocking UI.
animationTimer = new DrawingThread(canvas, world);
angleSlider.valueProperty().addListener(this::angleChanged);
......@@ -64,4 +66,49 @@ public class GameController {
, Number oldValue, Number newValue) {
world.setCannonStrength(newValue.doubleValue());
}
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() {
// leave empty
}
}
static class GameListenerImplStatic implements GameListener {
final Label shoots;
final Label hits;
public GameListenerImplStatic(Label shoots, Label hits) {
this.shoots = shoots;
this.hits = hits;
}
@Override
public void stateChanged(int shoots, int hits) {
this.shoots.setText("" + shoots);
this.hits.setText("" + hits);
}
@Override
public void gameOver() {
// leave empty
}
}
}
package lab;
import javafx.scene.control.Label;
public class GameListenerImpl implements GameListener {
final private Label shoots;
final private Label hits;
public GameListenerImpl(Label shoots, Label hits) {
this.shoots = shoots;
this.hits = hits;
}
@Override
public void stateChanged(int shoots, int hits) {
this.shoots.setText("" + shoots);
this.hits.setText("" + hits);
}
@Override
public void gameOver() {
// leave empty
}
}
......@@ -21,10 +21,35 @@ public class World {
super();
this.width = width;
this.height = height;
/*
class HitListenerImpl implements HitListener {
@Override
public void hit() {
hits++;
gameListener.stateChanged(shoots, hits);
}
}
*/
Cannon cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
entities = new DrawableSimulable[2 + dragonInit.length];
entities[0] = cannon;
entities[1] = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
//see ii
BulletAnimated temp;
entities[1] = temp = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
//see it
temp.setHitListener(new HitListener() {
@Override
public void hit() {
hits++;
gameListener.stateChanged(shoots, hits);
}
});
for (int i = 2; i < entities.length; i++) {
......
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