123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Game.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace Audio3D
- {
- /// <summary>
- /// Sample showing how to implement 3D audio.
- /// </summary>
- public class Audio3DGame : Microsoft.Xna.Framework.Game
- {
- #region Fields
- GraphicsDeviceManager graphics;
- AudioManager audioManager;
- SpriteEntity cat;
- SpriteEntity dog;
- Texture2D checkerTexture;
- QuadDrawer quadDrawer;
- Vector3 cameraPosition = new Vector3(0, 512, 0);
- Vector3 cameraForward = Vector3.Forward;
- Vector3 cameraUp = Vector3.Up;
- Vector3 cameraVelocity = Vector3.Zero;
- KeyboardState currentKeyboardState;
- GamePadState currentGamePadState;
- #endregion
- #region Initialization
- public Audio3DGame()
- {
- Content.RootDirectory = "Content";
- graphics = new GraphicsDeviceManager(this);
- audioManager = new AudioManager(this);
- Components.Add(audioManager);
- cat = new Cat();
- dog = new Dog();
- }
- /// <summary>
- /// Load your graphics content.
- /// </summary>
- protected override void LoadContent()
- {
- cat.Texture = Content.Load<Texture2D>("CatTexture");
- dog.Texture = Content.Load<Texture2D>("DogTexture");
- checkerTexture = Content.Load<Texture2D>("checker");
- quadDrawer = new QuadDrawer(graphics.GraphicsDevice);
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// Allows the game to run logic.
- /// </summary>
- protected override void Update(GameTime gameTime)
- {
- HandleInput();
- UpdateCamera();
- // Tell the AudioManager about the new camera position.
- audioManager.Listener.Position = cameraPosition;
- audioManager.Listener.Forward = cameraForward;
- audioManager.Listener.Up = cameraUp;
- audioManager.Listener.Velocity = cameraVelocity;
- // Tell our game entities to move around and play sounds.
- cat.Update(gameTime, audioManager);
- dog.Update(gameTime, audioManager);
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice device = graphics.GraphicsDevice;
- device.Clear(Color.CornflowerBlue);
- device.BlendState = BlendState.AlphaBlend;
- // Compute camera matrices.
- Matrix view = Matrix.CreateLookAt(cameraPosition,
- cameraPosition + cameraForward,
- cameraUp);
- Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, device.Viewport.AspectRatio,
- 1, 100000);
- // Draw the checkered ground polygon.
- Matrix groundTransform = Matrix.CreateScale(20000) *
- Matrix.CreateRotationX(MathHelper.PiOver2);
- quadDrawer.DrawQuad(checkerTexture, 32, groundTransform, view, projection);
- // Draw the game entities.
- cat.Draw(quadDrawer, cameraPosition, view, projection);
- dog.Draw(quadDrawer, cameraPosition, view, projection);
- base.Draw(gameTime);
- }
- #endregion
- #region Handle Input
- /// <summary>
- /// Handles input for quitting the game.
- /// </summary>
- void HandleInput()
- {
- currentKeyboardState = Keyboard.GetState();
- currentGamePadState = GamePad.GetState(PlayerIndex.One);
- // Check for exit.
- if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
- currentGamePadState.Buttons.Back == ButtonState.Pressed)
- {
- Exit();
- }
- }
- /// <summary>
- /// Handles input for moving the camera.
- /// </summary>
- void UpdateCamera()
- {
- const float turnSpeed = 0.05f;
- const float accelerationSpeed = 4;
- const float frictionAmount = 0.98f;
- // Turn left or right.
- float turn = -currentGamePadState.ThumbSticks.Left.X * turnSpeed;
- if (currentKeyboardState.IsKeyDown(Keys.Left))
- turn += turnSpeed;
- if (currentKeyboardState.IsKeyDown(Keys.Right))
- turn -= turnSpeed;
- cameraForward = Vector3.TransformNormal(cameraForward,
- Matrix.CreateRotationY(turn));
- // Accelerate forward or backward.
- float accel = currentGamePadState.ThumbSticks.Left.Y * accelerationSpeed;
- if (currentKeyboardState.IsKeyDown(Keys.Up))
- accel += accelerationSpeed;
- if (currentKeyboardState.IsKeyDown(Keys.Down))
- accel -= accelerationSpeed;
- cameraVelocity += cameraForward * accel;
- // Add velocity to the current position.
- cameraPosition += cameraVelocity;
- // Apply the friction force.
- cameraVelocity *= frictionAmount;
- }
- #endregion
- }
- }
|