BasePlayer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //-----------------------------------------------------------------------------
  2. // BasePlayer.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using RacingGame.GameScreens;
  11. using RacingGame.Graphics;
  12. using RacingGame.Helpers;
  13. using RacingGame.Landscapes;
  14. using RacingGame.Properties;
  15. using RacingGame.Sounds;
  16. using RacingGame.Tracks;
  17. namespace RacingGame.GameLogic
  18. {
  19. /// <summary>
  20. /// Base player helper class, holds all the current game values:
  21. /// Game time, game over, level number, victory.
  22. /// More stuff that has impact to the CarController or ChaseCamera classes
  23. /// should be included here, else add them directly to the Player class,
  24. /// which handles all the game logic.
  25. /// For example adding items or powerups should be done in this class
  26. /// if they affect the speed or physics of our car.
  27. /// For multiplayer purposes this class should be extended to assign
  28. /// a player id to each player and link the network stuff up here.
  29. /// </summary>
  30. public class BasePlayer
  31. {
  32. /// <summary>
  33. /// Current game time in ms. Used for time display in game. Also used to
  34. /// update the sun position and for the highscores.
  35. /// Will be stopped if we die or if we are still zooming in.
  36. /// </summary>
  37. protected float currentGameTimeMilliseconds = 0;
  38. /// <summary>
  39. /// Current lap. Increases and when we reach 3, the game is won.
  40. /// </summary>
  41. protected int lap;
  42. /// <summary>
  43. /// Current lap
  44. /// </summary>
  45. public int CurrentLap
  46. {
  47. get
  48. {
  49. return lap;
  50. }
  51. }
  52. /// <summary>
  53. /// Remember best lap time, unused until we complete the first lap.
  54. /// Then it is set every lap, always using the best and fastest lap time.
  55. /// </summary>
  56. private float bestLapTimeMilliseconds = 0;
  57. /// <summary>
  58. /// Best lap time we have archived in this game
  59. /// </summary>
  60. public float BestTimeMilliseconds
  61. {
  62. get
  63. {
  64. return bestLapTimeMilliseconds;
  65. }
  66. }
  67. /// <summary>
  68. /// Start new lap, will reset all lap variables and the game time.
  69. /// If all laps are done the game is over.
  70. /// </summary>
  71. protected void StartNewLap()
  72. {
  73. lap++;
  74. RacingGameManager.Landscape.StartNewLap();
  75. // Got new best time?
  76. if (bestLapTimeMilliseconds == 0 ||
  77. currentGameTimeMilliseconds < bestLapTimeMilliseconds)
  78. bestLapTimeMilliseconds = currentGameTimeMilliseconds;
  79. // Start at 0:00.00 again
  80. currentGameTimeMilliseconds = zoomInTime;
  81. }
  82. /// <summary>
  83. /// Game time ms, will return negative values if currently zooming in!
  84. /// </summary>
  85. /// <returns>Int</returns>
  86. public float GameTimeMilliseconds
  87. {
  88. get
  89. {
  90. return currentGameTimeMilliseconds - zoomInTime;
  91. }
  92. }
  93. /// <summary>
  94. /// How long do we zoom in.
  95. /// </summary>
  96. public const int StartGameZoomTimeMilliseconds = 5000;
  97. /// <summary>
  98. /// Zoom in time
  99. /// </summary>
  100. private float zoomInTime = StartGameZoomTimeMilliseconds;
  101. /// <summary>
  102. /// Zoom in time
  103. /// </summary>
  104. /// <returns>Float</returns>
  105. protected float ZoomInTime
  106. {
  107. get
  108. {
  109. return zoomInTime;
  110. }
  111. set
  112. {
  113. zoomInTime = value;
  114. }
  115. }
  116. /// <summary>
  117. /// The amount of time to remain fully zoomed in waiting for start light;
  118. /// </summary>
  119. public const int StartGameZoomedInTime = 3000;
  120. /// <summary>
  121. /// Won or lost?
  122. /// </summary>
  123. protected bool victory;
  124. /// <summary>
  125. /// Property for Victory
  126. /// </summary>
  127. public bool Victory
  128. {
  129. get
  130. {
  131. return victory;
  132. }
  133. }
  134. /// <summary>
  135. /// Level num, set when starting game!
  136. /// </summary>
  137. protected int levelNum;
  138. public int LevelNum
  139. {
  140. get
  141. {
  142. return levelNum;
  143. }
  144. }
  145. /// <summary>
  146. /// Game over?
  147. /// </summary>
  148. protected bool isGameOver;
  149. /// <summary>
  150. /// Is game over?
  151. /// </summary>
  152. /// <returns>Bool</returns>
  153. public bool GameOver
  154. {
  155. get
  156. {
  157. return isGameOver;
  158. }
  159. }
  160. /// <summary>
  161. /// Did the player win the game? Makes only sense if GameOver is true!
  162. /// </summary>
  163. public bool WonGame
  164. {
  165. get
  166. {
  167. return victory;
  168. }
  169. }
  170. /// <summary>
  171. /// Remember if we already uploaded our highscore for this game.
  172. /// Don't do this twice (e.g. when pressing esc).
  173. /// </summary>
  174. private bool alreadyUploadedHighscore = false;
  175. /// <summary>
  176. /// Set game over and upload highscore
  177. /// </summary>
  178. public void SetGameOverAndUploadHighscore()
  179. {
  180. // Set gameOver to true to mark this game as ended.
  181. isGameOver = true;
  182. // Upload highscore
  183. if (alreadyUploadedHighscore == false)
  184. {
  185. alreadyUploadedHighscore = true;
  186. Highscores.SubmitHighscore(levelNum,
  187. (int)currentGameTimeMilliseconds);
  188. }
  189. }
  190. /// <summary>
  191. /// Helper to determinate if user can control the car.
  192. /// If game just started we still zoom into the chase camera.
  193. /// </summary>
  194. /// <returns>Bool</returns>
  195. public bool CanControlCar
  196. {
  197. get
  198. {
  199. return zoomInTime <= 0 &&
  200. GameOver == false;
  201. }
  202. }
  203. private bool firstFrame = true;
  204. /// <summary>
  205. /// Reset all player entries for restarting a game.
  206. /// In derived classes reset all the variables we need to reset for
  207. /// a new game there (e.g. car speed in CarController or
  208. /// cameraWobbel in ChaseCamera).
  209. /// </summary>
  210. public virtual void Reset()
  211. {
  212. levelNum = TrackSelection.SelectedTrackNumber;
  213. isGameOver = false;
  214. alreadyUploadedHighscore = false;
  215. currentGameTimeMilliseconds = 0;
  216. bestLapTimeMilliseconds = 0;
  217. lap = 0;
  218. victory = false;
  219. zoomInTime = StartGameZoomTimeMilliseconds;
  220. firstFrame = true;
  221. }
  222. /// <summary>
  223. /// Clear variables for game over
  224. /// </summary>
  225. public virtual void ClearVariablesForGameOver()
  226. {
  227. }
  228. /// <summary>
  229. /// Update game logic, called every frame. In Rocket Commander we did
  230. /// all the game logic in one big method inside the player class, but it
  231. /// was hard to add new game logic and many small things were also in
  232. /// the GameAsteroidManager. For this game we split everything up into
  233. /// much more classes and every class handles only its own variables.
  234. /// For example this class just handles the game time and zoom in time,
  235. /// for the car speed and physics just go into the CarController class.
  236. /// </summary>
  237. public virtual void Update()
  238. {
  239. // Since there is no loading screen, we need to skip the first frame because
  240. // the loading will cause ElapsedTimeThisFrameInMilliseconds to be too high
  241. if (firstFrame)
  242. {
  243. firstFrame = false;
  244. return;
  245. }
  246. // Handle zoomInTime at the beginning of a game
  247. if (!RacingGameManager.InMenu && zoomInTime > 0)
  248. {
  249. float lastZoomInTime = zoomInTime;
  250. zoomInTime -= BaseGame.ElapsedTimeThisFrameInMilliseconds;
  251. if (zoomInTime < 2000 &&
  252. (int)((lastZoomInTime + 1000) / 1000) != (int)((zoomInTime + 1000) / 1000))
  253. {
  254. // Handle start traffic light object (red, yellow, green!)
  255. RacingGameManager.Landscape.ReplaceStartLightObject(
  256. 2 - (int)((zoomInTime + 1000) / 1000));
  257. }
  258. }
  259. // Don't handle any more game logic if game is over or still zooming in.
  260. if (CanControlCar == false)
  261. return;
  262. // Increase game time
  263. currentGameTimeMilliseconds += BaseGame.ElapsedTimeThisFrameInMilliseconds;
  264. }
  265. }
  266. }