Newer
Older
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
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 drawScene(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
}
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());
}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
@FXML
private void gameOverPressed() {
Score score = new Score(new Random().nextInt(11), nameTextField.getText());
scoreList.getItems().add(score);
Collections.sort(scoreList.getItems(), new ScoreComparator().reversed());
}
@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 {
try(PrintWriter pw = new PrintWriter(new FileWriter("high_score.csv"))){
for(Score score: highScores) {
pw.printf("%s;%d\n", score.getName(), score.getScore());
}
}
}
@FXML
private void loadPressed() throws IOException{
highScores.clear();
try(BufferedReader br = new BufferedReader(new FileReader("high_score.csv"))){
String line;
while(null != (line = br.readLine())) {
String [] tokens = line.split(";");
highScores.add(new Score(Integer.parseInt(tokens[1]), tokens[0]));
}
}
initScoreList();
}
private void initScoreList() {
scoreList.setItems(FXCollections.observableList(highScores));
}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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 shoots, int hits) {
GameController.this.shoots.setText("" + shoots);
GameController.this.hits.setText("" + hits);
}
@Override
public void gameOver() {
stopGame();
}
}