forked from CartBlanche/MonoGame-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDog.cs
executable file
·71 lines (60 loc) · 2.33 KB
/
Dog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#region File Description
//-----------------------------------------------------------------------------
// Dog.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion
namespace Audio3D
{
/// Entity class which sits in one place and plays dog sounds.
/// This uses a looping sound, which must be explicitly stopped
/// to prevent it going on forever. See the Cat class for an
/// example of using a single-shot sound.
class Dog : SpriteEntity
{
#region Fields
// How long until we should start or stop the sound.
TimeSpan timeDelay = TimeSpan.Zero;
// The sound which is currently playing, if any.
SoundEffectInstance activeSound = null;
#endregion
/// <summary>
/// Updates the position of the dog, and plays sounds.
/// </summary>
public override void Update(GameTime gameTime, AudioManager audioManager)
{
// Set the entity to a fixed position.
Position = new Vector3(0, 0, -4000);
Forward = Vector3.Forward;
Up = Vector3.Up;
Velocity = Vector3.Zero;
// If the time delay has run out, start or stop the looping sound.
// This would normally go on forever, but we stop it after a six
// second delay, then start it up again after four more seconds.
timeDelay -= gameTime.ElapsedGameTime;
if (timeDelay < TimeSpan.Zero)
{
if (activeSound == null)
{
// If no sound is currently playing, trigger one.
activeSound = audioManager.Play3DSound("DogSound", true, this);
timeDelay += TimeSpan.FromSeconds(6);
}
else
{
// Otherwise stop the current sound.
activeSound.Stop(false);
activeSound = null;
timeDelay += TimeSpan.FromSeconds(4);
}
}
}
}
}