PlayerStatus.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //---------------------------------------------------------------------------------
  2. // Written by Michael Hoffman
  3. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  4. //----------------------------------------------------------------------------------
  5. using AtomicEngine;
  6. using System.IO;
  7. namespace AtomicBlaster
  8. {
  9. static class PlayerStatus
  10. {
  11. // amount of time it takes, in seconds, for a multiplier to expire.
  12. private const float multiplierExpiryTime = 0.8f;
  13. private const int maxMultiplier = 20;
  14. public static int Lives { get; private set; }
  15. public static int Score { get; private set; }
  16. public static int Multiplier { get; private set; }
  17. public static bool IsGameOver { get { return Lives == 0; } }
  18. private static float multiplierTimeLeft; // time until the current multiplier expires
  19. private static int scoreForExtraLife; // score required to gain an extra life
  20. // Static constructor
  21. static PlayerStatus()
  22. {
  23. Reset();
  24. }
  25. public static void Reset()
  26. {
  27. Score = 0;
  28. Multiplier = 1;
  29. Lives = 4;
  30. scoreForExtraLife = 2000;
  31. multiplierTimeLeft = 0;
  32. }
  33. public static void Update()
  34. {
  35. if (Multiplier > 1)
  36. {
  37. // update the multiplier timer
  38. if ((multiplierTimeLeft -= (float)GameRoot.ElapsedTime) <= 0)
  39. {
  40. multiplierTimeLeft = multiplierExpiryTime;
  41. ResetMultiplier();
  42. }
  43. }
  44. }
  45. public static void AddPoints(int basePoints)
  46. {
  47. if (PlayerShip.Instance.IsDead)
  48. return;
  49. Score += basePoints * Multiplier;
  50. while (Score >= scoreForExtraLife)
  51. {
  52. scoreForExtraLife += 2000;
  53. Lives++;
  54. }
  55. }
  56. public static void IncreaseMultiplier()
  57. {
  58. if (PlayerShip.Instance.IsDead)
  59. return;
  60. multiplierTimeLeft = multiplierExpiryTime;
  61. if (Multiplier < maxMultiplier)
  62. Multiplier++;
  63. }
  64. public static void ResetMultiplier()
  65. {
  66. Multiplier = 1;
  67. }
  68. public static void RemoveLife()
  69. {
  70. Lives--;
  71. }
  72. }
  73. }