-
Notifications
You must be signed in to change notification settings - Fork 0
/
PuyoView.cpp
424 lines (376 loc) · 11.8 KB
/
PuyoView.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* FloboPuyo
* Copyright (C) 2004
* Florent Boudet <[email protected]>,
* Jean-Christophe Hoelt <[email protected]>,
* Guillaume Borios <[email protected]>
*
* iOS Software <http://www.ios-software.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*/
#include <cstdlib>
#include <cmath>
#include "PuyoView.h"
#include "PuyoAnimations.h"
#include "AnimatedPuyo.h"
#include "PuyoGame.h"
extern "C" {
#include "audio.h"
}
#include "IosImgProcess.h"
#include "SDL_Painter.h"
SDL_Painter painter;
SDL_Surface *display;
IIM_Surface *background, *fallingBlue, *fallingRed, *fallingGreen, *fallingViolet, *fallingYellow, *neutral;
IIM_Surface *bigNeutral;
IIM_Surface *puyoEyes;
IIM_Surface *puyoFaces[5][16];
IIM_Surface *puyoShadow;
static char PuyoGroupImageIndex[2][2][2][2] =
{ { // empty bottom
{ // empty right
{ // empty top
0, // empty left
4, // full left
},
{ // full top
3, // empty left
10, // full left
}
},
{ // full right
{ // empty top
2, // empty left
9, // full left
},
{ // full top
8, // empty left
14, // full left
}
}
},
{ // full bottom
{ // empty right
{ // empty top
1, // empty left
7, // full left
},
{ // full top
6, // empty left
13, // full left
}
},
{ // full right
{ // empty top
5, // empty left
12, // full left
},
{ // full top
11, // empty left
15, // full left
}
}
}
};
PuyoView::PuyoView(PuyoRandomSystem *attachedRandom, int xOffset, int yOffset, int nXOffset, int nYOffset)
:attachedPuyoFactory(this), attachedPainter(painter)
{
attachedGame = new PuyoGame(attachedRandom, &attachedPuyoFactory);
attachedGame->setDelegate(this);
this->xOffset = xOffset;
this->yOffset = yOffset - TSIZE;
this->nXOffset = nXOffset;
this->nYOffset = nYOffset;
gameRunning = true;
enemyGame = NULL;
skippedCycle = false;
cycleAllowance = 0;
}
void PuyoView::setEnemyGame(PuyoGame *enemyGame)
{
this->enemyGame = enemyGame;
}
IIM_Surface *PuyoView::getSurfaceForState(PuyoState state)
{
switch (state) {
case PUYO_BLUE:
return fallingBlue;
break;
case PUYO_RED:
return fallingRed;
break;
case PUYO_GREEN:
return fallingGreen;
break;
case PUYO_VIOLET:
return fallingViolet;
break;
case PUYO_YELLOW:
return fallingYellow;
break;
case PUYO_FALLINGBLUE:
return fallingBlue;
break;
case PUYO_FALLINGRED:
return fallingRed;
break;
case PUYO_FALLINGGREEN:
return fallingGreen;
break;
case PUYO_FALLINGVIOLET:
return fallingViolet;
break;
case PUYO_FALLINGYELLOW:
return fallingYellow;
break;
case PUYO_NEUTRAL:
return neutral;
break;
default:
return NULL;
break;
}
}
IIM_Surface *PuyoView::getSurfaceForPuyo(PuyoPuyo *puyo)
{
int i = puyo->getPuyoX();
int j = puyo->getPuyoY();
PuyoState currentPuyoState = puyo->getPuyoState();
AnimatedPuyo *down = (AnimatedPuyo *)(attachedGame->getPuyoAt(i, j+1));
AnimatedPuyo *right = (AnimatedPuyo *)(attachedGame->getPuyoAt(i+1, j));
AnimatedPuyo *up = (AnimatedPuyo *)(attachedGame->getPuyoAt(i, j-1));
AnimatedPuyo *left = (AnimatedPuyo *)(attachedGame->getPuyoAt(i-1, j));
PuyoState downState = (down == NULL) || (down->isRenderingAnimation()) ? PUYO_EMPTY : down->getPuyoState();
PuyoState rightState = (right == NULL) || (right->isRenderingAnimation()) ? PUYO_EMPTY : right->getPuyoState();
PuyoState upState = (up == NULL) || (up->isRenderingAnimation()) ? PUYO_EMPTY : up->getPuyoState();
PuyoState leftState = (left == NULL) || (left->isRenderingAnimation()) ? PUYO_EMPTY : left->getPuyoState();
int currentIndex = PuyoGroupImageIndex
[downState == currentPuyoState ? 1 : 0]
[rightState == currentPuyoState ? 1 : 0]
[upState == currentPuyoState ? 1 : 0]
[leftState == currentPuyoState ? 1 : 0];
switch (currentPuyoState) {
case PUYO_VIOLET:
return puyoFaces[0][currentIndex];
case PUYO_RED:
return puyoFaces[1][currentIndex];
case PUYO_BLUE:
return puyoFaces[2][currentIndex];
case PUYO_GREEN:
return puyoFaces[3][currentIndex];
case PUYO_YELLOW:
return puyoFaces[4][currentIndex];
default:
return getSurfaceForState(currentPuyoState);
}
}
void PuyoView::cycleAnimation()
{
// Cycling every puyo's animation
for (int i = 0, j = attachedGame->getPuyoCount() ; i < j ; i++) {
AnimatedPuyo *currentPuyo =
(AnimatedPuyo *)(attachedGame->getPuyoAtIndex(i));
currentPuyo->cycleAnimation();
}
// Cycling dead puyo's animations
attachedPuyoFactory.cycleWalhalla();
// Cycling view's animations
if (!viewAnimations.empty()) {
VanishSoundAnimation *currentAnimation = viewAnimations.at(0);
if (currentAnimation->isFinished()) {
viewAnimations.erase(viewAnimations.cbegin());
delete currentAnimation;
}
else {
currentAnimation->cycle();
}
}
// If there is a skipped cycle to do, do it
if ((skippedCycle || attachedGame->isEndOfCycle()) && attachedGame->isGameRunning() && cycleAllowed()) {
attachedGame->cycle();
skippedCycle = false;
}
}
void PuyoView::cycleGame()
{
// If we are not allowed to cycle the game, mark it
if (cycleAllowed()) {
skippedCycle = false;
attachedGame->cycle();
}
else {
skippedCycle = true;
}
}
void PuyoView::moveLeft()
{
if (cycleAllowed()) attachedGame->moveLeft();
}
void PuyoView::moveRight()
{
if (cycleAllowed()) attachedGame->moveRight();
}
void PuyoView::rotateLeft()
{
if (cycleAllowed()) attachedGame->rotate(true);
}
void PuyoView::rotateRight()
{
if (cycleAllowed()) attachedGame->rotate(false);
}
void PuyoView::render()
{
SDL_Rect drect;
SDL_Rect vrect;
vrect.x = xOffset;
vrect.y = yOffset;
vrect.w = TSIZE * PUYODIMX;
vrect.h = TSIZE * PUYODIMY;
bool displayFallings = this->cycleAllowed();
for (int i = 0 ; i < PUYODIMX ; i++) {
for (int j = 0 ; j < PUYODIMY ; j++) {
AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(attachedGame->getPuyoAt(i, j));
if ((currentPuyo != NULL) && (displayFallings || !currentPuyo->isFalling()) && (getSurfaceForPuyo(currentPuyo) != neutral)
&& (currentPuyo->getVisible()) && (currentPuyo->isRenderingAnimation() == false)) {
drect.x = xOffset + i * TSIZE;
drect.y = yOffset + j * TSIZE;
if (currentPuyo->getPuyoState() < PUYO_EMPTY)
drect.y -= attachedGame->getSemiMove() * TSIZE / 2;
drect.w = puyoShadow->w;
drect.h = puyoShadow->h;
if (drect.y + drect.h > vrect.y + vrect.h)
drect.h -= (drect.y + drect.h - vrect.y - vrect.h);
if (drect.x + drect.w > vrect.x + vrect.w)
drect.w -= (drect.x + drect.w - vrect.x - vrect.w);
attachedPainter.requestDraw(puyoShadow, &drect);
}
}
}
for (int i = 0, j = attachedGame->getPuyoCount() ; i < j ; i++) {
AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(attachedGame->getPuyoAtIndex(i));
if (displayFallings || !currentPuyo->isFalling()) currentPuyo->render();
}
// drawing the walhalla
attachedPuyoFactory.renderWalhalla();
drect.x = nXOffset;
drect.y = nYOffset;
drect.w = TSIZE;
drect.h = TSIZE * 2;
// Drawing next puyos
IIM_Surface *currentSurface = getSurfaceForState(attachedGame->getNextFalling());
if (currentSurface != NULL) {
drect.x = nXOffset;
drect.y = nYOffset + TSIZE;
drect.w = currentSurface->w;
drect.h = currentSurface->h;
attachedPainter.requestDraw(currentSurface, &drect);
if (currentSurface != neutral) attachedPainter.requestDraw(puyoEyes, &drect);
}
currentSurface = getSurfaceForState(attachedGame->getNextCompanion());
if (currentSurface != NULL) {
drect.x = nXOffset;
drect.y = nYOffset;
drect.w = currentSurface->w;
drect.h = currentSurface->h;
attachedPainter.requestDraw(currentSurface, &drect);
if (currentSurface != neutral) attachedPainter.requestDraw(puyoEyes, &drect);
}
// Drawing the view animation
if (!viewAnimations.empty()) {
Animation *currentAnimation = viewAnimations.at(0);
if (!currentAnimation->isFinished()) {
currentAnimation->draw(0);
}
}
}
void PuyoView::renderNeutral()
{
SDL_Rect drect;
// Drawing neutral puyos
int numBigNeutral = attachedGame->getNeutralPuyos() / PUYODIMX;
int numNeutral = attachedGame->getNeutralPuyos() % PUYODIMX;
int drect_x = xOffset;
int compressor = 0;
int width = numBigNeutral * bigNeutral->w + numNeutral * neutral->w;
if (width > TSIZE * PUYODIMX) {
compressor = (width - TSIZE * PUYODIMX) / (numNeutral + numBigNeutral);
}
for (int cpt = 0 ; cpt < numBigNeutral ; cpt++) {
drect.x = drect_x;
drect.y = yOffset + 3 + TSIZE;
drect.w = bigNeutral->w;
drect.h = bigNeutral->h;
attachedPainter.requestDraw(bigNeutral, &drect);
drect_x += bigNeutral->w - compressor;
}
for (int cpt = 0 ; cpt < numNeutral ; cpt++) {
drect.x = drect_x;
drect.y = yOffset + 3 + TSIZE;
drect.w = neutral->w;
drect.h = neutral->h;
attachedPainter.requestDraw(neutral, &drect);
drect_x += neutral->w - compressor;
}
}
void PuyoView::gameDidAddNeutral(PuyoPuyo *neutralPuyo, int neutralIndex) {
((AnimatedPuyo *)neutralPuyo)->addAnimation(new NeutralAnimation(*((AnimatedPuyo *)neutralPuyo), neutralIndex * 4));
}
void PuyoView::companionDidTurn(PuyoPuyo *companionPuyo, int companionVector, bool counterclockwise)
{
((AnimatedPuyo *)companionPuyo)->addAnimation(new TurningAnimation(*(AnimatedPuyo *)companionPuyo,
companionVector, counterclockwise));
}
void PuyoView::puyoDidFall(PuyoPuyo *puyo, int, int originY)
{
((AnimatedPuyo *)puyo)->addAnimation(new FallingAnimation(*(AnimatedPuyo *)puyo, originY, xOffset, yOffset));
}
void PuyoView::puyoWillVanish(std::vector<PuyoPuyo*> &puyoGroup, int groupNum, int phase)
{
AnimationSynchronizer *synchronizer = new AnimationSynchronizer();
viewAnimations.push_back(new VanishSoundAnimation(phase, synchronizer));
for (int i = 0, j = puyoGroup.size() ; i < j ; i++) {
AnimatedPuyo *currentPuyo = (AnimatedPuyo *)(puyoGroup.at(i));
currentPuyo->addAnimation(new VanishAnimation(*currentPuyo, i*2 , xOffset, yOffset, synchronizer));
}
// A revoir
if (groupNum == 0) {
if (phase>=2) {
audio_sound_play(sound_yahoohoo3[(int)((float)NB_YAHOOHOO3 * random()/(RAND_MAX+1.0))]);
}
if (phase==1) {
audio_sound_play(sound_yahoohoo2[(int)((float)NB_YAHOOHOO2 * random()/(RAND_MAX+1.0))]);
}
else {
audio_sound_play(sound_yahoohoo1[(int)((float)NB_YAHOOHOO1 * random()/(RAND_MAX+1.0))]);
}
}
}
void PuyoView::gameDidEndCycle()
{
if (enemyGame != NULL) {
if (attachedGame->getNeutralPuyos() < 0)
enemyGame->increaseNeutralPuyos(- attachedGame->getNeutralPuyos());
}
}
bool PuyoView::cycleAllowed()
{
if (cycleAllowance < 0)
return false;
return true;
}
void PuyoView::gameLost()
{
gameRunning = false;
}