Newer
Older
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;
@FXML
private Label shoots;
@FXML
private Label hits;
@FXML
private ListView<Score> scoreList;
@FXML
private TextField nameTextField;
private List<Score> highScores = new LinkedList<>();
this.world = new World(canvas.getWidth(), canvas.getHeight());
this.world.setGameListener(new GameListenerImpl());
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();
}
}
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);
}
@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 {
}
@FXML
private void loadPressed() throws IOException{
highScores.clear();
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();
}
}