WaypointGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //-----------------------------------------------------------------------------
  2. // WaypointSample.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Audio;
  10. //using Microsoft.Xna.Framework.GamerServices;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. using Microsoft.Xna.Framework.Input.Touch;
  14. //using Microsoft.Xna.Framework.Storage;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Media;
  17. namespace Waypoint
  18. {
  19. /// <summary>
  20. /// This sample shows how an AI can navigate from point to point using
  21. /// several different behaviors
  22. /// </summary>
  23. public class WaypointGame : Game
  24. {
  25. /// <summary>
  26. /// Screen width in pixels
  27. /// </summary>
  28. const int screenWidth = 640;
  29. /// <summary>
  30. /// Screen height in pixels
  31. /// </summary>
  32. const int screenHeight = 480;
  33. /// <summary>
  34. /// Cursor move speed in pixels per second
  35. /// </summary>
  36. const float cursorMoveSpeed = 250.0f;
  37. // Graphics data
  38. GraphicsDeviceManager graphics;
  39. SpriteBatch spriteBatch;
  40. // Cursor data
  41. Texture2D cursorTexture;
  42. Vector2 cursorCenter;
  43. Vector2 cursorLocation;
  44. // HUD data
  45. SpriteFont hudFont;
  46. // Where the HUD draws on the screen
  47. Vector2 hudLocation;
  48. // Input data
  49. KeyboardState previousKeyboardState;
  50. GamePadState previousGamePadState;
  51. MouseState previousMouseState;
  52. KeyboardState currentKeyboardState;
  53. GamePadState currentGamePadState;
  54. MouseState currentMouseState;
  55. TouchCollection currentTouchCollection;
  56. // The waypoint-following tank
  57. Tank tank;
  58. /// <summary>
  59. /// Construct a WaypointSample object
  60. /// </summary>
  61. public WaypointGame()
  62. {
  63. graphics = new GraphicsDeviceManager(this);
  64. Content.RootDirectory = "Content";
  65. graphics.PreferredBackBufferWidth = screenWidth;
  66. graphics.PreferredBackBufferHeight = screenHeight;
  67. // Make the mouse cursor visible (for Desktop platforms)
  68. this.IsMouseVisible = true;
  69. tank = new Tank(this);
  70. Components.Add(tank);
  71. }
  72. /// <summary>
  73. /// Allows the game to perform any initialization it needs to before starting
  74. /// to run. This is where it can query for any required services and load and
  75. /// non-graphic related content. Calling base.Initialize will enumerate
  76. /// through any components and initialize them as well.
  77. /// </summary>
  78. protected override void Initialize()
  79. {
  80. // This places the HUD near the upper left corner of the screen
  81. hudLocation = new Vector2(
  82. (float)Math.Floor(screenWidth * .01f),
  83. (float)Math.Floor(screenHeight * .01f));
  84. // places the cursor in the center of the screen
  85. cursorLocation =
  86. new Vector2((float)screenWidth / 2, (float)screenHeight / 2);
  87. // places the tank halfway between the center of the screen and the
  88. // upper left corner
  89. tank.Reset(
  90. new Vector2((float)screenWidth / 4, (float)screenHeight / 4));
  91. base.Initialize();
  92. }
  93. /// <summary>
  94. /// LoadContent will be called once per game and is the place to load
  95. /// all of your content.
  96. /// </summary>
  97. protected override void LoadContent()
  98. {
  99. // Create a new SpriteBatch, which can be used to draw textures.
  100. spriteBatch = new SpriteBatch(GraphicsDevice);
  101. cursorTexture = Content.Load<Texture2D>("cursor");
  102. cursorCenter =
  103. new Vector2(cursorTexture.Width / 2, cursorTexture.Height / 2);
  104. hudFont = Content.Load<SpriteFont>("HUDFont");
  105. }
  106. /// <summary>
  107. /// Allows the game to run logic such as updating the world,
  108. /// checking for collisions, gathering input, and playing audio.
  109. /// </summary>
  110. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  111. protected override void Update(GameTime gameTime)
  112. {
  113. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  114. HandleInput(elapsedTime);
  115. base.Update(gameTime);
  116. }
  117. /// <summary>
  118. /// This is called when the game should draw itself.
  119. /// </summary>
  120. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  121. protected override void Draw(GameTime gameTime)
  122. {
  123. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  124. base.Draw(gameTime);
  125. string HudString = "Behavior : " + tank.BehaviorType.ToString();
  126. spriteBatch.Begin();
  127. // Draw the cursor
  128. spriteBatch.Draw(cursorTexture, cursorLocation, null, Color.White, 0f,
  129. cursorCenter, 1f, SpriteEffects.None, 0f);
  130. // Draw the string for current behavior
  131. spriteBatch.DrawString(hudFont, HudString, hudLocation, Color.White);
  132. // Draw the string for current behavior
  133. spriteBatch.DrawString(hudFont, "Press B to change behavior", hudLocation + new Vector2(0, 25), Color.White);
  134. spriteBatch.DrawString(hudFont, "Press A to add a waypoint", hudLocation + new Vector2(0, 50), Color.White);
  135. spriteBatch.DrawString(hudFont, "Press X to reset the tank", hudLocation + new Vector2(0, 75), Color.White);
  136. spriteBatch.DrawString(hudFont, "Use the left thumbstick or\n arrow keys to move the cursor", hudLocation + new Vector2(0, 100), Color.White);
  137. spriteBatch.End();
  138. }
  139. /// <summary>
  140. /// Read keyboard and gamepad input
  141. /// </summary>
  142. private void HandleInput(float elapsedTime)
  143. {
  144. previousGamePadState = currentGamePadState;
  145. previousKeyboardState = currentKeyboardState;
  146. previousMouseState = currentMouseState;
  147. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  148. currentKeyboardState = Keyboard.GetState();
  149. currentMouseState = Mouse.GetState();
  150. currentTouchCollection = TouchPanel.GetState();
  151. //bool touched = false;
  152. int touchCount = 0;
  153. // tap the screen to select
  154. foreach (TouchLocation location in currentTouchCollection)
  155. {
  156. switch (location.State)
  157. {
  158. case TouchLocationState.Pressed:
  159. //touched = true;
  160. touchCount++;
  161. cursorLocation = location.Position;
  162. break;
  163. case TouchLocationState.Moved:
  164. break;
  165. case TouchLocationState.Released:
  166. break;
  167. }
  168. }
  169. if ( currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
  170. {
  171. cursorLocation.X = currentMouseState.X;
  172. cursorLocation.Y = currentMouseState.Y;
  173. touchCount = 1;
  174. }
  175. if (currentMouseState.MiddleButton == ButtonState.Released && previousMouseState.MiddleButton == ButtonState.Pressed) {
  176. touchCount = 2;
  177. }
  178. if (currentMouseState.RightButton == ButtonState.Released && previousMouseState.RightButton == ButtonState.Pressed) {
  179. touchCount = 3;
  180. }
  181. // Allows the game to exit
  182. if (currentGamePadState.Buttons.Back == ButtonState.Pressed ||
  183. currentKeyboardState.IsKeyDown(Keys.Escape))
  184. #if !___IOS___
  185. this.Exit();
  186. #endif
  187. // Update the cursor location by listening for left thumbstick input on
  188. // the GamePad and direction key input on the Keyboard, making sure to
  189. // keep the cursor inside the screen boundary
  190. cursorLocation.X +=
  191. currentGamePadState.ThumbSticks.Left.X * cursorMoveSpeed * elapsedTime;
  192. cursorLocation.Y -=
  193. currentGamePadState.ThumbSticks.Left.Y * cursorMoveSpeed * elapsedTime;
  194. if (currentKeyboardState.IsKeyDown(Keys.Up))
  195. {
  196. cursorLocation.Y -= elapsedTime * cursorMoveSpeed;
  197. }
  198. if (currentKeyboardState.IsKeyDown(Keys.Down))
  199. {
  200. cursorLocation.Y += elapsedTime * cursorMoveSpeed;
  201. }
  202. if (currentKeyboardState.IsKeyDown(Keys.Left))
  203. {
  204. cursorLocation.X -= elapsedTime * cursorMoveSpeed;
  205. }
  206. if (currentKeyboardState.IsKeyDown(Keys.Right))
  207. {
  208. cursorLocation.X += elapsedTime * cursorMoveSpeed;
  209. }
  210. cursorLocation.X = MathHelper.Clamp(cursorLocation.X, 0f, screenWidth);
  211. cursorLocation.Y = MathHelper.Clamp(cursorLocation.Y, 0f, screenHeight);
  212. // Change the tank move behavior if the user pressed B on
  213. // the GamePad or on the Keyboard.
  214. if ((previousGamePadState.Buttons.B == ButtonState.Released &&
  215. currentGamePadState.Buttons.B == ButtonState.Pressed) ||
  216. (previousKeyboardState.IsKeyUp(Keys.B) &&
  217. currentKeyboardState.IsKeyDown(Keys.B)) || ( touchCount == 2 ))
  218. {
  219. tank.CycleBehaviorType();
  220. }
  221. // Add the cursor's location to the WaypointList if the user pressed A on
  222. // the GamePad or on the Keyboard.
  223. if ((previousGamePadState.Buttons.A == ButtonState.Released &&
  224. currentGamePadState.Buttons.A == ButtonState.Pressed) ||
  225. (previousKeyboardState.IsKeyUp(Keys.A) &&
  226. currentKeyboardState.IsKeyDown(Keys.A)) || ( touchCount == 1 ))
  227. {
  228. tank.Waypoints.Enqueue(cursorLocation);
  229. }
  230. // Delete all the current waypoints and reset the tanks� location if
  231. // the user pressed X on the GamePad or on the Keyboard.
  232. if ((previousGamePadState.Buttons.X == ButtonState.Released &&
  233. currentGamePadState.Buttons.X == ButtonState.Pressed) ||
  234. (previousKeyboardState.IsKeyUp(Keys.X) &&
  235. currentKeyboardState.IsKeyDown(Keys.X)) || ( touchCount == 3 ))
  236. {
  237. tank.Reset(
  238. new Vector2((float)screenWidth / 4, (float)screenHeight / 4));
  239. }
  240. }
  241. }
  242. }