-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumBlockGame.cs
1403 lines (1237 loc) · 50 KB
/
NumBlockGame.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace numBlock
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class NumBlockGame : Microsoft.Xna.Framework.Game
{
private static int SCREEN_WIDTH = 480;
private static int SCREEN_HEIGHT = 800;
public static int BOARD_OFFSET_X = 46;
public static int BOARD_OFFSET_Y = 150;
public static int BOARD_OFFSET_SLOT_Y = 94;
public static int REMOVE_ANIMATION_LENGTH = 550;
public static int MAX_LOCAL_HIGH_SCORES = 10;
public static int NUM_COLUMNS = 8;
public static int NUM_ROWS = 8;
public static Random rand = new Random();
GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public SpriteFont font;
Matrix worldToScreenMatrix;
Rectangle worldRect;
/*
* Assets
*/
Texture2D background;
Texture2D splashScreen;
Texture2D menuSelectIcon;
Texture2D gameOver;
Texture2D highScoresBg;
Texture2D achieveScreenBg;
Texture2D instructionsScreenBg;
Texture2D checkMarkTexture;
Texture2D pauseOverlay;
Texture2D gameOverHighScoreOverlay;
List<Texture2D> blockIcons = null;
List<Texture2D> alphaTextures = null;
SoundEffect blockLand = null;
SoundEffect ping = null;
SoundEffect achSound = null;
public SoundEffect unlock = null;
Texture2D charSelectArrows = null;
public Texture2D achBG = null;
public List<Texture2D> achIcons = null;
public Texture2D achIconNone = null;
/*
* Gameplay attributes
*/
public List<List<NumberBlock>> gameBoard;
public KeyboardState prevKeyState;
public MouseState prevMouseState;
public List<ScoreSprite> scoreSprites;
public List<AchievementSpriteComponent> achSpriteList;
public AchievementHub achievementHub;
public NumberBlock nextBlock;
public List<NumberBlock> nextBlockQueue;
public int nextBlockColumn = 0;
public List<NumberBlock> blocksToRemove = null;
StringBuilder comboString = null;
private int droppingTimeout = 0;
private int removingTimeout = 0;
public int score = 0;
public int multiplier = 1;
public int comboScore = 0;
public int comboRecord = 0;
public int numMovesPerLevel = 0;
public int numMovesLeft = 0;
public int currLevel = 1;
GameSettings settings;
/*
* UI Fields
*/
public char[] initials = null;
public int currInitial = 0;
//public int mainMenuOptionIndex = 0;
public int tempIndex = 1;
bool saveHighScore = false;
bool unfocused = false;
//bool paused = false;
//int mouseDownInitPos = 0;
//Time delay quit
int timeToQuit = -1;
public enum AppState
{
MainMenu,
Game,
HighScores,
Achievements,
Instructions,
Unfocused,
Quiting
}
public AppState appState = AppState.MainMenu; //GameState.Game;
public AppState previousAppState = AppState.MainMenu;
public enum GamePlayState
{
idle,
dropping,
checking,
removingAnim,
removing,
gameOver
}
public GamePlayState gamePlayState = GamePlayState.idle;
public int dropBlockCount = 0;
public numblock savedData;
Menu mainMenu;
Menu gameOverMenu;
public NumBlockGame()
{
savedData = StoredInfo.LoadStoredData();
comboRecord = Convert.ToInt32(savedData.playerinfo.combo);
appState = AppState.MainMenu;
scoreSprites = new List<ScoreSprite>();
achSpriteList = new List<AchievementSpriteComponent>();
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
Content.RootDirectory = "Content/images";
SoundEffect.MasterVolume = 0.3f;
prevKeyState = Keyboard.GetState();
prevMouseState = Mouse.GetState();
blocksToRemove = new List<NumberBlock>();
mainMenu = new Menu(100,new int[] { 310, 355, 400, 445, 502 });
gameOverMenu = new Menu(120, new int[] { 318, 363 });
initials = savedData.playerinfo.initials.ToCharArray();
achievementHub = new AchievementHub(savedData.playerinfo.achievments);
this.settings = new GameSettings();
this.settings.applyDifficulty(GameSettings.Difficulty.Medium);
}
private void ClearGameBoard()
{
if (gameBoard != null)
{
foreach (List<NumberBlock> column in this.gameBoard)
{
foreach (NumberBlock block in column)
{
Components.Remove(block);
}
}
}
gameBoard = new List<List<NumberBlock>>();
for (int i = 0; i < NUM_COLUMNS; i++)
{
gameBoard.Add(new List<NumberBlock>());
}
}
private void generateNewGame()
{
this.comboString = new StringBuilder();
this.saveHighScore = false;
score = 0;
currLevel = 1;
numMovesPerLevel = GetNextNumBlocks();
numMovesLeft = numMovesPerLevel;
ClearGameBoard();
int addElementProbability = this.settings.addElementProbability;
for (int i = 0; i < NUM_COLUMNS; i++)
{
List<NumberBlock> column = gameBoard[i];
for (int j = 0; j < NUM_ROWS; j++)
{
if (rand.Next(100) <= addElementProbability)
{
NumberBlock newBlock = randomBlock(true);
newBlock.column = i;
newBlock.position = new Vector2((i * 48 + BOARD_OFFSET_X + i * 2), (BOARD_OFFSET_Y + j*48 + j*2));
column.Add(newBlock);
Components.Add(newBlock);
}
}
//Drop after adding elements, as the indexes can only be known after all blocks are dropped
foreach (NumberBlock block in column)
{
if (block.Drop())
this.dropBlockCount++;
}
}
CreateNextBlock(true);
//Create premonition blocks
nextBlockQueue = new List<NumberBlock>();
int numBlocksForseen = this.settings.numBlocksForseen;
for (int i = 0; i < numBlocksForseen; i++)
{
nextBlockQueue.Add(randomBlock(false));
}
gamePlayState = GamePlayState.dropping;
}
private void CreateNextBlock(bool cleanQueue)
{
int total = 0;
foreach (List<NumberBlock> col in this.gameBoard)
{
total += col.Count();
}
if (total == NUM_COLUMNS * NUM_ROWS)
{
GameOver();
}
else
{
if (total == 0)
{
this.Score(new Vector2(BOARD_OFFSET_X + 5, BOARD_OFFSET_SLOT_Y + NUM_ROWS*50 - 20), this.currLevel * 20000);
if (achievementHub.hasAchieved(AchievementHub.ACH_CLEAN_SLATE) == false)
{
AwardAchievement(AchievementHub.ACH_CLEAN_SLATE);
}
}
if (cleanQueue == true || nextBlockQueue.Count() == 0)
{
nextBlock = randomBlock(false);
}
else
{
nextBlock = nextBlockQueue.ElementAt(0);
nextBlockQueue.RemoveAt(0);
nextBlockQueue.Add(randomBlock(false));
}
nextBlock.position.X = BOARD_OFFSET_X + nextBlockColumn * 50;
nextBlock.position.Y = BOARD_OFFSET_SLOT_Y;
Components.Add(nextBlock);
}
}
private void GameOver()
{
gamePlayState = GamePlayState.gameOver;
if (isHighScore(this.score))
{
this.saveHighScore = true;
}
}
private NumberBlock randomBlock(bool allowLockedBlocks)
{
NumberBlock returnBlock = new NumberBlock(this);
if(allowLockedBlocks)
returnBlock.blockNumber = rand.Next(0,9);
else
returnBlock.blockNumber = rand.Next(1, 9);
if (returnBlock.blockNumber == 0)
{
returnBlock.lockCount = 2;
}
else if (returnBlock.blockNumber == 9)
{
returnBlock.lockCount = 1;
}
return returnBlock;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
worldRect = new Rectangle(0, 0, 272, 480);
worldToScreenMatrix = Matrix.CreateScale(
(float)GraphicsDevice.Viewport.Width / (float)worldRect.Width,
(float)GraphicsDevice.Viewport.Height / (float)worldRect.Height,
1);
this.IsMouseVisible = true;
base.Initialize();
}
private int GetNextNumBlocks()
{
//Bit too fast => 2*(1.5 + ArcTan[3-0.5x]) + 5
//2*(1.5 + ArcTan[3-0.5(x-1.5)]) + 5
int returnVal = 0;
if (this.settings.difficulty == GameSettings.Difficulty.Easy)
{
returnVal = (int)(3 * (1.5 + Math.Atan(3 - 0.5 * (currLevel - 1.5))) + 8);
}
else if (this.settings.difficulty == GameSettings.Difficulty.Hard)
{
//returnVal = (int)(2 * (1.5 + Math.Atan(3 - 0.5 * currLevel)) + 5);
//(2 * (1.5 + atan(3 - 0.5 * (x- 0.5))) + 3)
returnVal = (int)(2 * (1.5 + Math.Atan(3 - 0.5 * (currLevel - 0.5))) + 3);
}
else
{
returnVal = (int)(2 * (1.5 + Math.Atan(3 - 0.5 * (currLevel - 1.5))) + 5);
}
return returnVal;
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
blockIcons = new List<Texture2D>();
for (int i = 0; i < 11; i++)
{
blockIcons.Add(Content.Load<Texture2D>("block_" + i));
}
alphaTextures = new List<Texture2D>();
for (int i = 0; i < 26; i++)
{
alphaTextures.Add(Content.Load<Texture2D>("alpha" + i));
}
achBG = Content.Load<Texture2D>("ach_bg");
achIcons = new List<Texture2D>();
for (int i = 0; i < 7; i++)
{
achIcons.Add(Content.Load<Texture2D>("achIcon_" + i));
}
achIconNone = Content.Load<Texture2D>("achIcon_none");
background = Content.Load<Texture2D>("background");
splashScreen = Content.Load<Texture2D>("splash");
menuSelectIcon = Content.Load<Texture2D>("menuSelect");
gameOver = Content.Load<Texture2D>("gameOver");
highScoresBg = Content.Load<Texture2D>("scores_bg");
achieveScreenBg = Content.Load<Texture2D>("ach_screen_bg");
instructionsScreenBg = Content.Load<Texture2D>("ins_screen_bg");
charSelectArrows = Content.Load<Texture2D>("arrows");
font = Content.Load<SpriteFont>("ScoreFont");
checkMarkTexture = Content.Load<Texture2D>("checkmark");
pauseOverlay = Content.Load<Texture2D>("paused");
gameOverHighScoreOverlay = Content.Load<Texture2D>("gameOver_highScoreOverlay");
blockLand = Content.Load<SoundEffect>("thunk2");
unlock = Content.Load<SoundEffect>("unlock2");
ping = Content.Load<SoundEffect>("ping2");
achSound = Content.Load<SoundEffect>("achSound");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState(PlayerIndex.One);
MouseState mouseState = Mouse.GetState();
if (appState == AppState.MainMenu)
{
HandleMainMenuAppState(gameTime, keyState, mouseState);
}
else if(appState == AppState.Game)
{
HandlePlayAppState(gameTime, keyState, mouseState);
}
else if (appState == AppState.Unfocused)
{
HandleUnfocused(keyState, mouseState);
}
else if (appState == AppState.HighScores || appState == AppState.Achievements || appState == AppState.Instructions)
{
HandleHighScoresAppState(keyState, mouseState);
}
else if (appState == AppState.Quiting)
{
if (gameTime.TotalGameTime.Seconds > timeToQuit)
this.CloseGame(gameTime);
}
this.prevKeyState = keyState;
this.prevMouseState = mouseState;
base.Update(gameTime);
}
private void HandleUnfocused(KeyboardState keyState, MouseState mouseState)
{
if (unfocused)
return;
if (keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false
|| mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed)
{
appState = previousAppState;
}
}
private void HandleHighScoresAppState(KeyboardState keyState, MouseState mouseState)
{
if ((keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false)
|| (mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed)
|| (keyState.IsKeyDown(Keys.Escape)))
{
this.appState = AppState.MainMenu;
}
}
private void HandlePlayAppState(GameTime gameTime, KeyboardState keyState, MouseState mouseState)
{
// Allows the game to exit
if (keyState.IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
appState = AppState.MainMenu;
return;
}
if (gamePlayState == GamePlayState.idle)
{
HandleIdleState(keyState, mouseState);
}
else if (gamePlayState == GamePlayState.checking)
{
HandleCheckingState();
}
else if (gamePlayState == GamePlayState.removingAnim)
{
if (removingTimeout == 0)
{
removingTimeout = 0;
}
removingTimeout += gameTime.ElapsedGameTime.Milliseconds;
if (removingTimeout > GetRemoveAnimationTime())
{
removingTimeout = 0;
gamePlayState = GamePlayState.removing;
}
}
else if (gamePlayState == GamePlayState.removing)
{
HandleRemoveState();
}
else if (gamePlayState == GamePlayState.dropping)
{
HandleDroppingState(gameTime);
}
else if (gamePlayState == GamePlayState.gameOver)
{
HandleGameOver(keyState, mouseState);
}
}
private void HandleMainMenuAppState(GameTime gameTime, KeyboardState keyState, MouseState mouseState)
{
if (keyState.IsKeyDown(Keys.Escape) && prevKeyState.IsKeyDown(Keys.Escape) == false)
{
this.CloseGame(gameTime);
}
bool menuIndexChanged = mainMenu.HandleMenuInput(mouseState, prevMouseState, keyState, prevKeyState);
if (menuIndexChanged)
ping.Play();
if (keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false
|| (mainMenu.dragged == false && mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed))
{
if (mainMenu.menuOptionIndex == 0)
{
appState = AppState.Game;
generateNewGame();
}
else if (mainMenu.menuOptionIndex == 1)
{
appState = AppState.HighScores;
}
else if (mainMenu.menuOptionIndex == 2)
{
appState = AppState.Achievements;
}
else if (mainMenu.menuOptionIndex == 3)
{
appState = AppState.Instructions;
}
else if (mainMenu.menuOptionIndex == 4)
{
this.CloseGame(gameTime);
}
}
}
private void CloseGame(GameTime gameTime)
{
if (achievementHub.hasAchieved(AchievementHub.ACH_QUIT_GAME) == false)
{
AwardAchievement(AchievementHub.ACH_QUIT_GAME);
timeToQuit = gameTime.TotalGameTime.Seconds + 2;
appState = AppState.Quiting;
if (achievementHub.SaveAchievements(savedData.playerinfo))
{
StoredInfo.SaveData(this.savedData);
}
}
else
{
this.Exit();
}
}
private void HandleGameOver(KeyboardState keyState, MouseState mouseState)
{
if (saveHighScore == false)
{
bool menuIndexChanged = gameOverMenu.HandleMenuInput(mouseState, prevMouseState, keyState, prevKeyState);
if (menuIndexChanged)
ping.Play();
if (keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false
|| (gameOverMenu.dragged == false && mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed))
{
//Save Data
if (achievementHub.SaveAchievements(savedData.playerinfo))
{
StoredInfo.SaveData(this.savedData);
}
//Handle Menu entry
if (gameOverMenu.menuOptionIndex == 0)
{
generateNewGame();
}
else if (gameOverMenu.menuOptionIndex == 1)
{
appState = AppState.MainMenu;
}
}
}
else
{
if (keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false
|| (mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed))
{
achievementHub.SaveAchievements(savedData.playerinfo);
SaveHighScore();
}
if (keyState.IsKeyDown(Keys.Left) && prevKeyState.IsKeyDown(Keys.Left) == false)
{
//|| mouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released)
if (this.currInitial > 0)
{
this.currInitial--;
}
}
if (keyState.IsKeyDown(Keys.Right) && prevKeyState.IsKeyDown(Keys.Right) == false)
{
//|| mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released)
if (this.currInitial < 2)
{
this.currInitial++;
}
}
if (keyState.IsKeyDown(Keys.Up) && prevKeyState.IsKeyDown(Keys.Up) == false
|| mouseState.ScrollWheelValue > prevMouseState.ScrollWheelValue)
{
HandleUpInitial();
}
else if (keyState.IsKeyDown(Keys.Down) && prevKeyState.IsKeyDown(Keys.Down) == false
|| mouseState.ScrollWheelValue < prevMouseState.ScrollWheelValue)
{
HandleDownInitial();
}
}
}
private void SaveHighScore()
{
score newHighScore = new score();
newHighScore.level = this.currLevel.ToString();
newHighScore.value = this.score.ToString();
newHighScore.initials = new String(this.initials);
savedData.playerinfo.initials = newHighScore.initials;
score[] localScores = this.savedData.highscores.local;
score[] newLocalScores = null;
int length = localScores.Count();
if (length < MAX_LOCAL_HIGH_SCORES)
newLocalScores = new score[length + 1];
else
newLocalScores = new score[MAX_LOCAL_HIGH_SCORES];
bool indexfound = false;
for (int i = 0; i < length; i++)
{
if (indexfound == false)
{
if (Convert.ToInt64(localScores[i].value) < this.score)
{
indexfound = true;
newLocalScores[i] = newHighScore;
}
else
{
newLocalScores[i] = localScores[i];
}
}
else if(i < MAX_LOCAL_HIGH_SCORES-1)
{
newLocalScores[i] = localScores[i-1];
}
}
//Special condition, new score is lowest on list
if (indexfound == false)
newLocalScores[length] = newHighScore;
//Special condition, new score is highest on list, yet list isn't full
if (newLocalScores[newLocalScores.Count()-1] == null)
newLocalScores[newLocalScores.Count()-1] = localScores[length - 1];
this.savedData.highscores.local = newLocalScores;
StoredInfo.SaveData(this.savedData);
appState = AppState.HighScores;
}
private void HandleRemoveState()
{
foreach (NumberBlock block in blocksToRemove)
{
List<NumberBlock> col = this.gameBoard[block.column];
/*
* Check the locks on the block above
*/
int index = col.IndexOf(block);
if (index > 0)
{
col[index - 1].checkLock();
}
/*
* Check the locks on the block bellow
*/
if (index < col.Count()-1)
{
col[index + 1].checkLock();
}
/*
* Check the locks on the block to the left
*/
int heightCheck = col.Count() - index;
if (block.column > 0)
{
List<NumberBlock> leftColumn = this.gameBoard[block.column - 1];
if (leftColumn.Count() >= heightCheck)
{
leftColumn[leftColumn.Count()-heightCheck].checkLock();
}
}
/*
* Check the locks on the block to the right
*/
if (block.column < NUM_COLUMNS-1)
{
List<NumberBlock> rightColumn = this.gameBoard[block.column + 1];
if (rightColumn.Count() >= heightCheck)
{
rightColumn[rightColumn.Count() - heightCheck].checkLock();
}
}
col.RemoveAt(index);
for (int i = 0; i < col.Count(); i++)
{
NumberBlock curBlock = col[i];
if (curBlock.beingRemoved == false && col[i].Drop())
this.dropBlockCount++;
}
}
multiplier++;
gamePlayState = GamePlayState.dropping;
}
private void HandleDroppingState(GameTime gameTime)
{
foreach (NumberBlock rblock in blocksToRemove)
{
Components.Remove(rblock);
}
blocksToRemove = new List<NumberBlock>();
/*
* Catch conditions where blocks drop at the top of a stack
* or manage to lock in dropping state
*/
if (this.dropBlockCount <= 0)
{
this.dropBlockCount = 0;
}
else
{
if (droppingTimeout == 0)
{
droppingTimeout = 0;
}
droppingTimeout += gameTime.ElapsedGameTime.Milliseconds;
if (droppingTimeout > 750)
this.dropBlockCount = 0;
}
if (this.dropBlockCount == 0)
gamePlayState = GamePlayState.checking;
}
private void HandleCheckingState()
{
droppingTimeout = 0;
dropBlockCount = 0;
int[] maxMatchIndex = new int[NUM_COLUMNS];
//check for matches
int i = 0;
foreach (List<NumberBlock> column in this.gameBoard)
{
maxMatchIndex[i] = -1;
int j = 0;
foreach (NumberBlock block in column)
{
bool matchFound = false;
if (block.lockCount > 0)
{
//do nothing
} else if (block.blockNumber == column.Count())
{
matchFound = true;
} else {
//TODO count contiguous row count
int heightCheck = column.Count() - j;
int leftCount = 0;
for (int l = i - 1; l >= 0; l--)
{
if (this.gameBoard[l].Count() >= heightCheck)
leftCount++;
else
break;
}
int rightCount = 0;
for (int l = i + 1; l < NUM_COLUMNS; l++)
{
if (this.gameBoard[l].Count() >= heightCheck)
rightCount++;
else
break;
}
if (leftCount + rightCount + 1 == block.blockNumber)
{
matchFound = true;
}
}
if(matchFound) {
maxMatchIndex[i] = j;
this.blocksToRemove.Add(block);
block.beingRemoved = true;
this.Score(block.position);
this.comboString.Append(block.blockNumber);
}
j++;
}
i++;
}
if (maxMatchIndex.Sum() > -1 * NUM_COLUMNS)
{
gamePlayState = GamePlayState.removingAnim;
}
else
{
this.comboString.Append("|");
if (numMovesLeft == 0)
{
currLevel++;
numMovesPerLevel = GetNextNumBlocks();
numMovesLeft = numMovesPerLevel;
PushNewRow();
}
else
{
CheckComboString();
gamePlayState = GamePlayState.idle;
}
}
}
private void CheckComboString()
{
/*
* Evaluate combo string for bonus matches and achievements
*/
String combo = this.comboString.ToString();
if (achievementHub.hasAchieved(AchievementHub.ACH_3_to_1) == false)
{
Regex regX_321 = new Regex("3.*2.*1");
if (regX_321.IsMatch(combo))
{
AwardAchievement(AchievementHub.ACH_3_to_1);
}
}
if (achievementHub.hasAchieved(AchievementHub.ACH_FINAL_COUNTDOWN) == false)
{
Regex regX_8to1 = new Regex("8.*7.*6.*5.*4.*3.*2.*1");
if (regX_8to1.IsMatch(combo))
{
AwardAchievement(AchievementHub.ACH_FINAL_COUNTDOWN);
}
}
//Clear the Combo String
comboString = new StringBuilder();
}
private void Score(Vector2 pos)
{
this.Score(pos, -1);
}
private void Score(Vector2 pos, int scoreVal)
{
//(float)((int)multiplier - 6.0f) / 6;
float pitch = (float)((int)multiplier - 3.0f) / 6; // / 5;
if (pitch > 1.0f)
pitch = 1.0f;
int scoreMod = 0;
if (scoreVal > 0)
{
scoreMod = scoreVal;
}
else
{
scoreMod = 10 * multiplier * multiplier + 30 * multiplier + 50;
//only add points from removed blocks to combo value
comboScore += scoreMod;
}
score += scoreMod;
ScoreSprite sprite = new ScoreSprite(this);
sprite.StartScoreSprite(scoreMod, (int)(pos.X + 5), (int)(pos.Y+3));
Components.Add(sprite);
ping.Play(1.0f, pitch, 0.0f);
}
private void HandleIdleState(KeyboardState keyState, MouseState mouseState)
{
/*
* Reset combo fields
*/
multiplier = 1;
if (comboScore > comboRecord)
{
AwardAchievement(AchievementHub.ACH_COMBO_BREAKER);
this.comboRecord = comboScore;
this.savedData.playerinfo.combo = comboScore.ToString();
}
if (this.achievementHub.hasAchieved(AchievementHub.ACH_OVER_9000) == false && comboScore > 9000)
{
AwardAchievement(AchievementHub.ACH_OVER_9000);
}
comboScore = 0;
if (nextBlock == null)
{
CreateNextBlock(false);
}
/*
* Handle Idle State
*/
if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyDown(Keys.Space) == false)
{
DropNextBlock();
}
if (keyState.IsKeyDown(Keys.Left) && prevKeyState.IsKeyDown(Keys.Left) == false && nextBlockColumn > 0)
{
if (nextBlock != null)
{
nextBlockColumn -= 1;
nextBlock.MoveLeft();
}
}
if (keyState.IsKeyDown(Keys.Right) && prevKeyState.IsKeyDown(Keys.Right) == false && nextBlockColumn < 7)
{
if (nextBlock != null)
{
nextBlockColumn += 1;
nextBlock.MoveRight();
}
}
if (keyState.IsKeyDown(Keys.Enter) && prevKeyState.IsKeyDown(Keys.Enter) == false)
{
/*float step = 0.05f;
float pitch = tempIndex++*step;
if (pitch > 1.0)
{
pitch = -0.0f;
tempIndex = 0;
}
ping.Play(1.0f, pitch, 0.0f);*/
//multiplier++;
//this.Score(new Vector2());
//GameOver();
//AwardAchievement(AchievementHub.ACH_SURVIVOR_MAN);
}
if (mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed)
{
DropNextBlock();
}
else if (mouseState.LeftButton == ButtonState.Pressed)
{
//Null hanlding for when M1 + spacebar are both held down
if (nextBlock != null)
{
int mouseOverColumn = (int)((mouseState.X - BOARD_OFFSET_X) / 50);
if (mouseOverColumn < 0)
mouseOverColumn = 0;
else if (mouseOverColumn > NUM_COLUMNS - 1)