-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.cpp
865 lines (790 loc) · 22.4 KB
/
board.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
Pony48 source - audio.cpp
Copyright (c) 2014 Mark Hutcheson
*/
#include "Pony48.h"
TilePiece::TilePiece()
{
seg=NULL;
bg=NULL;
value=0;
drawSlide.SetZero();
drawSize.SetZero();
destx=desty=-1;
iAnimDir=1;
joined=false;
}
TilePiece::~TilePiece()
{
if(seg!=NULL)
delete seg;
if(bg!=NULL)
delete bg;
}
void TilePiece::draw()
{
if(bg!=NULL)
{
bg->size = drawSize;
bg->draw();
}
if(seg!=NULL)
{
seg->size = drawSize;
seg->draw();
}
}
void Pony48Engine::clearBoard()
{
//Clean up board
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] != NULL)
{
delete m_Board[j][i];
m_Board[j][i] = NULL;
}
}
}
//Clean up animations
for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end(); i++)
delete *i;
m_lSlideJoinAnimations.clear();
}
void Pony48Engine::resetBoard()
{
clearBoard();
//Start with 2 random tiles
for(int start = 0; start < 2; start++)
{
while(true)
{
int x = randInt(0, BOARD_WIDTH-1);
int y = randInt(0, BOARD_HEIGHT-1);
if(m_Board[x][y] != NULL) continue;
m_Board[x][y] = loadTile("res/tiles/2.xml");
break;
}
}
m_iScore = 0; //Reset score also
m_highestTile = NULL;
m_fLastMovedSec = getSeconds();
m_bHasBoredVox = false;
}
//Update slide-and-join animations if the to-join-to piece slid
void Pony48Engine::pieceSlid(int startx, int starty, int endx, int endy)
{
for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end(); i++)
{
if((*i)->destx == startx && (*i)->desty == starty)
{
(*i)->drawSlide.x += (startx - endx) * (TILE_WIDTH + TILE_SPACING);
(*i)->drawSlide.y -= (starty - endy) * (TILE_HEIGHT + TILE_SPACING);
(*i)->destx = endx;
(*i)->desty = endy;
}
}
}
#define PIECE_MOVE_SPEED 60.0
#define PIECE_APPEAR_SPEED 8.0
#define PIECE_BOUNCE_SPEED 4.5
#define PIECE_BOUNCE_SIZE TILE_WIDTH+TILE_SPACING*2.2
#define ARROW_SPEED 3.5
#define ARROW_RESET (TILE_WIDTH + TILE_SPACING)
#define SOUND_DIV_FAC 2.5f
void Pony48Engine::updateBoard(float32 dt)
{
m_fArrowAdd += dt * ARROW_SPEED;
if(m_fArrowAdd >= ARROW_RESET)
m_fArrowAdd -= ARROW_RESET;
//Check slide-and-join animations
for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end();)
{
if((*i)->drawSlide.y < 0)
{
(*i)->drawSlide.y += dt * PIECE_MOVE_SPEED;
if((*i)->drawSlide.y > 0)
(*i)->drawSlide.y = 0;
}
else if((*i)->drawSlide.y > 0)
{
(*i)->drawSlide.y -= dt * PIECE_MOVE_SPEED;
if((*i)->drawSlide.y < 0)
(*i)->drawSlide.y = 0;
}
if((*i)->drawSlide.x < 0)
{
(*i)->drawSlide.x += dt * PIECE_MOVE_SPEED;
if((*i)->drawSlide.x > 0)
(*i)->drawSlide.x = 0;
}
else if((*i)->drawSlide.x > 0)
{
(*i)->drawSlide.x -= dt * PIECE_MOVE_SPEED;
if((*i)->drawSlide.x < 0)
(*i)->drawSlide.x = 0;
}
if((*i)->drawSlide.x == 0 && (*i)->drawSlide.y == 0)
{
if((*i)->destx >= 0 && (*i)->desty >= 0)
{
//Hit the end; join with destination tile
if(m_Board[(*i)->destx][(*i)->desty] != NULL)
{
addScore((*i)->value * 2);
float32 xPos = ((float32)(*i)->destx - 2.0f);
if(xPos >= 0) xPos++;
xPos /= SOUND_DIV_FAC;
playSound("jointile", m_fSoundVolume, xPos);
if(m_highestTile == m_Board[(*i)->destx][(*i)->desty])
m_highestTile = NULL;
rumbleController(0.2, 0.1);
delete m_Board[(*i)->destx][(*i)->desty];
ostringstream oss;
oss << "res/tiles/" << min((*i)->value * 2, MAX_TILE_VALUE) << ".xml"; //"Duh, muffins" is highest possible tile
m_Board[(*i)->destx][(*i)->desty] = loadTile(oss.str());
m_Board[(*i)->destx][(*i)->desty]->drawSize.Set(TILE_WIDTH+0.001, TILE_HEIGHT+0.001); //Start bounce animation
m_Board[(*i)->destx][(*i)->desty]->iAnimDir = 1;
if(!(m_highestTile) || m_highestTile->value < m_Board[(*i)->destx][(*i)->desty]->value)
{
m_highestTile = m_Board[(*i)->destx][(*i)->desty];
m_newHighTile->firing = true;
if(m_highestTile->value == 2048)
achievementGet("wutup");
else if(m_highestTile->value == 4096)
achievementGet("ermahgerd");
}
}
else
errlog << "Err board[x][y] == NULL" << (*i)->destx << "," << (*i)->desty << endl;
}
else
errlog << "Err destx/y < 0: " << (*i)->destx << "," << (*i)->desty << endl;
delete (*i);
i = m_lSlideJoinAnimations.erase(i);
continue;
}
i++;
}
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] == NULL) continue;
//Check sliding animations
if(m_Board[j][i]->drawSlide.y < 0)
{
m_Board[j][i]->drawSlide.y += dt * PIECE_MOVE_SPEED;
if(m_Board[j][i]->drawSlide.y > 0)
m_Board[j][i]->drawSlide.y = 0;
}
if(m_Board[j][i]->drawSlide.y > 0)
{
m_Board[j][i]->drawSlide.y -= dt * PIECE_MOVE_SPEED;
if(m_Board[j][i]->drawSlide.y < 0)
m_Board[j][i]->drawSlide.y = 0;
}
if(m_Board[j][i]->drawSlide.x < 0)
{
m_Board[j][i]->drawSlide.x += dt * PIECE_MOVE_SPEED;
if(m_Board[j][i]->drawSlide.x > 0)
m_Board[j][i]->drawSlide.x = 0;
}
if(m_Board[j][i]->drawSlide.x > 0)
{
m_Board[j][i]->drawSlide.x -= dt * PIECE_MOVE_SPEED;
if(m_Board[j][i]->drawSlide.x < 0)
m_Board[j][i]->drawSlide.x = 0;
}
//Check appearing animations
if(m_Board[j][i]->drawSize.x < TILE_WIDTH)
{
m_Board[j][i]->drawSize.x += PIECE_APPEAR_SPEED * dt;
if(m_Board[j][i]->drawSize.x > TILE_WIDTH)
m_Board[j][i]->drawSize.x = TILE_WIDTH;
}
else if(m_Board[j][i]->drawSize.x > TILE_WIDTH) //And bouncing animations
{
m_Board[j][i]->drawSize.x += m_Board[j][i]->iAnimDir * PIECE_BOUNCE_SPEED * dt;
if(m_Board[j][i]->drawSize.x > PIECE_BOUNCE_SIZE)
m_Board[j][i]->iAnimDir = -1; //Reverse animation direction
else if(m_Board[j][i]->drawSize.x < TILE_WIDTH)
m_Board[j][i]->drawSize.x = TILE_WIDTH; //This also stops bouncing
}
if(m_Board[j][i]->drawSize.y < TILE_HEIGHT)
{
m_Board[j][i]->drawSize.y += PIECE_APPEAR_SPEED * dt;
if(m_Board[j][i]->drawSize.y > TILE_HEIGHT)
m_Board[j][i]->drawSize.y = TILE_HEIGHT;
}
else if(m_Board[j][i]->drawSize.y > TILE_HEIGHT)
{
m_Board[j][i]->drawSize.y += m_Board[j][i]->iAnimDir * PIECE_BOUNCE_SPEED * dt;
if(m_Board[j][i]->drawSize.y > PIECE_BOUNCE_SIZE)
m_Board[j][i]->iAnimDir = -1;
else if(m_Board[j][i]->drawSize.y < TILE_HEIGHT)
m_Board[j][i]->drawSize.y = TILE_HEIGHT;
}
}
}
}
void Pony48Engine::clearBoardAnimations()
{
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] == NULL) continue;
m_Board[j][i]->drawSlide.SetZero();
m_Board[j][i]->drawSize.Set(TILE_WIDTH, TILE_HEIGHT);
}
}
//Check slide-and-join anims
for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end();)
{
//Hit the end; join with destination tile
if((*i)->destx >= 0 && (*i)->desty >= 0)
{
if(m_Board[(*i)->destx][(*i)->desty] != NULL)
{
addScore((*i)->value * 2);
float32 xPos = ((float32)(*i)->destx - 2.0f);
if(xPos >= 0) xPos++;
xPos /= SOUND_DIV_FAC;
playSound("jointile", m_fSoundVolume, xPos);
if(m_highestTile == m_Board[(*i)->destx][(*i)->desty])
m_highestTile = NULL;
rumbleController(0.2, 0.1);
delete m_Board[(*i)->destx][(*i)->desty];
ostringstream oss;
oss << "res/tiles/" << min((*i)->value * 2, MAX_TILE_VALUE) << ".xml"; //"Duh, muffins" is highest possible tile
m_Board[(*i)->destx][(*i)->desty] = loadTile(oss.str());
m_Board[(*i)->destx][(*i)->desty]->drawSize.Set(TILE_WIDTH, TILE_HEIGHT); //Don't have a newly-created piece make an appear animation here
if(!(m_highestTile) || m_highestTile->value < m_Board[(*i)->destx][(*i)->desty]->value)
{
m_highestTile = m_Board[(*i)->destx][(*i)->desty];
m_newHighTile->firing = true;
if(m_highestTile->value == 2048)
achievementGet("wutup");
else if(m_highestTile->value == 4096)
achievementGet("ermahgerd");
}
}
}
//Wipe this out (Step through list this way, rather than incrementing i)
delete (*i);
i = m_lSlideJoinAnimations.erase(i);
}
}
#define TILEBG_DRAWZ 0.3
#define JOINANIM_DRAWZ 0.5
#define TILE_DRAWZ 0.7
#define MOVEARROW_DRAWZ 0.9
#define MOVEARROW_FADEOUTDIST (TILE_WIDTH * 0.5f)
#define MOVEARROW_FADEINDIST (TILE_WIDTH * 0.95f)
void Pony48Engine::drawBoard()
{
float fTotalWidth = BOARD_WIDTH * TILE_WIDTH + (BOARD_WIDTH + 1) * TILE_SPACING;
float fTotalHeight = BOARD_HEIGHT * TILE_HEIGHT + (BOARD_HEIGHT + 1) * TILE_SPACING;
//Fill in bg
fillRect(Point(-fTotalWidth/2.0, fTotalHeight/2.0), Point(fTotalWidth/2.0, -fTotalHeight/2.0), m_BoardBg); //Draw at z = 0
//Fill in bg for individual tiles
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
//Draw tile bg
glPushMatrix();
glTranslatef(0, 0, TILEBG_DRAWZ);
Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j,
fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i);
fillRect(ptDrawPos, Point(ptDrawPos.x + TILE_WIDTH, ptDrawPos.y - TILE_HEIGHT), m_TileBg[j][i]);
glPopMatrix();
}
}
//Draw joining-tile animations
for(list<TilePiece*>::iterator i = m_lSlideJoinAnimations.begin(); i != m_lSlideJoinAnimations.end(); i++)
{
Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * (*i)->destx,
fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * (*i)->desty);
glPushMatrix();
glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+(*i)->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+(*i)->drawSlide.y, JOINANIM_DRAWZ);
(*i)->draw();
glPopMatrix();
}
//Draw tiles themselves (separate loop because z-order alpha issues with animations)
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j,
fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i);
//Draw tile
if(m_Board[j][i] != NULL)
{
glPushMatrix();
glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+m_Board[j][i]->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+m_Board[j][i]->drawSlide.y, TILE_DRAWZ);
m_Board[j][i]->draw();
glPopMatrix();
}
}
}
//Draw particle fx for highest tile
if(m_highestTile != NULL)
{
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 0; j < BOARD_WIDTH; j++)
{
//Draw tile
if(m_Board[j][i] == m_highestTile)
{
Point ptDrawPos(-fTotalWidth/2.0 + TILE_SPACING + (TILE_SPACING + TILE_WIDTH) * j,
fTotalHeight/2.0 - TILE_SPACING - (TILE_SPACING + TILE_HEIGHT) * i);
glPushMatrix();
glTranslatef(ptDrawPos.x+TILE_WIDTH/2.0+m_Board[j][i]->drawSlide.x, ptDrawPos.y-TILE_HEIGHT/2.0+m_Board[j][i]->drawSlide.y, TILE_DRAWZ + 0.1);
m_newHighTile->img = m_highestTile->bg->img;
m_newHighTile->draw();
m_newHighTile->img = m_highestTile->seg->img;
m_newHighTile->draw();
glPopMatrix();
break;
}
}
}
}
//Draw arrows for direction the mouse will move the board
if(m_iMouseControl >= MOUSE_MOVE_TRIP_AMT && m_iCurMode == PLAYING)
{
glPushMatrix();
Point ptMoveDir = worldPosFromCursor(getCursorPos());
//Rotate first to simplify logic. Hooray!
switch(getDirOfVec2(ptMoveDir))
{
case UP:
glRotatef(90, 0, 0, 1);
break;
case DOWN:
glRotatef(-90, 0, 0, 1);
break;
case LEFT:
glRotatef(180, 0, 0, 1);
break;
}
//Determine the drawing alpha based on how far away from the center the mouse is
float32 fDestAlpha = min(fabs(ptMoveDir.Length() / (getCameraView().height() / 2.0)) - 0.4, 0.4);
//Draw 16 arrows pointing in the direction we'll move
for(int y = 0; y < BOARD_HEIGHT; y++)
{
for(int x = 0; x < BOARD_WIDTH; x++)
{
//Position to draw this arrow at
Point ptDrawPos(-fTotalWidth/2.0 + (TILE_SPACING + TILE_WIDTH) * x + TILE_WIDTH / 2.0 + TILE_SPACING + m_fArrowAdd,
fTotalHeight/2.0 - (TILE_SPACING + TILE_WIDTH) * y - TILE_HEIGHT / 2.0 - TILE_SPACING);
//If this arrow is reaching the end of its lifespan, fade out
float32 fDrawAlpha = fDestAlpha;
if(m_fArrowAdd >= MOVEARROW_FADEOUTDIST && x == BOARD_WIDTH - 1)
fDrawAlpha *= 1.0f - ((m_fArrowAdd - MOVEARROW_FADEOUTDIST) / MOVEARROW_FADEOUTDIST);
fDrawAlpha = min(fDrawAlpha, 1.0f);
fDrawAlpha = max(fDrawAlpha, 0.0f);
//Now draw
glColor4f(1,1,1,fDrawAlpha);
glPushMatrix();
glTranslatef(ptDrawPos.x, ptDrawPos.y, MOVEARROW_DRAWZ);
m_imgMouseMoveArrow->render(Point(1,1));
glPopMatrix();
//See if we should draw new arrow spawning
if(!x && (ARROW_RESET - m_fArrowAdd) <= MOVEARROW_FADEINDIST)
{
fDrawAlpha = fDestAlpha;
fDrawAlpha *= 1.0 - (ARROW_RESET - m_fArrowAdd) / MOVEARROW_FADEINDIST;
fDrawAlpha = min(fDrawAlpha, 1.0f);
fDrawAlpha = max(fDrawAlpha, 0.0f);
glColor4f(1,1,1,fDrawAlpha);
glPushMatrix();
glTranslatef(ptDrawPos.x - (TILE_SPACING + TILE_WIDTH), ptDrawPos.y, MOVEARROW_DRAWZ);
m_imgMouseMoveArrow->render(Point(1,1));
glPopMatrix();
}
}
}
glPopMatrix();
}
}
direction Pony48Engine::getDirOfVec2(Point ptVec)
{
float32 fAngle = atan2(ptVec.y, ptVec.x);
if(fAngle > PI/4.0 && fAngle < 3.0*PI/4.0) //Up
return UP;
else if(fAngle > -3.0*PI/4.0 && fAngle < -PI/4.0) //Down
return DOWN;
else if(fAngle < PI/4.0 && fAngle > -PI/4.0) //Right
return RIGHT;
else //Left
return LEFT;
//Unreachable
return UP;
}
TilePiece* Pony48Engine::loadTile(string sFilename)
{
TilePiece* ret = new TilePiece();
XMLDocument* doc = new XMLDocument();
int iErr = doc->LoadFile(sFilename.c_str());
if(iErr != XML_NO_ERROR)
{
errlog << "Error parsing XML file " << sFilename << ": Error " << iErr << endl;
delete doc;
return NULL;
}
XMLElement* root = doc->FirstChildElement("tile");
if(root == NULL)
{
errlog << "Error: No toplevel \"tile\" item in XML file " << sFilename << endl;
delete doc;
return NULL;
}
root->QueryIntAttribute("value", &ret->value);
vector<Image*> vImages;
for(XMLElement* img = root->FirstChildElement("img"); img != NULL; img = img->NextSiblingElement("img"))
{
const char* cPath = img->Attribute("path");
if(cPath != NULL)
vImages.push_back(getImage(cPath));
}
for(XMLElement* sound = root->FirstChildElement("sound"); sound != NULL; sound = sound->NextSiblingElement("sound"))
{
bool bPlaySoundImmediately = true;
const char* cType = sound->Attribute("type");
if(cType)
{
string sType = cType;
if(sType == "newhigh")
//I have no idea how this happens, but apparently the highest tile can be this before it even returns. Wat
bPlaySoundImmediately = ((m_highestTile == NULL) || (m_highestTile->value < ret->value));
}
//Save all sfx if there's multiple
vector<string> vSounds;
for(XMLElement* fx = sound->FirstChildElement("fx"); fx != NULL; fx = fx->NextSiblingElement("fx"))
{
const char* cPath = fx->Attribute("path");
const char* cName = fx->Attribute("name");
if(cPath && cName)
{
createSound(cPath, cName);
vSounds.push_back(cName);
}
}
//Play one of these randomly
if(vSounds.size() && bPlaySoundImmediately)
playSound(vSounds[randInt(0, vSounds.size() - 1)], m_fVoxVolume);
}
physSegment* tmpseg = new physSegment();
int which = randInt(0, vImages.size()-1);
tmpseg->img = vImages[which];
tmpseg->size = Point(TILE_WIDTH,TILE_HEIGHT);
ret->seg = tmpseg;
tmpseg = new physSegment();
tmpseg->img = getImage("res/tiles/tilebg.png");
tmpseg->size = Point(TILE_WIDTH,TILE_HEIGHT);
const char* cBgColor = root->Attribute("bgcolor");
if(cBgColor != NULL)
tmpseg->col = colorFromString(cBgColor);
ret->bg = tmpseg;
ret->origCol = tmpseg->col;
delete doc;
return ret;
}
bool Pony48Engine::movePossible()
{
//Have to take animations into account here, otherwise we could gameover when moves are still possible
return (m_lSlideJoinAnimations.size() || movePossible(UP) || movePossible(DOWN) || movePossible(LEFT) || movePossible(RIGHT));
}
bool Pony48Engine::movePossible(direction dir)
{
switch(dir)
{
case UP:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = 1; i < BOARD_HEIGHT; i++)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j][i-1] == NULL || m_Board[j][i-1]->value == m_Board[j][i]->value)
return true;
}
}
}
break;
case DOWN:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = BOARD_HEIGHT-2; i >= 0; i--)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j][i+1] == NULL || m_Board[j][i+1]->value == m_Board[j][i]->value)
return true;
}
}
}
break;
case LEFT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 1; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j-1][i] == NULL || m_Board[j-1][i]->value == m_Board[j][i]->value)
return true;
}
}
}
break;
case RIGHT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = BOARD_WIDTH-2; j >= 0; j--)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j+1][i] == NULL || m_Board[j+1][i]->value == m_Board[j][i]->value)
return true;
}
}
}
break;
}
return false;
}
void Pony48Engine::move(direction dir)
{
m_fLastMovedSec = getSeconds();
m_bHasBoredVox = false;
clearBoardAnimations(); //Wipe out any movement animations that are still playing
bool moved = false;
while(slide(dir)) //Slide as far as we can
moved = true;
moved = join(dir) || moved; //Join once
while(slide(dir)) //Slide again!
moved = true;
if(moved)
placenew(); //Create a new tile if we've successfully moved
}
bool Pony48Engine::join(direction dir)
{
bool mademove = false;
switch(dir)
{
case UP:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = 1; i < BOARD_HEIGHT; i++)
{
if(m_Board[j][i] != NULL && m_Board[j][i-1] != NULL)
{
if(m_Board[j][i-1]->value == m_Board[j][i]->value)
{
m_Board[j][i]->destx = j;
m_Board[j][i]->desty = i-1;
m_Board[j][i]->drawSlide.y -= TILE_HEIGHT + TILE_SPACING;
m_lSlideJoinAnimations.push_back(m_Board[j][i]);
m_Board[j][i] = NULL;
mademove = true;
}
}
}
}
break;
case DOWN:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = BOARD_HEIGHT-2; i >= 0; i--)
{
if(m_Board[j][i] != NULL && m_Board[j][i+1] != NULL)
{
if(m_Board[j][i+1]->value == m_Board[j][i]->value)
{
m_Board[j][i]->destx = j;
m_Board[j][i]->desty = i+1;
m_Board[j][i]->drawSlide.y += TILE_HEIGHT + TILE_SPACING;
m_lSlideJoinAnimations.push_back(m_Board[j][i]);
m_Board[j][i] = NULL;
mademove = true;
}
}
}
}
break;
case LEFT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 1; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] != NULL && m_Board[j-1][i] != NULL)
{
if(m_Board[j-1][i]->value == m_Board[j][i]->value)
{
m_Board[j][i]->destx = j-1;
m_Board[j][i]->desty = i;
m_Board[j][i]->drawSlide.x += TILE_WIDTH + TILE_SPACING;
m_lSlideJoinAnimations.push_back(m_Board[j][i]);
m_Board[j][i] = NULL;
mademove = true;
}
}
}
}
break;
case RIGHT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = BOARD_WIDTH-2; j >= 0; j--)
{
if(m_Board[j][i] != NULL && m_Board[j+1][i] != NULL)
{
if(m_Board[j+1][i]->value == m_Board[j][i]->value)
{
m_Board[j][i]->destx = j+1;
m_Board[j][i]->desty = i;
m_Board[j][i]->drawSlide.x -= TILE_WIDTH + TILE_SPACING;
m_lSlideJoinAnimations.push_back(m_Board[j][i]);
m_Board[j][i] = NULL;
mademove = true;
}
}
}
}
break;
}
return mademove;
}
bool Pony48Engine::slide(direction dir)
{
bool mademove = false;
switch(dir)
{
case UP:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = 1; i < BOARD_HEIGHT; i++)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j][i-1] == NULL)
{
m_Board[j][i-1] = m_Board[j][i];
m_Board[j][i] = NULL;
mademove = true;
m_Board[j][i-1]->drawSlide.y -= TILE_HEIGHT + TILE_SPACING;
pieceSlid(j, i, j, i-1);
}
}
}
}
break;
case DOWN:
for(int j = 0; j < BOARD_WIDTH; j++)
{
for(int i = BOARD_HEIGHT-2; i >= 0; i--)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j][i+1] == NULL)
{
m_Board[j][i+1] = m_Board[j][i];
m_Board[j][i] = NULL;
mademove = true;
m_Board[j][i+1]->drawSlide.y += TILE_HEIGHT + TILE_SPACING;
pieceSlid(j, i, j, i+1);
}
}
}
}
break;
case LEFT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = 1; j < BOARD_WIDTH; j++)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j-1][i] == NULL)
{
m_Board[j-1][i] = m_Board[j][i];
m_Board[j][i] = NULL;
mademove = true;
m_Board[j-1][i]->drawSlide.x += TILE_WIDTH + TILE_SPACING;
pieceSlid(j, i, j-1, i);
}
}
}
}
break;
case RIGHT:
for(int i = 0; i < BOARD_HEIGHT; i++)
{
for(int j = BOARD_WIDTH-2; j >= 0; j--)
{
if(m_Board[j][i] != NULL)
{
if(m_Board[j+1][i] == NULL)
{
m_Board[j+1][i] = m_Board[j][i];
m_Board[j][i] = NULL;
mademove = true;
m_Board[j+1][i]->drawSlide.x -= TILE_WIDTH + TILE_SPACING;
pieceSlid(j, i, j+1, i);
}
}
}
}
break;
}
return mademove;
}
void Pony48Engine::placenew()
{
ostringstream oss;
oss << "res/tiles/" << randInt(1,2) * 2 << ".xml";
if(movePossible()) //Make sure there aren't no blank spaces or something
{
while(true) //Could possibly hang here for a while
{
int x = randInt(0, BOARD_WIDTH-1);
int y = randInt(0, BOARD_HEIGHT-1);
if(m_Board[x][y] != NULL) continue;
m_Board[x][y] = loadTile(oss.str());
break;
}
}
}
void Pony48Engine::spawnScoreParticles(uint32_t amt)
{
ostringstream oss;
oss << "res/particles/" << amt << ".xml";
if(m_ScoreParticles.count(oss.str()))
{
m_ScoreParticles[oss.str()]->firing = true;
}
else if(ttvfs::FileExists(oss.str().c_str()))
{
ParticleSystem* pSys = new ParticleSystem();
pSys->fromXML(oss.str());
pSys->init();
pSys->firing = true;
m_ScoreParticles[oss.str()] = pSys;
}
}
void Pony48Engine::addScore(uint32_t amt)
{
m_iScore += amt;
if(m_iScore > m_iHighScore)
m_iHighScore = m_iScore;
if(m_iScore > DEV_SCORE)
achievementGet("hiscore");
spawnScoreParticles(amt);
}