Cat.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Cat.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 System;
  11. using Microsoft.Xna.Framework;
  12. #endregion
  13. namespace Audio3D
  14. {
  15. /// <summary>
  16. /// Entity class which moves in a circle and plays cat sounds.
  17. /// This uses a single-shot sound, which will stop automatically
  18. /// when it finishes playing. See the Dog class for an example of
  19. /// using a looping sound.
  20. /// </summary>
  21. class Cat : SpriteEntity
  22. {
  23. #region Fields
  24. // How long until we should play the next sound.
  25. TimeSpan timeDelay = TimeSpan.Zero;
  26. // Random number generator for choosing between sound variations.
  27. static Random random = new Random();
  28. #endregion
  29. /// <summary>
  30. /// Updates the position of the cat, and plays sounds.
  31. /// </summary>
  32. public override void Update(GameTime gameTime, AudioManager audioManager)
  33. {
  34. // Move the cat in a big circle.
  35. double time = gameTime.TotalGameTime.TotalSeconds;
  36. float dx = (float)-Math.Cos(time);
  37. float dz = (float)-Math.Sin(time);
  38. Vector3 newPosition = new Vector3(dx, 0, dz) * 6000;
  39. // Update entity position and velocity.
  40. Velocity = newPosition - Position;
  41. Position = newPosition;
  42. if (Velocity == Vector3.Zero)
  43. Forward = Vector3.Forward;
  44. else
  45. Forward = Vector3.Normalize(Velocity);
  46. Up = Vector3.Up;
  47. // If the time delay has run out, trigger another single-shot sound.
  48. timeDelay -= gameTime.ElapsedGameTime;
  49. if (timeDelay < TimeSpan.Zero)
  50. {
  51. // For variety, randomly choose between three slightly different
  52. // variants of the sound (CatSound0, CatSound1, and CatSound2).
  53. string soundName = "CatSound" + random.Next(3);
  54. audioManager.Play3DSound(soundName, false, this);
  55. timeDelay += TimeSpan.FromSeconds(1.25f);
  56. }
  57. }
  58. }
  59. }