-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMain.cpp
1249 lines (1150 loc) · 40 KB
/
StateMain.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StateMain.h"
StateMain::StateMain()
{
isPaused = false;
plagueRate = 6;
mutationRate = 4;
//decayRate = 10;
variables.resize(2); // We need 2 slots... one for AI true/false and the other for player colour
// Slot 0 is for Colour
choice = -1;
longSnake = 0;
}
void StateMain::loadMenu()
{
menu.clear();
menu.setMenuStart(GFX::getXResolution()*0.151690141f,GFX::getYResolution()*0.333f);
menu.loadFont("font/origami.ttf", 16);
menu.setTextColour(Colour(YELLOW));
menu.setSelection(1);
menu.addItem(MENU_STRING_ITEM);
menu.setMenuItemText("Pause Menu");
menu.setIsSelectable(false);
menu.addItem(MENU_STRING_ITEM);
menu.setMenuItemText("Resume");
menu.setIsSelectable(true);
menu.addItem(MENU_STRING_ITEM);
menu.setMenuItemText("Quit to Title");
menu.setIsSelectable(true);
menu.addItem(MENU_STRING_ITEM);
menu.setMenuItemText("Quit Game");
menu.setIsSelectable(true);
pauseRect.setColour(Colour(0,0,0,64));
pauseRect.setPosition(GFX::getXResolution()*0.151690141f,GFX::getYResolution()*0.333f);
pauseRect.setDimensions(222,80);
}
void StateMain::loadMusic()
{
string dir = "music/";
string ext = ".ogg";
music.loadMusic(dir+"water"+ext);
music.setLooping(true);
music.play();
}
void StateMain::loadSounds()
{
string dir = "sounds/";
string ext = ".wav";
eatSound.loadSound(dir+"blurp" + ext);
splitSound.loadSound(dir+"bloop" + ext);
plagueSound.loadSound(dir+"plague" + ext);
}
void StateMain::loadSprites()
{
// Setup sprites
string dir = "images/";
string ext = ".png";
redSprite.loadFrames(dir + "redamoeba" + ext, 14, 1);
redSprite.setFrameRate(FIFTEEN_FRAMES);
redSprite.setLooping(true);
// Now we have loaded the sprite get the width squared
widthSq = redSprite.getWidth();
widthSq *= widthSq;
string col = "p1/";
for(uint i=1; i < 11; ++i)
{
string temp = dir;
p1indicator.loadFrame(temp.append(col).append(StringUtility::leadingZero(i)).append(ext));
}
p1indicator.setFrameRate(FIFTEEN_FRAMES);
p1indicator.setLooping(true);
greenSprite.loadFrames(dir + "greenamoeba" + ext, 14, 1);
greenSprite.setFrameRate(FIFTEEN_FRAMES);
greenSprite.setLooping(true);
blueSprite.loadFrames(dir + "blueamoeba" + ext, 14, 1);
blueSprite.setFrameRate(FIFTEEN_FRAMES);
blueSprite.setLooping(true);
// Setup background
background.loadFrames(dir+"background" + ext, 8, 2);
background.setFrameRate(FIFTEEN_FRAMES);
background.setLooping(true);
backPos[0] = Vector2di(0,0);
backPos[1] = Vector2di(160,0);
backPos[2] = Vector2di(0,120);
backPos[3] = Vector2di(160,120);
// Setup hazard animations
ext=".gif";
plagueSprite.loadFrames(dir + "plague" + ext,4,1);
plagueSprite.setFrameRate(FIFTEEN_FRAMES);
plagueSprite.setLooping(8);
plagueSprite.setX(270);
mutationSprite.loadFrames(dir + "mutation" + ext,7,1);
mutationSprite.setFrameRate(FIFTEEN_FRAMES);
mutationSprite.setLooping(4);
mutationSprite.setX(290);
}
void StateMain::setupObjects()
{
// Setup node
food = food2 = NULL;
food = new Node(50,50);
food2 = new Node(50,50);
foodEaten = false;
foodEaten2 = false;
food->randomisePos();
food->randomiseColour();
food2->randomiseColour();
food2->randomisePos();
setupSnakes();
setupText();
for (uint i = 0; i < 4; ++i)
collisionLists[i].clear();
godManager.setInvincibilityLimit(80);
godManager.resize(snakes.size());
}
void StateMain::setupSnakes()
{
// Check for values passed from title screen
int type = CUSTOM_NODE;
if (variables.size())
{
if (variables[0].hasInt()) // Get the colour
{
// We get the variables
type = variables[0].getInt();
}
}
// Setup Snakes
snakes.push_back(NULL);
snakes.at(0) = new Snake(Node(50,150));
snakes.at(0)->setVelocityXY(0,0);
snakes.at(0)->setHeadColour(type);
// Setup enemies
snakes.push_back(NULL);
snakes.at(1) = new Snake(Node(50,50));
snakes.push_back(NULL);
snakes.at(2) = new Snake(Node(150,150));
// Also depending on type we need to add more nodes.
Node *temp = NULL;
temp = new Node(100,100);
temp->setColour(type);
uint greenStart = 4;
uint redStart = 1;
uint blueStart = 3;
if (type == RED_NODE)
{
// Setup the green and blue enemies
snakes.at(1)->setHeadColour(GREEN_NODE);
snakes.at(1)->setVelocityXY(1,-1);
temp->setColour(GREEN_NODE);
for (uint i = 0; i < greenStart; ++i)
snakes.at(1)->eat(temp);
snakes.at(1)->setHeadColour(BLUE_NODE);
snakes.at(1)->setVelocityXY(-1,-1);
temp->setColour(BLUE_NODE);
for (uint i = 0; i < blueStart; ++i)
snakes.at(2)->eat(temp);
// Setup player
temp->setColour(RED_NODE);
for (uint i = 0; i < redStart; ++i)
snakes.at(0)->eat(temp);
}
else if (type == GREEN_NODE)
{
// Setup the red and blue enemies
snakes.at(1)->setHeadColour(RED_NODE);
snakes.at(1)->setVelocityXY(1,-1);
temp->setColour(RED_NODE);
for (uint i = 0; i < redStart; ++i)
snakes.at(1)->eat(temp);
snakes.at(2)->setHeadColour(BLUE_NODE);
snakes.at(2)->setVelocityXY(-1,-1);
temp->setColour(BLUE_NODE);
for (uint i = 0; i < blueStart; ++i)
snakes.at(2)->eat(temp);
temp->setColour(GREEN_NODE);
for (uint i = 0; i < greenStart; ++i)
snakes.at(0)->eat(temp);
}
else if (type == BLUE_NODE)
{
// Setup the red and green enemies
snakes.at(1)->setHeadColour(GREEN_NODE);
snakes.at(1)->setVelocityXY(1,-1);
temp->setColour(GREEN_NODE);
for (uint i = 0; i < greenStart; ++i)
snakes.at(1)->eat(temp);
snakes.at(2)->setHeadColour(RED_NODE);
snakes.at(2)->setVelocityXY(-1,-1);
temp->setColour(RED_NODE);
for (uint i = 0; i < redStart; ++i)
snakes.at(2)->eat(temp);
temp->setColour(BLUE_NODE);
for (uint i = 0; i < blueStart; ++i)
{
snakes.at(0)->eat(temp);
}
}
// Now set snake 0 to be player controlled
if (variables[1].hasInt())
{
if(variables[1].getInt() != 1)
{
snakes.at(0)->disableBrain();
snakes.at(0)->setMode(NONE);
// And position p1indicator to the snake[0]
p1indicator.setPosition(snakes.at(0)->getNode(0).getPos().x-4,snakes.at(0)->getNode(0).getPos().y-4);
}
}
else
{
switch (snakes.at(0)->getHeadColour())
{
case RED_NODE:
snakes.at(0)->setMode(AGRESSIVE);
break;
case GREEN_NODE:
snakes.at(0)->setMode(NEUTRAL);
break;
case BLUE_NODE:
snakes.at(0)->setMode(PASSIVE);
break;
}
}
for(int i=0;i<3;i++)
{
if(i == 0)
{
if (variables[1].hasInt())
{
if(variables[1].getInt() != 1)
{
snakes.at(i)->disableBrain();
snakes.at(i)->setMode(NONE);
// And position p1indicator to the snake[0]
p1indicator.setPosition(snakes.at(i)->getNode(0).getPos().x-4,snakes.at(i)->getNode(0).getPos().y-4);
}
}
else
{
switch (snakes.at(i)->getHeadColour())
{
case RED_NODE:
snakes.at(i)->setMode(AGRESSIVE);
break;
case GREEN_NODE:
snakes.at(i)->setMode(NEUTRAL);
break;
case BLUE_NODE:
snakes.at(i)->setMode(PASSIVE);
break;
}
}
}
}
if (temp)
{
delete temp;
temp = NULL;
}
}
void StateMain::setupText()
{
text.loadFont("font/origami.ttf", 12);
text.setPosition(5,5);
text.setRelativity(true);
text.setColour(Colour(WHITE));
}
void StateMain::init()
{
playPlagueAnimation = false;
playMutationAnimation = false;
loadMusic();
loadMenu();
loadSounds();
loadSprites();
setupObjects();
}
void StateMain::clear()
{
if (food)
{
delete food;
food = NULL;
}
if (food2)
{
delete food2;
food2 = NULL;
}
}
StateMain::~StateMain()
{
clear();
for(int i = snakes.size()-1; i>=0; --i)
{
if(snakes.at(i))
{
delete snakes.at(i);
}
snakes.at(i) = NULL;
}
}
void StateMain::render()
{
// Draw the background first
int currFrame = background.getCurrentFrame();
for(int i = 3; i >= 0; i--)
{
background.setPosition(backPos[i]);
background.setCurrentFrame(NumberUtility::wrapValue(currFrame + i, 16));
background.render();
}
background.setCurrentFrame(currFrame);
// We check if the food exists and if it does we draw it.
if (!foodEaten)
food->render(redSprite,greenSprite,blueSprite);
if (!foodEaten2)
food2->render(redSprite,greenSprite,blueSprite);
// draw all snakes
uint numSnakes = (uint)snakes.size();
for (int i = numSnakes-1; i >= 0; i--)
snakes.at(i)->render(redSprite,greenSprite,blueSprite);
if (variables[1].hasInt()) // Check for AI mode
{
if(variables[1].getInt() != 1)
{
// Display p1 indicator
p1indicator.render();
snakes.at(0)->disableBrain();
snakes.at(0)->setMode(NONE);
}
}
else
{
switch (snakes.at(0)->getHeadColour())
{
case RED_NODE:
snakes.at(0)->setMode(AGRESSIVE);
break;
case GREEN_NODE:
snakes.at(0)->setMode(NEUTRAL);
break;
case BLUE_NODE:
snakes.at(0)->setMode(PASSIVE);
break;
}
}
// Display Game Info
text.setPosition(5,5);
//text.print(screen,"CromoZomes:");
//text.print(screen, (int)numSnakes);
text.print((string)"Score:");
text.print(score.getScore());
text.print((string)" Mult: ");
text.print(score.getMultiplier());
text.print((string)"X");
/// EFFECT ANIMATIONS
if (playPlagueAnimation)
{
plagueSprite.render();
text.setPosition(110,115);
text.setColour(Colour(GREEN));
text.print((string)"THE PLAGUE!");
text.setColour(Colour(WHITE));
}
if (playMutationAnimation)
{
mutationSprite.render();
text.setPosition(110,115);
text.setColour(Colour(GREEN));
text.print((string)"MU");
text.setColour(Colour(RED));
text.print((string)"TA");
text.setColour(Colour(BLUE));
text.print((string)"TI");
text.setColour(Colour(PURPLE));
text.print((string)"ON");
text.setColour(Colour(ORANGE));
text.print((string)"!!");
text.setColour(Colour(WHITE));
}
}
void StateMain::update()
{
if(!isPaused)
godManager.update();
// animate sprite
redSprite.update();
blueSprite.update();
greenSprite.update();
p1indicator.update();
for(int i = 3; i >= 0; i--)
background.update();
if (playPlagueAnimation)
plagueSprite.update();
if (playMutationAnimation)
mutationSprite.update();
if (plagueSprite.hasFinished())
{
playPlagueAnimation = false;
plagueSprite.rewind();
}
if (mutationSprite.hasFinished())
{
playMutationAnimation = false;
mutationSprite.rewind();
}
//staleMate(); // Workaround for stalemate situations
uint numSnakes = (uint)snakes.size();
if(numSnakes==0)
return;
// We need to find the longest snake... we should do it in these loops as we are already running through them
int snakeLength = 0;
longSnake = 0; // Default to snake 0!
for (uint i = 0; i < numSnakes; ++i)
{
uint snSize = snakes.at(0)->size();
if (snSize > snakeLength)
{
snakeLength=snSize;
longSnake = i;
}
if (i>0)
{
uint col = snakes.at(0)->getHeadColour();
Vector2df closest(0,0);
uint part;
uint cLSize;
randGen.setLimits(0,snakes.size() - 1);
// Get the partition of the current AI snake
part = partition.getPartition(snakes.at(0)->getXY());
cLSize = collisionLists[part].size();
switch (col)
{
case RED_NODE:
if (snSize >= 5) //if the snake is longer than 5 nodes
{
if (cLSize > 1) //if it is still in the colission lists
{
for (int j=0;j<cLSize-1;j++)
{
if (i == j) //if we are checking it against itself
{
closest = food->getPos();
continue;
}else
{
uint snake = collisionLists[part][j]; //find the closest snake
if (snake > snakes.size() - 1)
{
cLSize--; //this one has already been processed
continue;
}
if (snakes.at(snake)->size() < 1)
{
cLSize--; //as has this one
continue;
}
// Get the tail segment of the closest snake
Vector2df snakePos = snakes.at(snake)->getNode(snakes.at(snake)->size()-1).getPos();
//get the distance between this snakes head and the closests tail
Vector2df difference(snakePos - closest);
if (difference.lengthSquared() < widthSq)
{
//tell this snake that another is close
closest = snakePos;
}
}
}
}
else
{
//or tell it to hunt for food
closest = food->getPos();
}
}
else
{
//if the snake is still too small, tell it to hunt for food
closest = food->getPos();
}
if(i>= snakes.size())
continue;
snakes.at(i)->setMode(AGRESSIVE);
snakes.at(i)->setWaypoint(closest);
break;
case GREEN_NODE:
//Green snakes always hunt for food
if(i>= snakes.size())
continue;
snakes.at(i)->setMode(NEUTRAL);
snakes.at(i)->setWaypoint(food->getPos());
break;
case BLUE_NODE:
if (snSize < 8) //if the snake is less than 8 segments long
{
if (cLSize > 1)//and its on the colission list
{
for (int j=0;j<cLSize-1;j++)
{
if (i == j)
{
//do nothing, have the sanke continue on its way
continue;
}else
{
uint snake = collisionLists[part][j];
if (snake > snakes.size() - 1)
{
cLSize--;
continue;
}
if (snakes.at(snake)->size() < 1)
{
cLSize--;
continue;
}
// We want the blue guys to run away from the head segment!
Vector2df snakePos = snakes.at(snake)->getXY();
snakePos -= closest;
if (snakePos.lengthSquared() < widthSq)
{
closest = snakePos; //run away from the nearest snake
//blue snakes are passive when other snakes are around;
if(i>= snakes.size())
continue;
snakes.at(i)->setMode(PASSIVE);
snakes.at(i)->setWaypoint(closest);
}
}
}
}
}
else
{
//but they can also hunt for food
closest = food->getPos();
if(i>= snakes.size())
continue;
snakes.at(i)->setMode(NEUTRAL);
snakes.at(i)->setWaypoint(closest);
}
break;
}
}
if(i>= snakes.size())
continue;
snakes.at(i)->update();
//processCollisionLists();
newCollisions();
}//END FOR
// And position p1indicator to the snake[0]
p1indicator.setPosition(snakes.at(0)->getNode(0).getX()-4,snakes.at(0)->getNode(0).getY()-4);
// If any of the snakes get too long we trigger a decay
// The longest snake is worked in the earlier AI loop to avoid having to traverse the array again!
// update effects
numSnakes = snakes.size();
if (numSnakes > plagueRate)
{
// reset score multiplier
plagueSound.play();
//score.setMultiplier(1);
++plagueRate;
uint halfRate = plagueRate*0.5f;
for (uint i = 0; i < halfRate; ++i)
{
delete snakes.back();
snakes.back() = NULL;
snakes.pop_back();
--numSnakes;
}
playPlagueAnimation = true;
}
else if (numSnakes > mutationRate)
{
plagueSound.play();
//score.changeScore(10);
mutationRate+=2;
uint iMax = (uint)snakes.size();
// kill off some snakes if we can
// if (iMax > 4)
// {
// // kill three snakes
// for (uint i =0; i < 3; i++)
// snakes.pop_back();
//
// iMax = (uint)snakes.size();
// }
for (uint i = 0; i < iMax; i++)
{
snakes.at(i)->mutation();
if(snakes.at(i)->getAIMode() != NONE)
{
uint col = snakes.at(i)->getHeadColour();
switch(col)
{
case RED_NODE:
snakes.at(i)->setMode(AGRESSIVE);
break;
case BLUE_NODE:
snakes.at(i)->setMode(PASSIVE);
break;
case GREEN_NODE:
snakes.at(i)->setMode(NEUTRAL);
break;
}
}
}
playMutationAnimation = true;
}
rebuildCollisionLists();
}
void StateMain::userInput()
{
input->update();
#ifdef PLATFORM_PC
if(input->isQuit())
nullifyState();
#endif
// Check for start button to pause game
if (input->isStart())
{
pauseToggle();
input->resetKeys();
}
if (variables[1].hasInt()) // Check for AI mode
{
if(variables[1].getInt() != 1)
{
if (input->isUp())
snakes.at(0)->setVelocityXY(0,-1);
if (input->isDown())
snakes.at(0)->setVelocityXY(0,1);
if (input->isLeft())
snakes.at(0)->setVelocityXY(-1,0);
if (input->isRight())
snakes.at(0)->setVelocityXY(1,0);
#ifndef PLATFORM_WII
// Check diagonals
if (input->isUpLeft())
snakes.at(0)->setVelocityXY(-1,-1);
if (input->isUpRight())
snakes.at(0)->setVelocityXY(1,-1);
if (input->isDownLeft())
snakes.at(0)->setVelocityXY(-1,1);
if (input->isDownRight())
snakes.at(0)->setVelocityXY(1,1);
#endif
// Check for DPAD "Diagonals"
if (input->isUp() && input->isLeft())
snakes.at(0)->setVelocityXY(-1,-1);
if (input->isUp() && input->isRight())
snakes.at(0)->setVelocityXY(1,-1);
if (input->isDown() && input->isLeft())
snakes.at(0)->setVelocityXY(-1,1);
if (input->isDown() && input->isRight())
snakes.at(0)->setVelocityXY(1,1);
}
}
#ifdef PLATFORM_GP2X
// adjust volume
uint volume = music.getVolume();
if (input->isVolumeUp() && volume < 128)
music.setVolume(volume+2);
else if (input->isVolumeDown() && volume > 0)
music.setVolume(volume-2);
#endif
}
void StateMain::rebuildCollisionLists()
{
// Clear collision lists
for (uint i = 0; i < 4; ++i)
collisionLists[i].clear();
// work out which quarter of the screen the food is in
Vector2df foodPos = food->getPos();
Vector2df foodPos2 = food2->getPos();
uint foodQuarter = partition.getPartition(foodPos);
uint foodQuarter2 = partition.getPartition(foodPos2);
uint currentPartition = UNKNOWN_PARTITION;
uint iMax = snakes.size();
godManager.resize(iMax);
// Now rebuild collisionLists
for (uint i = 0; i < iMax; ++i)
{
Vector2df currPos = snakes.at(i)->getXY();
currentPartition = partition.getPartition( currPos);
// Do food check
if (currentPartition == foodQuarter)
{
currPos -= foodPos;
if (currPos.lengthSquared() <= widthSq)
{
foodEaten = true;
}
if (foodEaten)
{
// check for snake[0]
if (i == 0)
{
score.changeScore(food->getColour()+1);
}
snakes.at(i)->eat(food);
eatSound.play();
food->randomisePos();
foodPos = food->getPos();
foodQuarter = partition.getPartition(foodPos);
food->randomiseColour();
foodEaten = false;
}
}
else if (currentPartition == foodQuarter2)
{
currPos -= foodPos2;
if (currPos.lengthSquared() <= widthSq)
{
foodEaten2= true;
}
if (foodEaten2)
{
// check for snake[0]
if (i == 0)
{
score.changeScore(food2->getColour()+1);
}
snakes.at(i)->eat(food2);
eatSound.play();
food2->randomisePos();
foodPos2 = food2->getPos();
foodQuarter2 = partition.getPartition(foodPos2);
food2->randomiseColour();
foodEaten2 = false;
}
}
// Partition snakes
if (currentPartition == TOP_LEFT)
collisionLists[TOP_LEFT].push_back(i);
else if (currentPartition == TOP_RIGHT)
collisionLists[TOP_RIGHT].push_back(i);
else if (currentPartition == BOTTOM_LEFT)
collisionLists[BOTTOM_LEFT].push_back(i);
else
collisionLists[BOTTOM_RIGHT].push_back(i); //BOTTOM_RIGHT
}
}
void StateMain::checkSubList(ScreenPartition& subList, CRuint quad)
{
int maxSnakes = snakes.size();
/// Run through the available snakes in this quadrant
int CLmax = collisionLists[quad].size()-1;
for(int CL = CLmax; CL >= 0; --CL)
{
int currSnake = collisionLists[quad][CL];
if(currSnake >= maxSnakes)
collisionLists[quad][CL] = -1; // Snake out of bounds, must be dead.
if(collisionLists[quad][CL] == -1) // If snake is dead, we don't need to do further tests!
continue;
/// Get snakes to test in this sub-quadrant
Vector2df currPos = snakes.at(currSnake)->getXY();
int currSubQuad = subList.getPartition(currPos);
for(int sQ = CLmax; sQ >= 0; --sQ)
{
if(sQ == CL) // Don't test against self
continue;
int enemySnake = collisionLists[quad][sQ];
if(enemySnake == -1) // snake is ded snake
continue;
if(currSnake == enemySnake)
continue;
if(currSubQuad == subList.getPartition(snakes.at(enemySnake)->getXY()))
{
/// snakes are in the same quad, do distance collision tests
distanceCollision(currSnake,enemySnake,CL,sQ,quad);
}
}
}
}
void StateMain::cleanDead()
{
for(int currSnake = snakes.size()-1; currSnake >= 0; --currSnake)
{
if(snakes.at(currSnake)->size() == 0)
{
/// Check if player is dead
if(currSnake == 0)
{
/// if it is a human player!
if (variables[1].hasInt() && variables[1].getInt() != 1)
{
variables[1] = 0;
// push score through and add it to variable stack
// We use the same slot as AI true/false since AI true means no high score!
variables[1].setInt((int)score.getScore());
// Now change to gameover state
setNextState(STATE_GAMEOVER);
}
}
delete snakes.at(currSnake);
snakes.at(currSnake) = NULL;
snakes.erase(snakes.begin() + currSnake);
}
}
}
void StateMain::distanceCollision(CRint curr, CRint enem, CRint currIndex, CRint enemIndex, CRuint quad)
{
/// first check if either snake is actually dead
int currMax = snakes.at(curr)->size();
int enemMax = snakes.at(enem)->size();
if(currMax <= 0) // flag as dead - can't eat or be eaten!
collisionLists[quad][currIndex] = -1;
if(enemMax <= 0) // flag as dead
collisionLists[quad][enemIndex] = -1;
if(collisionLists[quad][currIndex] == -1 || collisionLists[quad][enemIndex] == -1)
return;
/// Check invincibility status of eatee (can't eat if invincible)
if(godManager.getIsInvincible(collisionLists[quad][enemIndex]))
return;
/// If the snake is alive and the eatee is not invincible then we will see if we can eat him
// Get eater's head position
Vector2di diff = snakes.at(curr)->getXY();
// compare head position to all nodes on eatee
for(int n = snakes.at(enem)->size()-1; n >=0; --n)
{
Vector2di temp(diff.x,diff.y);
temp -= snakes.at(enem)->getNode(n).getPos();
if (temp.lengthSquared() <= widthSq-32)
{
if (curr == 0) // If the eater is the player
score.changeScore(snakes.at(enem)->getNode(n).getColour()+1);
if (enem == 0) // If the eatee is the player
score.setMultiplier(1);
eatSound.play();
snakes.at(curr)->eat(&snakes.at(enem)->getNode(n));
snakes.at(enem)->eraseNode(n);
if(snakes.at(enem)->size() <= 0)
collisionLists[quad][enemIndex] = -1;
else
godManager.setIsInvincible(enem);
// We don't want to eat multiple nodes simultaneously.
if (n == 0) // If the head is eaten, you're dead!
{
// compare lengths of snakes to see which one should die
if(currMax > enemMax)
{
if (enem == 0)
{
if (variables[1].hasInt() && variables[1].getInt() != 1)
{
variables[1] = 0;
// push score through and add it to variable stack
// We use the same slot as AI true/false since AI true means no high score!
variables[1].setInt((int)score.getScore());
// Now change to gameover state
setNextState(STATE_GAMEOVER);
}
}
}
}
break;
}
}
}
void StateMain::newCollisions()
{
// Check through each list
for(int l = 3; l>= 0; --l)
{
ScreenPartition tempSP;
if(l == BOTTOM_LEFT)
tempSP.setPartition(Vector2di(80,60));
else if(l == BOTTOM_RIGHT)
tempSP.setPartition(Vector2di(240,60));
else if(l == TOP_RIGHT)
tempSP.setPartition(Vector2di(240,180));
else // TOP_LEFT
tempSP.setPartition(Vector2di(80,180));
checkSubList(tempSP, l);
}
/// Once all collisions have been tested, clean out all snakes marked as dead
cleanDead();
checkLevelUp();
}
void StateMain::processCollisionLists()
{
Vector2di diff;
uint iMax = (uint)snakes.size();
godManager.resize(iMax);
// run through all lists
for (uint i = 0; i < 4; i++)
{
// run through snakes in current list
uint jMax = (uint)collisionLists[i].size();
for (uint j = 0; j < jMax; j++)
{
//comparing against snakes in same partition
uint jMax = (uint)collisionLists[i].size();
for (uint k = 0; k < jMax; k++)
{
// Guard against self collisions
if (j == k)
continue;
// Guard against j and k being out of bounds
if (k >= collisionLists[i].size() || j >= collisionLists[i].size())
continue;
if (collisionLists[i][k] >= snakes.size() || collisionLists[i][j] >= snakes.size())
continue;
// Guard against snakes already removed from collision lists
if (collisionLists[i][j] == -1 || collisionLists[i][k] == -1)
continue;
// Check if a snake is temprorarily invincible
if(godManager.getIsInvincible(collisionLists[i][k]))
continue;
uint numNodes = snakes.at(collisionLists[i][k])->size();
/* if the number of nodes on any snakes is greater than the trigger we should randomly reduce the current
number of nodes in all snakes to keep tabs on game speed.