Audio3DGame.cs 5.5 KB

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