Player.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //-----------------------------------------------------------------------------
  2. // Player.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using RacingGame.GameScreens;
  12. using RacingGame.Graphics;
  13. using RacingGame.Helpers;
  14. using RacingGame.Landscapes;
  15. using RacingGame.Properties;
  16. using RacingGame.Sounds;
  17. using RacingGame.Tracks;
  18. using Texture = RacingGame.Graphics.Texture;
  19. using Microsoft.Xna.Framework.Graphics;
  20. namespace RacingGame.GameLogic
  21. {
  22. /// <summary>
  23. /// Player helper class, holds all the current game properties:
  24. /// Fuel, Health, Speed, Lifes and Score.
  25. /// Note: This class is just used in RacingGame and we only have
  26. /// 1 instance of it for the current player for the current game.
  27. /// If we want to have more than 1 player (e.g. in multiplayer mode)
  28. /// you should add a multiplayer class and have all player instances there.
  29. /// </summary>
  30. public class Player : ChaseCamera
  31. {
  32. /// <summary>
  33. /// Remember all lap times for the victory screen.
  34. /// </summary>
  35. private List<float> lapTimes = new List<float>();
  36. /// <summary>
  37. /// The number of laps in each race
  38. /// </summary>
  39. private const int LapCount = 3;
  40. /// <summary>
  41. /// Add lap time
  42. /// </summary>
  43. /// <param name="setLapTime">Lap time</param>
  44. public void AddLapTime(float setLapTime)
  45. {
  46. lapTimes.Add(setLapTime);
  47. }
  48. /// <summary>
  49. /// The amount of time (in milliseconds) the car has
  50. /// been in the air since last touching the ground
  51. /// If the car is in the air and does not reach the
  52. /// ground again for too long, its game over!
  53. /// </summary>
  54. private float inAirTimeMilliseconds = 0.0f;
  55. /// <summary>
  56. /// The amount of time (in milliseconds) the car must be
  57. /// in the air before game over occurs
  58. /// </summary>
  59. private const float InAirTimeoutMilliseconds = 3000.0f;
  60. /// <summary>
  61. /// Create chase camera
  62. /// </summary>
  63. /// <param name="setCarPosition">Set car position</param>
  64. /// <param name="setCameraPos">Set camera pos</param>
  65. public Player(Vector3 setCarPosition)
  66. : base(setCarPosition)
  67. {
  68. }
  69. /// <summary>
  70. /// Reset player values.
  71. /// </summary>
  72. public override void Reset()
  73. {
  74. base.Reset();
  75. lapTimes.Clear();
  76. }
  77. /// <summary>
  78. /// Update game logic, called every frame.
  79. /// </summary>
  80. public override void Update()
  81. {
  82. // Don't handle any more game logic if game is over.
  83. if (RacingGameManager.InGame &&
  84. ZoomInTime <= 0)
  85. {
  86. // Game over? Then show end screen!
  87. if (isGameOver)
  88. {
  89. // Just rotate around, don't use camera class!
  90. cameraPos = CarPosition + new Vector3(0, -5, +20) +
  91. Vector3.TransformNormal(new Vector3(30, 0, 0),
  92. Matrix.CreateRotationZ(BaseGame.TotalTimeMilliseconds / 2593.0f));
  93. BaseGame.ViewMatrix = Matrix.CreateLookAt(
  94. cameraPos, CarPosition, CarUpVector);
  95. int rank = Highscores.GetRankFromCurrentTime(
  96. this.levelNum, (int)this.BestTimeMilliseconds);
  97. this.currentGameTimeMilliseconds = this.BestTimeMilliseconds;
  98. if (victory)
  99. {
  100. // Display Victory message
  101. TextureFont.WriteTextCentered(
  102. BaseGame.Width / 2, BaseGame.Height / 7,
  103. "Victory! You won.",
  104. Color.LightGreen, 1.25f);
  105. }
  106. else
  107. {
  108. // Display game over message
  109. TextureFont.WriteTextCentered(
  110. BaseGame.Width / 2, BaseGame.Height / 7,
  111. "Game Over! You lost.",
  112. Color.Red, 1.25f);
  113. }
  114. for (int num = 0; num < lapTimes.Count; num++)
  115. TextureFont.WriteTextCentered(
  116. BaseGame.Width / 2,
  117. BaseGame.Height / 7 + BaseGame.YToRes(35) * (1 + num),
  118. "Lap " + (num + 1) + " Time: " +
  119. (((int)lapTimes[num]) / 60).ToString("00") + ":" +
  120. (((int)lapTimes[num]) % 60).ToString("00") + "." +
  121. (((int)(lapTimes[num] * 100)) % 100).ToString("00"),
  122. Color.White, 1.25f);
  123. TextureFont.WriteTextCentered(
  124. BaseGame.Width / 2,
  125. BaseGame.Height / 7 + BaseGame.YToRes(35) * (1 + lapTimes.Count),
  126. "Rank: " + (1 + rank),
  127. Color.White, 1.25f);
  128. // Don't continue processing game logic
  129. return;
  130. }
  131. // Check if car is in the air,
  132. // used to check if the player died.
  133. if (this.isCarOnGround == false)
  134. inAirTimeMilliseconds +=
  135. BaseGame.ElapsedTimeThisFrameInMilliseconds;
  136. else
  137. // Back on ground, reset
  138. inAirTimeMilliseconds = 0;
  139. // Game not over yet, check if we lost or won.
  140. // Check if we have fallen from the track
  141. float trackDistance = Vector3.Distance(CarPosition, groundPlanePos);
  142. if (trackDistance > 20 ||
  143. inAirTimeMilliseconds > InAirTimeoutMilliseconds)
  144. {
  145. // Reset player variables (stop car, etc.)
  146. ClearVariablesForGameOver();
  147. // And indicate that game is over and we lost!
  148. isGameOver = true;
  149. victory = false;
  150. Sound.Play(Sound.Sounds.CarLose);
  151. // Also stop engine sound
  152. Sound.StopGearSound();
  153. }
  154. // Finished all laps? Then we won!
  155. if (CurrentLap >= LapCount)
  156. {
  157. // Reset player variables (stop car, etc.)
  158. ClearVariablesForGameOver();
  159. // When you win, you start an extra lap we don't want to show
  160. this.lap--;
  161. // Then game is over and we won!
  162. isGameOver = true;
  163. victory = true;
  164. Sound.Play(Sound.Sounds.Victory);
  165. // Also stop engine sound
  166. Sound.StopGearSound();
  167. }
  168. }
  169. base.Update();
  170. }
  171. }
  172. }