Dog.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //-----------------------------------------------------------------------------
  2. // Dog.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. using Microsoft.Xna.Framework.Audio;
  10. namespace Audio3D
  11. {
  12. /// Entity class which sits in one place and plays dog sounds.
  13. /// This uses a looping sound, which must be explicitly stopped
  14. /// to prevent it going on forever. See the Cat class for an
  15. /// example of using a single-shot sound.
  16. class Dog : SpriteEntity
  17. {
  18. // How long until we should start or stop the sound.
  19. TimeSpan timeDelay = TimeSpan.Zero;
  20. // The sound which is currently playing, if any.
  21. SoundEffectInstance activeSound = null;
  22. /// <summary>
  23. /// Updates the position of the dog, and plays sounds.
  24. /// </summary>
  25. public override void Update(GameTime gameTime, AudioManager audioManager)
  26. {
  27. // Set the entity to a fixed position.
  28. Position = new Vector3(0, 0, -4000);
  29. Forward = Vector3.Forward;
  30. Up = Vector3.Up;
  31. Velocity = Vector3.Zero;
  32. // If the time delay has run out, start or stop the looping sound.
  33. // This would normally go on forever, but we stop it after a six
  34. // second delay, then start it up again after four more seconds.
  35. timeDelay -= gameTime.ElapsedGameTime;
  36. if (timeDelay < TimeSpan.Zero)
  37. {
  38. if (activeSound == null)
  39. {
  40. // If no sound is currently playing, trigger one.
  41. activeSound = audioManager.Play3DSound("DogSound", true, this);
  42. timeDelay += TimeSpan.FromSeconds(6);
  43. }
  44. else
  45. {
  46. // Otherwise stop the current sound.
  47. activeSound.Stop(false);
  48. activeSound = null;
  49. timeDelay += TimeSpan.FromSeconds(4);
  50. }
  51. }
  52. }
  53. }
  54. }