-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.pde
357 lines (323 loc) · 9.08 KB
/
Ball.pde
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
class Ball {
int x = 0;
int y = 0;
color baseColor = BALL_COLOR;
color flashColor = HIGHLIGHT_COLOR;
color col = baseColor;
boolean isFlashing = false;
int flashCount = 0;
int flashTime = 6;
int unitCount = 0;
int sideCount = 0;
int turnCount = 0;
int turnLength = 0;
int rythmeCount = 0;
int preCounter = 0;
String[] dirList = new String[4];
int[] unitLengthList = new int[4];
int[] pastSideList = {0, 0, 0, 0}; // past count each side
boolean preWaitable = false;
boolean preCountable = false;
boolean isMovable = false;
boolean isMoving = false;
boolean endWaitable = false;
boolean mapUpdatable = false;
void drawBall() {
String startPos = data.getStartPos(mapId);
int horiUnitLength = data.getUnitLengthOfSide("HORI", mapId);
int vertUnitLength = data.getUnitLengthOfSide("VERT", mapId);
int bpm = data.getBpm(mapId);
float distPerFrame = calcDistPerFrame(bpm);
float framePerUnit = calcFramePerUnit(distPerFrame);
if(mapUpdatable) { // (6) MAP_UPDATE (or end
updateMapId(); // of root
mapUpdatable = false;
}
else if(endWaitable) { // (5) END_COUNT
waitEndCount();
}
else if(isMovable) { // (4) MOVE_LOOP
if(frameCount <= 1 && sideCount == 0 && turnCount == 0) moveFirstAction();
moveLoop(startPos, horiUnitLength, vertUnitLength, distPerFrame, framePerUnit);
updateUnitCount(distPerFrame);
if(unitCount >= unitLengthList[sideCount]) updateSideCount();
}
else if(preCountable) { // (3) PRE_COUNT
preCount(framePerUnit);
drawPreCounter();
}
else if(preWaitable) { // (2) WAIT_COUNT
waitPreCount();
drawPreCounter();
}
else { // (1) INIT
initTurnLength();
initDirList(startPos);
initMoveType(startPos, horiUnitLength, vertUnitLength);
resetPastSideList();
turnCount = 0;
preWaitable = true;
}
displayBall();
}
private float calcDistPerFrame(int bpm) {
float beatPerSecond = bpm / 60;
float distPerSecond = beatPerSecond * UNIT_WIDTH;
return distPerSecond / FPS;
}
private float calcFramePerUnit(float distPerFrame) {
return UNIT_WIDTH / distPerFrame;
}
private void initTurnLength() {
turnLength = data.getTurnLength(mapId);
}
private void initDirList(String startPos) {
// startPos => LEFT_TOP pr RIGHT_TOP
String evenDir = "HORI";
String oddDir = "VERT";
if(startPos == "RIGHT_TOP") {
evenDir = "VERT";
oddDir = "HORI";
}
dirList[0] = evenDir;
dirList[1] = oddDir;
dirList[2] = evenDir;
dirList[3] = oddDir;
}
private void initMoveType(String startPos, int horiUnitLength, int vertUnitLength) {
// startPos => LEFT_TOP pr RIGHT_TOP
switch(startPos){
case "LEFT_TOP":
initPos(0, 0);
initUnitLengthList(horiUnitLength, vertUnitLength);
break;
case "RIGHT_TOP":
initPos(UNIT_WIDTH * horiUnitLength, 0);
initUnitLengthList(vertUnitLength, horiUnitLength);
break;
}
}
private void initPos(int startX, int startY) {
x = startX;
y = startY;
}
private void initUnitLengthList(int evenSum, int oddSum) {
// array => {even, odd, even, odd}
unitLengthList[0] = evenSum;
unitLengthList[1] = oddSum;
unitLengthList[2] = evenSum;
unitLengthList[3] = oddSum;
}
private void displayBall() {
fill(col);
if(isFlashing) flashCount++;
if(flashCount > flashTime) {
isFlashing = false;
col = baseColor;
flashCount = 0;
}
noStroke();
ellipse(x, y, BALL_SIZE, BALL_SIZE);
}
private void waitPreCount() {
if(frameCount >= PRE_WAIT_TIME * FPS) {
preWaitable = false;
preCountable = true;
frameCount = 0;
}
}
private void preCount(float framePerUnit) {
final int beat = 4;
if(frameCount >= framePerUnit * beat) {
flashBall();
preCounter++;
if(preCounter > 4) {
preCounter = 0;
preCountable = false;
isMovable = true;
}
frameCount = 0;
}
}
private void drawPreCounter() {
fill(BASE_COLOR);
noStroke();
int rx = 6;
int ry = 6;
int rw = 14;
int rh = 14;
int margin = 5;
int col1 = BASE_COLOR;
int col2 = BASE_COLOR;
int col3 = BASE_COLOR;
int col4 = BASE_COLOR;
switch(preCounter) {
case 1:
col1 = HIGHLIGHT_COLOR;
break;
case 2:
col1 = HIGHLIGHT_COLOR;
col2 = HIGHLIGHT_COLOR;
break;
case 3:
col1 = HIGHLIGHT_COLOR;
col2 = HIGHLIGHT_COLOR;
col3 = HIGHLIGHT_COLOR;
break;
case 4:
col1 = HIGHLIGHT_COLOR;
col2 = HIGHLIGHT_COLOR;
col3 = HIGHLIGHT_COLOR;
col4 = HIGHLIGHT_COLOR;
break;
}
if(data.getStartPos(mapId) == "RIGHT_TOP") {
int maxX = data.getRectWidth(mapId);
fill(col1);
rect(maxX-rx-rw-margin-rw, ry, rw, rh);
fill(col2);
rect(maxX-rx-rw, ry, rw, rh);
fill(col3);
rect(maxX-rx-rw-margin-rw, ry+rh+margin, rw, rh);
fill(col4);
rect(maxX-rx-rw, ry+rh+margin, rw, rh);
} else {
fill(col1);
rect(rx, ry, rw, rh);
fill(col2);
rect(rx+rw+margin, ry, rw, rh);
fill(col3);
rect(rx, ry+rh+margin, rw, rh);
fill(col4);
rect(rx+rw+margin, ry+rh+margin, rw, rh);
}
}
private void moveFirstAction() {
isMoving = true;
flashBall();
map.flashArea();
}
private void moveLoop(String startPos, int horiUnitLength, int vertUnitLength, float distPerFrame, float framePerUnit) {
// startPos => LEFT_TOP pr RIGHT_TOP
int s = sideCount;
int rectWidth = data.getRectWidth(mapId);
int rectHeight = data.getRectHeight(mapId);
if(startPos == "RIGHT_TOP") s = (s + 1) % 4; // LEFT=>0, 1, 2, 3 RIGHT=>1, 2, 3, 0
switch(s){
case 0:
x = Math.min( (int)Math.floor(distPerFrame * frameCount), rectWidth);
y = 0;
break;
case 1:
x = UNIT_WIDTH * horiUnitLength;
y = Math.min( (int)Math.floor(distPerFrame * frameCount), rectHeight);
break;
case 2:
x = Math.max( (int)Math.floor(distPerFrame * (horiUnitLength * framePerUnit - frameCount)), 0);
y = UNIT_WIDTH * vertUnitLength;
break;
case 3:
x = 0;
y = Math.max( (int)Math.floor(distPerFrame * (vertUnitLength * framePerUnit - frameCount)), 0);
break;
}
}
private void updateUnitCount(float distPerFrame) {
unitCount = (int)Math.floor((frameCount * distPerFrame) / UNIT_WIDTH);
int[] rythmeList = data.getRythmeList(dirList[sideCount], mapId);
int rythmeCountOfPastSide = countRythmeSumOfPastSide(sideCount);
int rythmeSumOfPastUnit = countRythmeSumOfPastUnit(rythmeList, rythmeCountOfPastSide);
int rythmeSumOfSide = rythmeSumOfPastUnit + rythmeList[rythmeCount-rythmeCountOfPastSide];
if(unitCount >= rythmeSumOfSide) {
updateRythmeCount();
}
}
private int countRythmeSumOfPastSide(int s) {
int result = 0;
for(int pr=s-1; pr>=0; pr--){
int[] pastRythmeList = data.getRythmeList(dirList[pr], mapId);
result += pastRythmeList.length;
}
return result;
}
private int countRythmeSumOfPastUnit(int[] rythmeList, int rythmeCountOfPastSide) {
int result = 0;
for(int i = rythmeCount-1-rythmeCountOfPastSide; i>=0; i--) {
result += rythmeList[i];
}
return result;
}
private void updateRythmeCount() {
if(turnCount < data.getWholeNoteStartMapId(mapId)) {
flashBall();
map.flashArea();
if(mapId == data.getMapLength()-1 && turnCount >= 1) {
map.updateLogicArtDrawArea();
map.flashLogicArt();
}
if(mapId == 4 && turnCount == 2) map.updateGradationColor();
}
rythmeCount++;
}
private void flashBall() {
col = flashColor;
isFlashing = true;
}
private void updateSideCount() {
updatePastSideList(sideCount);
sideCount++;
if(sideCount == 4) {
updateTurnCount();
rythmeCount = 0;
sideCount = 0;
}
unitCount = 0;
frameCount = 0;
}
private void updatePastSideList(int s) {
pastSideList[s]++;
}
private void updateTurnCount() {
turnCount++;
if(turnCount >= turnLength) {
isMovable = false;
isMoving = false;
endWaitable = true;
}
}
private void resetPastSideList() {
for(int i=0; i<pastSideList.length; i++) pastSideList[i] = 0;
}
private void waitEndCount() {
if(frameCount >= END_WAIT_TIME * FPS) {
endWaitable = false;
mapUpdatable = true;
preCounter = 0;
frameCount = 0;
}
}
boolean getIsMoving() {
return isMoving;
}
int[] getPastSideList() {
return pastSideList;
}
int getTurnCount() {
return turnCount;
}
int getSideCount() {
return sideCount;
}
int getRythmeCount() {
return rythmeCount;
}
int getUnitCount() {
return unitCount;
}
boolean getIsLastTurn() {
return turnCount + 1 == turnLength;
}
boolean getEndWaitable() {
return endWaitable;
}
}