SpacewarScreen.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpacewarScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. #endregion
  15. namespace Spacewar
  16. {
  17. public class SpacewarScreen : Screen
  18. {
  19. protected Projectiles bullets;
  20. protected Ship ship1;
  21. protected Ship ship2;
  22. protected SceneItem sun;
  23. protected bool paused = true;
  24. protected int player1Score;
  25. protected int player2Score;
  26. protected Particles particles;
  27. protected SceneItem backdrop;
  28. public SpacewarScreen(Game game)
  29. : base(game)
  30. {
  31. }
  32. /// <summary>
  33. /// Checks for collisions between objects
  34. /// </summary>
  35. protected virtual void handleCollisions(TimeSpan gameTime)
  36. {
  37. if (!paused)
  38. {
  39. //Sun always does 5 damage i.e kills you
  40. if (sun.Collide(ship1) && !ship1.Uncollidable)
  41. HitPlayer1(gameTime, 5);
  42. if (sun.Collide(ship2) && !ship2.Uncollidable)
  43. HitPlayer2(gameTime, 5);
  44. //Ship collisions do 2 damage and bounce you
  45. if (ship1.Collide(ship2) && !ship1.Uncollidable && !ship2.Uncollidable)
  46. {
  47. if (SpacewarGame.GameState == GameState.PlayEvolved)
  48. {
  49. HitPlayer1(gameTime, 2);
  50. HitPlayer2(gameTime, 2);
  51. float ship1Speed = ship1.Velocity.Length();
  52. float ship2Speed = ship2.Velocity.Length();
  53. Vector3 tmp = ship1.Velocity;
  54. Vector3 vel = ship2.Velocity;
  55. if (vel.LengthSquared() > 0.0f)
  56. vel.Normalize();
  57. ship1.Velocity = vel * ship1Speed;
  58. if (tmp.LengthSquared() > 0.0f)
  59. tmp.Normalize();
  60. ship2.Velocity = tmp * ship2Speed;
  61. }
  62. else
  63. {
  64. //Retro is instant kill
  65. HitPlayer1(gameTime, 5);
  66. HitPlayer2(gameTime, 5);
  67. }
  68. }
  69. foreach (Projectile bullet in bullets)
  70. {
  71. if (bullet.Collide(sun))
  72. bullet.DeleteProjectile();
  73. //Bullets to differing amounts of damage depending on the bullet type
  74. if (bullet.Collide(ship1) && !ship1.Uncollidable)
  75. {
  76. bullet.DeleteProjectile();
  77. HitPlayer1(gameTime, bullet.Damage);
  78. }
  79. if (bullet.Collide(ship2) && !ship2.Uncollidable)
  80. {
  81. bullet.DeleteProjectile();
  82. HitPlayer2(gameTime, bullet.Damage);
  83. }
  84. }
  85. }
  86. }
  87. protected void HitPlayer2(TimeSpan gameTime, int damage)
  88. {
  89. SpacewarGame.Players[1].Health -= damage;
  90. if (SpacewarGame.Players[1].Health <= 0)
  91. {
  92. SpacewarGame.Players[1].Health = 5;
  93. if (SpacewarGame.GameState == GameState.PlayEvolved)
  94. particles.AddExplosion(ship2.Position);
  95. player1Score++;
  96. Sound.PlayCue(Sounds.ExplodeShip);
  97. resetShips(gameTime);
  98. }
  99. else
  100. {
  101. Sound.PlayCue(Sounds.DamageShip);
  102. }
  103. }
  104. protected void HitPlayer1(TimeSpan gameTime, int damage)
  105. {
  106. SpacewarGame.Players[0].Health -= damage;
  107. if (SpacewarGame.Players[0].Health <= 0)
  108. {
  109. SpacewarGame.Players[0].Health = 5;
  110. if (SpacewarGame.GameState == GameState.PlayEvolved)
  111. particles.AddExplosion(ship1.Position);
  112. player2Score++;
  113. Sound.PlayCue(Sounds.ExplodeShip);
  114. resetShips(gameTime);
  115. }
  116. else
  117. {
  118. Sound.PlayCue(Sounds.DamageShip);
  119. }
  120. }
  121. private void resetShips(TimeSpan gameTime)
  122. {
  123. //When either player dies reset both ships to their start positions
  124. if (SpacewarGame.GameState == GameState.PlayEvolved)
  125. {
  126. ship2.ResetShip(gameTime, new Vector3(SpacewarGame.Settings.Ships[1].StartPosition, 0.0f));
  127. ship1.ResetShip(gameTime, new Vector3(SpacewarGame.Settings.Ships[0].StartPosition, 0.0f));
  128. }
  129. else
  130. {
  131. ship2.ResetShip(gameTime, new Vector3(250, 0, 0));
  132. ship1.ResetShip(gameTime, new Vector3(-250, 0, 0));
  133. }
  134. ship1.Silence();
  135. ship2.Silence();
  136. //and remove any bullets
  137. bullets.Clear();
  138. }
  139. /// <summary>
  140. /// Tidies up anything that may need tidying up
  141. /// </summary>
  142. public override void Shutdown()
  143. {
  144. //Quiet any ship noises
  145. ship1.Silence();
  146. ship2.Silence();
  147. base.Shutdown();
  148. }
  149. /// <summary>
  150. /// OnCreateDevice is called when the device is created
  151. /// </summary>
  152. public override void OnCreateDevice()
  153. {
  154. base.OnCreateDevice();
  155. ship1.OnCreateDevice();
  156. ship2.OnCreateDevice();
  157. ship1.ShapeItem.OnCreateDevice();
  158. ship2.ShapeItem.OnCreateDevice();
  159. sun.ShapeItem.OnCreateDevice();
  160. backdrop.ShapeItem.OnCreateDevice();
  161. }
  162. }
  163. }