2
0

Dog.cs 2.3 KB

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