-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeatmapController.java
287 lines (241 loc) · 7.9 KB
/
BeatmapController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import greenfoot.Actor;
import greenfoot.Color;
import greenfoot.GreenfootImage;
import java.util.ArrayList;
/**
* A BeatmapController is an actor that controls the beatmap, spawns the
* notes in the correct timing, etc.
*
* @author Team APCSA 2019
* @author Yijie Gui
* @since 2019-05-25 06:37
*/
@SuppressWarnings("WeakerAccess")
public class BeatmapController extends Actor
{
/** Timing controller. */
private final TimingController timer;
/** Beatmap. */
private final Beatmap beatmap;
/** Judgement calculator */
private final JudgementCalculator judgementCalculator;
/** Score counter. */
private final ScoreCounter scoreCounter;
/** Key hit score */
private final KeyHitScore keyHitDisplayer;
/** Key hit animation displayers, in a row */
private final KeyHitAnimation[] keyHitAnimations;
/**
* Create a beatmap controller.
*
* @param beatmap The beatmap
* @param scoreCounter Score counter
*/
public BeatmapController(Beatmap beatmap, ScoreCounter scoreCounter)
{
this.timer = new TimingController();
this.beatmap = beatmap;
this.judgementCalculator = new JudgementCalculator(beatmap);
this.scoreCounter = scoreCounter;
// Create key hit score object
keyHitDisplayer = new KeyHitScore();
// Create key hit animation array
keyHitAnimations = new KeyHitAnimation[Constants.NUM_COLS];
setImage((GreenfootImage) null);
}
/**
* Initialize position. This method is called in KeypressHandler when
* the key score object is created.
*/
public void init()
{
// Put the key hit score displayer in the world
getWorld().addObject(keyHitDisplayer, 0, 0);
keyHitDisplayer.init();
for (int i = 0; i < keyHitAnimations.length; i++)
{
getWorld().addObject(keyHitAnimations[i] = new KeyHitAnimation(i), 0, 0);
keyHitAnimations[i].init();
}
}
/**
* Start the timer and music. ( = Start the game)
*/
public void start()
{
// Use async execution to deal with offsets.
new Thread(() ->
{
try
{
int baseDelay = Constants.GAME_START_OFFSET + Constants.GAME_SPEED_MS;
int musicDelay = baseDelay + Constants.GAME_MUSIC_OFFSET;
// Get who's bigger. This is necessary because a thread can't
// sleep negative seconds.
if (baseDelay > musicDelay)
{
timer.start(baseDelay);
Thread.sleep(musicDelay);
beatmap.getMusic().play();
}
else
{
Thread.sleep(baseDelay);
timer.start();
Thread.sleep(musicDelay - baseDelay);
beatmap.getMusic().play();
}
}
catch (InterruptedException ignored) {}
}).start();
}
/**
* Act: Detect the notes that are about to fall down, and spawn them.
*/
public void act()
{
if (!timer.isRunning()) return;
// Get time
int gameTime = timer.getTotalDuration();
// Spawn notes.
for (ArrayList<NoteInformation> col : beatmap.getFuture())
{
// Used this to bypass java.util.ConcurrentModificationException
for (int i = 0; i < col.size(); i++)
{
NoteInformation noteInfo = col.get(i);
// Within the speed range.
if (noteInfo.getTime() + Constants.GAME_SPAWNING_OFFSET - gameTime < Constants.GAME_SPEED_MS)
{
// Spawn the note to the top.
spawnNote(noteInfo);
i--;
}
}
}
// Register non-hit notes as missed.
for (ArrayList<Note> col : beatmap.getPresent())
{
// Used this to bypass java.util.ConcurrentModificationException
for (int i = 0; i < col.size(); i++)
{
Note note = col.get(i);
// Note is missed
if (judgementCalculator.isMissed(note.getHitTime(), gameTime))
{
// Register a missed note.
registerHitAndRemoveNote(note, 5);
i--;
}
}
}
// Check if done.
if (beatmap.isDone())
{
// Stop timer
timer.stop();
/* Output JSON object for debug.
try
{
System.out.println(new Gson().toJson(scoreCounter));
new Gson().toJson(scoreCounter, new FileWriter("./scores/score-counter-object-" + System.currentTimeMillis() + ".json"));
}
catch (IOException e)
{
e.printStackTrace();
}*/
// Remove score objects
getWorld().removeObject(scoreCounter.getAccuracyDisplayer());
getWorld().removeObject(scoreCounter.getBonusDisplayer());
getWorld().removeObject(scoreCounter.getTotalDisplayer());
// Make combo display max combo
scoreCounter.getComboDisplayer().update(scoreCounter.getMaxCombo());
// Create score report.
ScoreReport report = new ScoreReport(scoreCounter);
getWorld().addObject(report, Constants.WIDTH / 2, Constants.HEIGHT / 2);
report.draw();
}
}
/**
* Spawn a note.
*
* @param noteInfo Information of the note.
*/
private void spawnNote(NoteInformation noteInfo)
{
// Spawn the note.
Note note = new Note(noteInfo);
getWorld().addObject(note, 0, 0);
note.init();
// Put in present and remove from future.
beatmap.getPresent(noteInfo.getColumn()).add(note);
beatmap.getFuture(noteInfo.getColumn()).remove(noteInfo);
}
/**
* Remove a present note.
*
* @param note Present note actor.
* @param hitScore Hit score (From 0 to 5)
*/
private void registerHitAndRemoveNote(Note note, int hitScore)
{
// Hit
scoreCounter.hit(hitScore);
// Remove note
note.getWorld().removeObject(note);
// Move from present to past
beatmap.getPresent(note.getColumn()).remove(note);
beatmap.getPast(note.getColumn()).add(new NoteInformation(note));
// Show hit image
keyHitDisplayer.hit(hitScore);
// Show hit animation if not missed.
if (hitScore < 5)
{
keyHitAnimations[note.getColumn()].resetIndex();
}
}
/**
* Register a hit.
*
* @param col Column number
*/
public void hit(int col)
{
// Get time
int gameTime = timer.getTotalDuration();
// Check if there are any present notes
if (beatmap.getPresent(col).size() == 0) return;
// Get note
Note note = beatmap.getPresent(col).get(0);
// Calculate hit score
int hit = judgementCalculator.calculateHitValue(note.getHitTime(), gameTime);
// Player didn't hit anything in range.
if (hit == -1) return;
// Show debug line
if (Constants.DEBUG_MODE)
{
((BeatmapWorld) getWorld()).drawOffsetLine(gameTime - note.getHitTime(), Color.PINK, 1);
}
// Register hit
registerHitAndRemoveNote(note, hit);
}
// ###################
// Getters and Setters
// ###################
public TimingController getTimer()
{
return timer;
}
public Beatmap getBeatmap()
{
return beatmap;
}
public JudgementCalculator getJudgementCalculator()
{
return judgementCalculator;
}
public ScoreCounter getScoreCounter()
{
return scoreCounter;
}
}