-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
1037 lines (810 loc) · 38 KB
/
Game1.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 Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using System.Collections;
using System;
namespace _3DAsteroids
{
/// <summary>
/// This is the main type for your game.
/// </summary>
///
static class Constants
{
public const int OUTERLIMIT = 50; //the outer +- x, y, and z of the universe (as a cube)
public const float SHOOTTIMER = 0.5F; //2 bullets per second
public const float MAXSPEED = 0.5F; //The max speed the ship can reach with thrusters
public const int SMALLSIZE = 100; //small universe size
public const int MEDIUMSIZE = 200; //medium universe size
public const int LARGESIZE = 300; //large universe size
public const int SMALLASTEROIDS = 20; //number of asteroids in a small universe
public const int MEDIUMASTEROIDS = 30; //number of asteroids in a medium universe
public const int LARGEASTEROIDS = 40; //number of asteroids in a large universe
public const int EASYMODE = 100; //all asteroids will break when colliding
public const int MEDMODE = 50;
public const int HARDMODE = 0; //all asteroids never break on collision
public enum PowerUpType
{
Fuel,
MultiShot,
Speed
}
}
public class Game1 : Game
{
private enum GameStates
{
StartMenu,
Controls,
PrePlaying,
Playing,
Dead,
End
}
public enum GameDifficulties
{
Easy,
Medium,
Hard
}
public enum GameSize
{
Small,
Medium,
Large
}
private GameStates currentState;
public GameDifficulties currentDifficulty;
public GameSize currentSize;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont myFont;
private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
private Matrix view = Matrix.CreateLookAt(new Vector3(0, 100, 100), new Vector3(0, 0, 0), Vector3.UnitY);
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 2000f);
private Model skyboxModel;
private Texture2D menuTexture;
//private Texture2D blackScreen;
private Texture2D titleText;
private Texture2D controlText;
private Random rand;
private float gameTimer;
private bool goodToGo;
private Model[] asteroidModels;
private ArrayList allAsteroids;
private Model fuelModel;
private Model speedModel;
private Model multiModel;
private Powerup[] allItems;
private int currentItem;
private float itemTimer;
public int asteroidsStart;
public int asteroidsLeft;
private Player player;
private Model bullet;
private Texture2D[] buttonTextures;
private Explosion[] allExplosions;
private Texture2D explosion;
private int currentExplosion;
private int lives;
private SoundEffect missileShot;
private SoundEffect explosionSound;
private SoundEffect powerupSound;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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()
{
// TODO: Add your initialization logic here
allAsteroids = new ArrayList();
allItems = new Powerup[20];
rand = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
player = new Player();
currentState = GameStates.StartMenu;
buttonTextures = new Texture2D[11];
asteroidModels = new Model[7];
currentDifficulty = GameDifficulties.Medium;
currentSize = GameSize.Medium;
allExplosions = new Explosion[15]; //should be enough
for (int i = 0; i < allExplosions.Length; i++)
{
allExplosions[i] = new Explosion();
}
lives = 3;
gameTimer = 0;
goodToGo = false;
base.Initialize();
}
/// <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);
//player.model = Content.Load<Model>("karenspaceship");
player.model = Content.Load<Model>("droidShip");
//http://thebrb.com/stockpile/files/starfieldimage/starfield.png
skyboxModel = Content.Load<Model>("skyboxStarField");
menuTexture = Content.Load<Texture2D>("menuImage");
//blackScreen = Content.Load<Texture2D>("blackScreen");
asteroidModels[0] = Content.Load<Model>("Asteroids/asteroid1");
asteroidModels[1] = Content.Load<Model>("Asteroids/asteroid2");
asteroidModels[2] = Content.Load<Model>("Asteroids/asteroid3");
asteroidModels[3] = Content.Load<Model>("Asteroids/asteroid4");
asteroidModels[4] = Content.Load<Model>("Asteroids/asteroid5");
asteroidModels[5] = Content.Load<Model>("Asteroids/asteroid6");
//asteroidModel = Content.Load<Model>("Asteroids/asteroid1");
bullet = Content.Load<Model>("bullet");
myFont = Content.Load<SpriteFont>("myFont");
buttonTextures[0] = Content.Load<Texture2D>("Buttons/Easy1");
buttonTextures[1] = Content.Load<Texture2D>("Buttons/Easy2");
buttonTextures[2] = Content.Load<Texture2D>("Buttons/Medium1");
buttonTextures[3] = Content.Load<Texture2D>("Buttons/Medium2");
buttonTextures[4] = Content.Load<Texture2D>("Buttons/Hard1");
buttonTextures[5] = Content.Load<Texture2D>("Buttons/Hard2");
buttonTextures[6] = Content.Load<Texture2D>("Buttons/Small1");
buttonTextures[7] = Content.Load<Texture2D>("Buttons/Small2");
buttonTextures[8] = Content.Load<Texture2D>("Buttons/Large1");
buttonTextures[9] = Content.Load<Texture2D>("Buttons/Large2");
explosion = Content.Load<Texture2D>("explosion");
missileShot = Content.Load<SoundEffect>("missileFire");
explosionSound = Content.Load<SoundEffect>("ExplosionSound");
powerupSound = Content.Load<SoundEffect>("powerupGet");
player.setSound(missileShot);
fuelModel = Content.Load<Model>("fuelItem");
speedModel = Content.Load<Model>("speedItem");
multiModel = Content.Load<Model>("multiItem");
titleText = Content.Load<Texture2D>("titleText");
controlText = Content.Load<Texture2D>("ControlsText");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; //the change in time since last update;
if (currentState == GameStates.StartMenu)
{
//check for option changes
//Mouse.GetState().
this.IsMouseVisible = true;
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
Point mousePos = Mouse.GetState().Position;
if (mousePos.Y >= 200 && mousePos.Y <= 280)
{
if (mousePos.X >= 120 && mousePos.X <= 280)
{
currentDifficulty = GameDifficulties.Easy;
}
if (mousePos.X >= 320 && mousePos.X <= 480)
{
currentDifficulty = GameDifficulties.Medium;
}
if (mousePos.X >= 520 && mousePos.X <= 680)
{
currentDifficulty = GameDifficulties.Hard;
}
}
if (mousePos.Y >= 330 && mousePos.Y <= 410)
{
if (mousePos.X >= 120 && mousePos.X <= 280)
{
currentSize = GameSize.Small;
}
if (mousePos.X >= 320 && mousePos.X <= 480)
{
currentSize = GameSize.Medium;
}
if (mousePos.X >= 520 && mousePos.X <= 680)
{
currentSize = GameSize.Large;
}
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
currentState = GameStates.Controls;
}
}
if (currentState == GameStates.Controls)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter) && goodToGo)
{
currentState = GameStates.PrePlaying;
}
if (Keyboard.GetState().IsKeyUp(Keys.Enter))
{
goodToGo = true;
}
}
if (currentState == GameStates.PrePlaying)
{
//create our starting asteroids
int numAsteroids = 0;
int outerLimit = 0;
if (currentSize == GameSize.Small)
{
numAsteroids = Constants.SMALLASTEROIDS;
outerLimit = Constants.SMALLSIZE;
}
if (currentSize == GameSize.Medium)
{
numAsteroids = Constants.MEDIUMASTEROIDS;
outerLimit = Constants.MEDIUMSIZE;
}
if (currentSize == GameSize.Large)
{
numAsteroids = Constants.LARGEASTEROIDS;
outerLimit = Constants.LARGESIZE;
}
asteroidsStart = numAsteroids;
int difficulty = 0;
if (currentDifficulty == GameDifficulties.Easy)
{
difficulty = Constants.EASYMODE;
}
if (currentDifficulty == GameDifficulties.Medium)
{
difficulty = Constants.MEDMODE;
}
if (currentDifficulty == GameDifficulties.Hard)
{
difficulty = Constants.HARDMODE;
}
for (int i = 0; i < numAsteroids; i++)
{
allAsteroids.Add(new Asteroid(rand, outerLimit, difficulty));
asteroidsLeft++;
}
int j = 0;
foreach (Asteroid a in allAsteroids)
{
if (j % 2 == 0)
{
a.bigModel = asteroidModels[0];
a.medModel = asteroidModels[1];
a.smallModel = asteroidModels[2];
}
else
{
a.bigModel = asteroidModels[3];
a.medModel = asteroidModels[4];
a.smallModel = asteroidModels[5];
}
j++;
}
player.setBounds(outerLimit);
for (int i = 0; i < 20; i++)
{
Powerup p = new Powerup(rand, outerLimit);
p.setModel(fuelModel, multiModel, speedModel);
allItems[i] = (p);
}
this.IsMouseVisible = false;
currentState = GameStates.Playing;
}
if (currentState == GameStates.Playing)
{
player.playerFlight(); //keyboard flight
updateCamera(); //update camera position
gameTimer += delta;
itemTimer += delta;
//spawn an item every 15 seconds
if (itemTimer > 15)
{
allItems[currentItem].spawnPowerup();
currentItem++;
currentItem %= (allItems.Length);
itemTimer = 0;
}
//have all asteroid float in space
foreach (Asteroid a in allAsteroids)
{
if (a.isActive)
{
a.updatePosition(delta);
}
}
//update player stuff
player.updateTimer(delta);
//update explosions
foreach (Explosion e in allExplosions)
{
if (e.isExploding)
{
e.updateTimer(delta);
}
}
//bullets also fly through space
foreach (Bullet b in player.bulletSupply)
{
b.updateBullet();
if (b.outOfBounds() && b.isActive)
{
//EXPLODE!
createExplosion(b.position);
b.isActive = false;
explosionSound.Play();
}
}
foreach (Powerup p in allItems)
{
if (p.isActive)
{
p.updatePosition(delta);
}
}
//see if stuff is colliding with other stuff
checkCollisions();
if (asteroidsStart * .25F >= asteroidsLeft)
{
//You Win!
currentState = GameStates.End;
}
}
if (currentState == GameStates.Dead)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
player.position = Vector3.Zero;
currentState = GameStates.Playing;
}
if (lives == 0)
{
currentState = GameStates.End;
}
}
if (currentState == GameStates.End)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
Exit();
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DeepSkyBlue);
Vector2 center = new Vector2(GraphicsDevice.Viewport.Bounds.Width / 2, GraphicsDevice.Viewport.Bounds.Height / 2);
Rectangle screen = new Rectangle(0, 0, GraphicsDevice.Viewport.Bounds.Width, GraphicsDevice.Viewport.Bounds.Height);
if (currentState == GameStates.StartMenu)
{
spriteBatch.Begin();
spriteBatch.Draw(menuTexture, screen, Color.White);
spriteBatch.Draw(titleText, new Vector2(center.X - 160, 20), Color.White);
string pressEnter = "Press Enter to Start";
Vector2 stringLen = myFont.MeasureString(pressEnter);
spriteBatch.DrawString(myFont, pressEnter, center - stringLen / 2, Color.White);
string selectDifficulty = "Select Your Difficulty";
stringLen = myFont.MeasureString(selectDifficulty);
spriteBatch.DrawString(myFont, selectDifficulty, new Vector2(center.X - stringLen.X / 2, 170), Color.White);
if (currentDifficulty == GameDifficulties.Easy)
{
spriteBatch.Draw(buttonTextures[0], new Vector2(120, 200), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[1], new Vector2(120, 200), Color.White);
}
if (currentDifficulty == GameDifficulties.Medium)
{
spriteBatch.Draw(buttonTextures[2], new Vector2(320, 200), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[3], new Vector2(320, 200), Color.White);
}
if (currentDifficulty == GameDifficulties.Hard)
{
spriteBatch.Draw(buttonTextures[4], new Vector2(520, 200), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[5], new Vector2(520, 200), Color.White);
}
string selectSize = "Select Universe Size";
stringLen = myFont.MeasureString(selectSize);
spriteBatch.DrawString(myFont, selectSize, new Vector2(center.X - stringLen.X / 2, 300), Color.White);
if (currentSize == GameSize.Small)
{
spriteBatch.Draw(buttonTextures[6], new Vector2(120, 330), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[7], new Vector2(120, 330), Color.White);
}
if (currentSize == GameSize.Medium)
{
spriteBatch.Draw(buttonTextures[2], new Vector2(320, 330), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[3], new Vector2(320, 330), Color.White);
}
if (currentSize == GameSize.Large)
{
spriteBatch.Draw(buttonTextures[8], new Vector2(520, 330), Color.White);
}
else
{
spriteBatch.Draw(buttonTextures[9], new Vector2(520, 330), Color.White);
}
string beginSize = "Select with Mouse, Enter to Continue!";
stringLen = myFont.MeasureString(beginSize);
spriteBatch.DrawString(myFont, beginSize, new Vector2(center.X - stringLen.X / 2, 430), Color.White);
spriteBatch.End();
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
if (currentState == GameStates.Controls)
{
spriteBatch.Begin();
spriteBatch.Draw(menuTexture, screen, Color.White);
spriteBatch.Draw(controlText, screen, Color.White);
spriteBatch.End();
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
if (currentState == GameStates.Playing)
{
//world = Matrix.CreateRotationY(MathHelper.Pi) *
// Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z) * Matrix.CreateTranslation(position);
world = Matrix.CreateRotationX(-MathHelper.Pi) * Matrix.CreateFromQuaternion(player.rotation) * Matrix.CreateTranslation(player.position);
//draw the player
DrawModel(player.model, world, view, projection);
//draw all our asteroids a bit bigger than normal
foreach (Asteroid a in allAsteroids)
{
//Matrix.CreateFromYawPitchRoll(a.rotation.X, a.rotation.Y, a.rotation.Z) rotate the stuff. DONT FORGET TO DO THIS IN COLLISION CHECK TOO!
if (a.isActive)
{
DrawModel(a.getModel(), a.getWorldMatrix(), view, projection);
}
}
//Draw the cylinder longer because it's a stupid bullet not a cylinder
//DrawModel(bullet, Matrix.CreateScale(1, 1, 5) * Matrix.CreateTranslation(new Vector3(1, 1, 1)), view, projection);
DrawSkybox(skyboxModel, Matrix.CreateScale(400, 400, 400), view, projection);
//draw all the bullets
foreach (Bullet b in player.bulletSupply)
{
if (b.isActive)
{
DrawModel(bullet, b.getWorldMatrix(), view, projection);
}
}
foreach (Powerup p in allItems)
{
if (p.isActive)
{
DrawModel(p.getModel(), p.getWorldMatrix(), view, projection);
}
}
int gameMins = (int)TimeSpan.FromSeconds(gameTimer).TotalMinutes;
int gameSecs = (int)TimeSpan.FromSeconds(gameTimer).TotalSeconds % 60;
int gameMilli = (int)TimeSpan.FromSeconds(gameTimer).TotalMilliseconds;
//String gametimeString = "" + gameMins.ToString("D2") + ":" + gameSecs.ToString("D2") + ":" + gameMilli.ToString("D3");
String gametimeString = "Time Elapsed: " + gameMins.ToString("D2") + ":" + gameSecs.ToString("D2");
spriteBatch.Begin();
//spriteBatch.DrawString(myFont, "Asteroids Left: " + asteroidsLeft, new Vector2(20, 20), Color.White);
spriteBatch.DrawString(myFont, "Lives Left: " + lives, new Vector2(100, GraphicsDevice.Viewport.Height - 20), Color.White);
//spriteBatch.DrawString(myFont, "Player Pos: " + player.position.ToString(), new Vector2(20, 60), Color.Black);
//spriteBatch.DrawString(myFont, "" + asteroidsStart *.25, new Vector2(20, 60), Color.Black);
spriteBatch.DrawString(myFont, gametimeString, new Vector2(GraphicsDevice.Viewport.Width - 250, GraphicsDevice.Viewport.Height - 20), Color.White);
spriteBatch.DrawString(myFont, "Fuel Remaining: " + Math.Ceiling(player.fuel), new Vector2(100, 20), Color.White);
String upgradesString = "Upgrades Active: ";
if (player.doubleSpeed) upgradesString += "Double Speed ";
if (player.multiBullet) upgradesString += "Multi Bullets ";
spriteBatch.DrawString(myFont, upgradesString, new Vector2(GraphicsDevice.Viewport.Width - 250, 20), Color.White);
spriteBatch.End();
//draw explosions if any
foreach (Explosion e in allExplosions)
{
if (e.isExploding == true)
{
//Pretty sure this came from https://blogs.msdn.microsoft.com/shawnhar/2011/01/12/spritebatch-billboards-in-a-3d-world/
BasicEffect basicEffect = new BasicEffect(GraphicsDevice);
Vector3 camPos = new Vector3(0, 0, 0.5F);
camPos = Vector3.Transform(camPos, Matrix.CreateFromQuaternion(player.rotation));
camPos += player.position;
basicEffect.World = Matrix.CreateConstrainedBillboard(e.position, camPos, Vector3.Down, null, null);
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.TextureEnabled = true;
spriteBatch.Begin(0, null, null, null, null, basicEffect);
spriteBatch.Draw(explosion, new Rectangle(((int)e.position.X - explosion.Width / 2) / 40, ((int)e.position.Y - explosion.Height / 2) / 40, 5, 5), Color.White);
spriteBatch.End();
}
}
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
if (currentState == GameStates.Dead)
{
spriteBatch.Begin();
string livesSize = "Lives Remaining: " + lives;
Vector2 stringLen = myFont.MeasureString(livesSize);
spriteBatch.Draw(menuTexture, screen, Color.White);
spriteBatch.DrawString(myFont, livesSize, new Vector2(center.X - stringLen.X / 2, center.Y - stringLen.Y / 2), Color.White);
string EnterSize = "Press Enter to Respawn";
stringLen = myFont.MeasureString(EnterSize);
spriteBatch.DrawString(myFont, EnterSize, new Vector2(center.X - stringLen.X / 2, 100 + center.Y - stringLen.Y / 2), Color.White);
spriteBatch.End();
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
if (currentState == GameStates.End)
{
if (lives == 0)
{
//You lose!
spriteBatch.Begin();
spriteBatch.Draw(menuTexture, screen, Color.White);
string lose = "You lose!";
Vector2 stringLen = myFont.MeasureString(lose);
spriteBatch.DrawString(myFont, lose, new Vector2(center.X - stringLen.X / 2, center.Y - stringLen.Y / 2), Color.White);
string enter = "Enter to Quit";
stringLen = myFont.MeasureString(enter);
spriteBatch.DrawString(myFont, enter, new Vector2(center.X - stringLen.X / 2, 200 + center.Y - stringLen.Y / 2), Color.White);
spriteBatch.End();
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
else
{
//You Win!
int gameMins = (int)TimeSpan.FromSeconds(gameTimer).TotalMinutes;
int gameSecs = (int)TimeSpan.FromSeconds(gameTimer).TotalSeconds % 60;
int gameMilli = (int)TimeSpan.FromSeconds(gameTimer).TotalMilliseconds;
//String gametimeString = "" + gameMins.ToString("D2") + ":" + gameSecs.ToString("D2") + ":" + gameMilli.ToString("D3");
String gametimeString = "Time Elapsed: " + gameMins.ToString("D2") + ":" + gameSecs.ToString("D2");
spriteBatch.Begin();
spriteBatch.Draw(menuTexture, screen, Color.White);
string win = "You win!";
Vector2 stringLen = myFont.MeasureString(win);
spriteBatch.DrawString(myFont, win, new Vector2(center.X - stringLen.X / 2, center.Y - stringLen.Y / 2), Color.White);
stringLen = myFont.MeasureString(gametimeString);
spriteBatch.DrawString(myFont, gametimeString, new Vector2(center.X - stringLen.X / 2, 100 + center.Y - stringLen.Y / 2), Color.White);
string enter = "Enter to Quit";
stringLen = myFont.MeasureString(enter);
spriteBatch.DrawString(myFont, enter, new Vector2(center.X - stringLen.X / 2, 200 + center.Y - stringLen.Y / 2), Color.White);
spriteBatch.End();
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
}
}
// TODO: Add your drawing code here
// spriteBatch.Begin();
// spriteBatch.Draw(skyboxBack, new Rectangle(0, 0, 400, 240), Color.White);
// spriteBatch.End();
base.Draw(gameTime);
}
/// <summary>
///
/// Draws a given model at a given location from a given view/projection. Took it from
/// http://rbwhitaker.wikidot.com/basic-effect-lighting
///
/// </summary>
/// <param name="model"></param>
/// <param name="world"></param>
/// <param name="view"></param>
/// <param name="projection"></param>
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = world;
effect.View = view;
effect.Projection = projection;
effect.LightingEnabled = true;
effect.DirectionalLight0.DiffuseColor = new Vector3(1f, 0.2f, 0.2f); // a reddish light
effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis
effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights
effect.AmbientLightColor = new Vector3(0.5f, 0.5f, 0.5f); // Add some overall ambient light.
}
mesh.Draw();
}
}
/// <summary>
///
/// Pretty much the exact same as above but no lighting (for skybox)
///
/// </summary>
/// <param name="model"></param>
/// <param name="world"></param>
/// <param name="view"></param>
/// <param name="projection"></param>
private void DrawSkybox(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = world;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
/// <summary>
/// Taken from https://msdn.microsoft.com/en-us/library/bb203906.aspx (BUT THEN I HAD TO MODIFY IT!)
/// Will check for collision between two objects, then bounce them back if they are colliding
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
private void CheckForCollision(Asteroid c1, Asteroid c2)
{
// Check whether the bounding boxes of the two objects intersect.
//there is only 1 mesh for my asteroids so this is easier to check
BoundingSphere c1BoundingSphere = c1.getModel().Meshes[0].BoundingSphere.Transform(c1.getWorldMatrix());
BoundingSphere c2BoundingSphere = c2.getModel().Meshes[0].BoundingSphere.Transform(c2.getWorldMatrix());
if (c1BoundingSphere.Intersects(c2BoundingSphere))
{
c1.CollisionHit();
c2.CollisionHit();
//System.Diagnostics.Debug.Write("Ouch!\n");
return;
}
}
private void updateCamera()
{
Vector3 camPos = new Vector3(0, 0, 0.5F);
camPos = Vector3.Transform(camPos, Matrix.CreateFromQuaternion(player.rotation));
camPos += player.position;
Vector3 cameraUp = new Vector3(0, 1, 0);
cameraUp = Vector3.Transform(cameraUp, Matrix.CreateFromQuaternion(player.rotation));
view = Matrix.CreateLookAt(camPos, player.position, cameraUp);
}
private void createExplosion(Vector3 location)
{
allExplosions[currentExplosion].CreateExplosion(location);
currentExplosion++;
currentExplosion %= (allExplosions.Length);
}
private void checkCollisions()
{
//checks to see if the player is touching an aseroid
BoundingSphere playerBoundingSphere = player.model.Meshes[0].BoundingSphere.Transform(Matrix.CreateTranslation(player.position));
foreach (Asteroid a in allAsteroids)
{
if (a.isActive)
{
BoundingSphere aBoundSphere = a.getModel().Meshes[0].BoundingSphere.Transform(a.getWorldMatrix());
if (playerBoundingSphere.Intersects(aBoundSphere))
{
//System.Diagnostics.Debug.Write("MAN DOWN\n");
//DIE!
currentState = GameStates.Dead;
lives--;
}
}
}
//Is the player hitting an item?
foreach (Powerup p in allItems)
{
if (p.isActive)
{
BoundingSphere aBoundSphere = p.getModel().Meshes[0].BoundingSphere.Transform(p.getWorldMatrix());
if (playerBoundingSphere.Intersects(aBoundSphere))
{
//System.Diagnostics.Debug.Write("Got a powerup!\n");
//Find Item type
powerupSound.Play();
Constants.PowerUpType thisType = p.PickUp();
if (thisType == Constants.PowerUpType.Fuel)
{
player.addFuel(25);
}
if (thisType == Constants.PowerUpType.MultiShot)
{
player.multiBullet = true;
}
if (thisType == Constants.PowerUpType.Speed)
{
player.doubleSpeed = true;
}
}
}
}
//checks to see if an asteroid is colliding with another asteroid
foreach (Asteroid a in allAsteroids)
{
foreach (Asteroid b in allAsteroids)
{
if (!a.Equals(b) && a.isActive && b.isActive)
{
CheckForCollision(a, b);
}
}
}
//checks if a bullet is hitting an asteroid
foreach (Bullet b in player.bulletSupply)
{
if (b.isActive)
{
BoundingBox bulletBox = UpdateBoundingBox(bullet, b.getWorldMatrix());
foreach (Asteroid a in allAsteroids)
{
if (a.isActive)
{
BoundingSphere aBoundSphere = a.getModel().Meshes[0].BoundingSphere.Transform(a.getWorldMatrix());
if (bulletBox.Intersects(aBoundSphere))
{
//System.Diagnostics.Debug.Write("Asteroid Shot Down!\n");
//Destroy asteroid!!
if (a.hit()) asteroidsLeft--; //destroy the asteroid
b.isActive = false; //destory the bullet
//asteroidsLeft--;
//TODO MAKE AN EXPLOSION HERE!
explosionSound.Play();
createExplosion(a.position);
}
}
}
}
}
}
/// <summary>
/// Taken from http://gamedev.stackexchange.com/questions/2438/how-do-i-create-bounding-boxes-with-xna-4-0
/// by user tunnez.
/// </summary>
/// <param name="model">The model that is being used as a base for the boundingBox</param>
/// <param name="worldTransform">The location of the object in world space</param>
/// <returns></returns>
protected BoundingBox UpdateBoundingBox(Model model, Matrix worldTransform)
{