Cat.cs 2.2 KB

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