Game.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace Audio3D
  16. {
  17. /// <summary>
  18. /// Sample showing how to implement 3D audio.
  19. /// </summary>
  20. public class Audio3DGame : Microsoft.Xna.Framework.Game
  21. {
  22. #region Fields
  23. GraphicsDeviceManager graphics;
  24. AudioManager audioManager;
  25. SpriteEntity cat;
  26. SpriteEntity dog;
  27. Texture2D checkerTexture;
  28. QuadDrawer quadDrawer;
  29. Vector3 cameraPosition = new Vector3(0, 512, 0);
  30. Vector3 cameraForward = Vector3.Forward;
  31. Vector3 cameraUp = Vector3.Up;
  32. Vector3 cameraVelocity = Vector3.Zero;
  33. KeyboardState currentKeyboardState;
  34. GamePadState currentGamePadState;
  35. #endregion
  36. #region Initialization
  37. public Audio3DGame()
  38. {
  39. Content.RootDirectory = "Content";
  40. graphics = new GraphicsDeviceManager(this);
  41. audioManager = new AudioManager(this);
  42. Components.Add(audioManager);
  43. cat = new Cat();
  44. dog = new Dog();
  45. }
  46. /// <summary>
  47. /// Load your graphics content.
  48. /// </summary>
  49. protected override void LoadContent()
  50. {
  51. cat.Texture = Content.Load<Texture2D>("CatTexture");
  52. dog.Texture = Content.Load<Texture2D>("DogTexture");
  53. checkerTexture = Content.Load<Texture2D>("checker");
  54. quadDrawer = new QuadDrawer(graphics.GraphicsDevice);
  55. }
  56. #endregion
  57. #region Update and Draw
  58. /// <summary>
  59. /// Allows the game to run logic.
  60. /// </summary>
  61. protected override void Update(GameTime gameTime)
  62. {
  63. HandleInput();
  64. UpdateCamera();
  65. // Tell the AudioManager about the new camera position.
  66. audioManager.Listener.Position = cameraPosition;
  67. audioManager.Listener.Forward = cameraForward;
  68. audioManager.Listener.Up = cameraUp;
  69. audioManager.Listener.Velocity = cameraVelocity;
  70. // Tell our game entities to move around and play sounds.
  71. cat.Update(gameTime, audioManager);
  72. dog.Update(gameTime, audioManager);
  73. base.Update(gameTime);
  74. }
  75. /// <summary>
  76. /// This is called when the game should draw itself.
  77. /// </summary>
  78. protected override void Draw(GameTime gameTime)
  79. {
  80. GraphicsDevice device = graphics.GraphicsDevice;
  81. device.Clear(Color.CornflowerBlue);
  82. device.BlendState = BlendState.AlphaBlend;
  83. // Compute camera matrices.
  84. Matrix view = Matrix.CreateLookAt(cameraPosition,
  85. cameraPosition + cameraForward,
  86. cameraUp);
  87. Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, device.Viewport.AspectRatio,
  88. 1, 100000);
  89. // Draw the checkered ground polygon.
  90. Matrix groundTransform = Matrix.CreateScale(20000) *
  91. Matrix.CreateRotationX(MathHelper.PiOver2);
  92. quadDrawer.DrawQuad(checkerTexture, 32, groundTransform, view, projection);
  93. // Draw the game entities.
  94. cat.Draw(quadDrawer, cameraPosition, view, projection);
  95. dog.Draw(quadDrawer, cameraPosition, view, projection);
  96. base.Draw(gameTime);
  97. }
  98. #endregion
  99. #region Handle Input
  100. /// <summary>
  101. /// Handles input for quitting the game.
  102. /// </summary>
  103. void HandleInput()
  104. {
  105. currentKeyboardState = Keyboard.GetState();
  106. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  107. // Check for exit.
  108. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  109. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  110. {
  111. Exit();
  112. }
  113. }
  114. /// <summary>
  115. /// Handles input for moving the camera.
  116. /// </summary>
  117. void UpdateCamera()
  118. {
  119. const float turnSpeed = 0.05f;
  120. const float accelerationSpeed = 4;
  121. const float frictionAmount = 0.98f;
  122. // Turn left or right.
  123. float turn = -currentGamePadState.ThumbSticks.Left.X * turnSpeed;
  124. if (currentKeyboardState.IsKeyDown(Keys.Left))
  125. turn += turnSpeed;
  126. if (currentKeyboardState.IsKeyDown(Keys.Right))
  127. turn -= turnSpeed;
  128. cameraForward = Vector3.TransformNormal(cameraForward,
  129. Matrix.CreateRotationY(turn));
  130. // Accelerate forward or backward.
  131. float accel = currentGamePadState.ThumbSticks.Left.Y * accelerationSpeed;
  132. if (currentKeyboardState.IsKeyDown(Keys.Up))
  133. accel += accelerationSpeed;
  134. if (currentKeyboardState.IsKeyDown(Keys.Down))
  135. accel -= accelerationSpeed;
  136. cameraVelocity += cameraForward * accel;
  137. // Add velocity to the current position.
  138. cameraPosition += cameraVelocity;
  139. // Apply the friction force.
  140. cameraVelocity *= frictionAmount;
  141. }
  142. #endregion
  143. }
  144. }