package lab; import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.Set; import javafx.animation.AnimationTimer; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.scene.canvas.Canvas; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.Slider; import javafx.scene.control.TextField; public class GameController { private World world; @FXML private Slider angleSlider; @FXML private Slider strengthSlider; @FXML private Canvas canvas; @FXML private Label shoots; @FXML private Label hits; @FXML private ListView<Score> scoreList; @FXML private TextField nameTextField; private List<Score> highScores = new LinkedList<>(); private AnimationTimer animationTimer; private ScoreDAO scoreDAO; public GameController() { } public void startGame() { scoreDAO = new ScoreDAO(); initScoreList(); 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 DrawingThread(canvas, world); angleSlider.valueProperty().addListener(this::angleChanged); world.setCannonAngle(angleSlider.getValue()); strengthSlider.valueProperty().addListener(this::strenghtChanged); world.setCannonStrength(strengthSlider.getValue()); animationTimer.start(); } public void stopGame() { animationTimer.stop(); } @FXML private void firePressed() { world.fire(); } private void angleChanged(ObservableValue<? extends Number> observable , Number oldValue, Number newValue) { world.setCannonAngle(newValue.doubleValue()); } private void strenghtChanged(ObservableValue<? extends Number> observable , Number oldValue, Number newValue) { world.setCannonStrength(newValue.doubleValue()); } @FXML private void gameOverPressed() { Score score = new Score(new Random().nextInt(11), nameTextField.getText()); scoreList.getItems().add(score); sortScores(); } @FXML private void highScoresPressed() { Set<Score> tempScores = new HashSet<>(); tempScores.addAll(highScores); highScores.clear(); highScores.addAll(tempScores); Collections.sort(highScores, new ScoreComparator().reversed()); initScoreList(); } @FXML private void savePressed() throws IOException { scoreDAO.save(highScores); } @FXML private void loadPressed() throws IOException{ highScores.clear(); highScores.addAll(scoreDAO.load()); sortScores(); initScoreList(); } private void initScoreList() { scoreList.setItems(FXCollections.observableList(highScores)); } private void sortScores() { Collections.sort(scoreList.getItems(), new ScoreComparator().reversed()); } 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(); } } }