Skip to content

Commit 0e9f67c

Browse files
Merge pull request #16 from red-the-random-dev/intermediate-changes
[w4d7] Sound management implementation
2 parents d1a462a + 7d4f91b commit 0e9f67c

File tree

8 files changed

+152
-3
lines changed

8 files changed

+152
-3
lines changed

Behavior/Defaults/TopDownFrame.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class TopDownFrame : Frame, IXMLSceneBuilder, ILoadable
2323
CameraFrameObject cam = new CameraFrameObject();
2424
CameraManager cameraManager;
2525
ObjectHeapManager obj;
26+
SoundManager audio;
2627
PlayerObject player;
2728
Int32 _id;
2829
readonly List<ResourceLoadTask> resourceLoad = new List<ResourceLoadTask>();
@@ -128,6 +129,8 @@ public void PostLoadTask()
128129
{
129130
x.Runtime["obj"] = obj;
130131
x.Runtime["camera"] = cameraManager;
132+
x.Runtime["audio"] = audio;
133+
x.Start();
131134
}
132135
loaded = true;
133136
LastMWheelValue = Mouse.GetState().ScrollWheelValue;
@@ -386,6 +389,7 @@ public TopDownFrame(Game owner, ResourceHeap globalGameResources, HashSet<TimedE
386389
{
387390
cameraManager = new CameraManager(cam, props);
388391
obj = new ObjectHeapManager(props);
392+
audio = new SoundManager(FrameResources);
389393
}
390394

391395
public override void Update(GameTime gameTime, bool[] controls)
@@ -491,6 +495,7 @@ public override void Initialize()
491495

492496
public override void UnloadContent()
493497
{
498+
audio.Dispose();
494499
FrameResources.Dispose();
495500
props.Clear();
496501
interactable.Clear();

Behavior/Management/ObjectHeapManager.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class ObjectHeapManager
1111
protected Dictionary<String, DynamicRectObject> ObjectHeap;
1212
public PlayerObject Player;
1313

14-
public Vector2 GetScalarVelocity(String name)
14+
public Single GetScalarVelocity(String name)
1515
{
16-
return ObjectHeap[name].Velocity;
16+
return ObjectHeap[name].Velocity.Length();
1717
}
1818

1919
public Single GetDistance(String one, String other)
@@ -55,5 +55,12 @@ public ObjectHeapManager(Dictionary<String, DynamicRectObject> objects)
5555
{
5656
ObjectHeap = objects;
5757
}
58+
59+
public Single GetSoundPanning(String target)
60+
{
61+
Vector2 v = ObjectHeap[target].Location - Player.Location;
62+
v /= v.Length();
63+
return v.X;
64+
}
5865
}
5966
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using DotRPG.Objects;
2+
using Microsoft.Xna.Framework.Audio;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace DotRPG.Behavior.Management
8+
{
9+
public class SoundManager : IDisposable
10+
{
11+
protected ResourceHeap Resources;
12+
Dictionary<String, SoundEffectInstance> PlayedSounds = new Dictionary<string, SoundEffectInstance>();
13+
14+
public SoundManager(ResourceHeap resources)
15+
{
16+
Resources = resources;
17+
}
18+
19+
public void PlayLocal(String ID)
20+
{
21+
Resources.Sounds[ID].Play();
22+
}
23+
24+
public void BufferLocal(String ID, String playID)
25+
{
26+
PlayedSounds.Add(playID, Resources.Sounds[ID].CreateInstance());
27+
}
28+
29+
public void BufferGlobal(String ID, String playID)
30+
{
31+
PlayedSounds.Add(playID, Resources.Global.Sounds[ID].CreateInstance());
32+
}
33+
34+
public void PlayLocal(String ID, Single volume, Single pitch, Single pan)
35+
{
36+
Resources.Sounds[ID].Play(volume, pitch, pan);
37+
}
38+
39+
public void PlayGlobal(String ID)
40+
{
41+
Resources.Global.Sounds[ID].Play();
42+
}
43+
44+
public void PlayGlobal(String ID, Single volume, Single pitch, Single pan)
45+
{
46+
Resources.Global.Sounds[ID].Play(volume, pitch, pan);
47+
}
48+
49+
public void Play(String ID)
50+
{
51+
PlayedSounds[ID].Play();
52+
}
53+
54+
public void SetLooped(String ID, Boolean looped)
55+
{
56+
PlayedSounds[ID].IsLooped = looped;
57+
}
58+
59+
public void SetParameters(String ID, Single volume, Single pitch, Single pan)
60+
{
61+
PlayedSounds[ID].Volume = volume;
62+
PlayedSounds[ID].Pitch = pitch;
63+
PlayedSounds[ID].Pan = pan;
64+
}
65+
66+
public void Stop(String ID)
67+
{
68+
PlayedSounds[ID].Stop();
69+
}
70+
71+
public void Unbuffer(String ID)
72+
{
73+
PlayedSounds[ID].Stop();
74+
PlayedSounds.Remove(ID);
75+
}
76+
77+
public void StopAll()
78+
{
79+
foreach (String x in PlayedSounds.Keys)
80+
{
81+
Stop(x);
82+
}
83+
}
84+
85+
public void UnbufferAll()
86+
{
87+
foreach (String x in PlayedSounds.Keys)
88+
{
89+
Unbuffer(x);
90+
}
91+
}
92+
93+
public void Dispose()
94+
{
95+
UnbufferAll();
96+
}
97+
}
98+
}

Scripting/LuaModule.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ public LuaModule(String initFile, String initName = "dotrpgmodule")
4141
}
4242
}
4343

44+
public void Start()
45+
{
46+
if (Runtime["start"] != null)
47+
{
48+
LuaFunction startFunction = Runtime["start"] as LuaFunction;
49+
try
50+
{
51+
startFunction.Call();
52+
LastError = "";
53+
LastErrorDetails = null;
54+
}
55+
catch (LuaException e)
56+
{
57+
LastError = e.Message;
58+
LastErrorDetails = e;
59+
}
60+
catch (Exception e)
61+
{
62+
LastError = "Something is creating script errors";
63+
LastErrorDetails = e;
64+
}
65+
}
66+
}
67+
4468
public void Update(String EventSetID, Single elapsedTime, Single totalTime)
4569
{
4670
// Event will be ignored if its ID is not referenced in file's eventAmounts table

_Example/DotRPG.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Authors>red-the-random-dev</Authors>
1717
<Product>DotRPG</Product>
1818
<PackageId>DotRPG Example Application</PackageId>
19+
<AssemblyName>dotrpg-example</AssemblyName>
1920
</PropertyGroup>
2021
<ItemGroup>
2122
<Compile Remove=".legacy\**" />

_Example/GameData/DotRPG.Example_data.mgcb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
#begin Sounds/wallcling.wav
7676
/importer:WavImporter
7777
/processor:SoundEffectProcessor
78-
/processorParam:Quality=Best
78+
/processorParam:Quality=Low
7979
/build:Sounds/wallcling.wav
8080

8181
#begin Texture2D/backdrop.png

_Example/GameData/Maps/TestRoom_00.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<Require>
77
<Texture2D id="backdrop" location="Texture2D/backdrop"/>
88
<SoundEffect id="pixelText" location="Sounds/text-scroll"/>
9+
<SoundEffect id="hit" location="Sounds/impact"/>
10+
<SoundEffect id="slide" location="Sounds/wallcling"/>
911
</Require>
1012

1113
<Backdrop local="backdrop" offset="480,270" />

_Example/GameData/Scripts/dialog.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ _manifest = {
99

1010
shake_count = 0
1111

12+
function start()
13+
audio:BufferLocal("slide", "slide")
14+
audio:SetLooped("slide", true)
15+
end
16+
1217
function update(objID, eventID, frameTime, totalTime)
1318
if objID == "default" then
1419
if obj:GetVelocityDerivative("whiterect") < -32 then
1520
camera:Shake(10 * math.abs(obj:GetVelocityDerivative("whiterect")) / obj:GetDistanceToPlayer("whiterect"), 50 * math.abs(obj:GetVelocityDerivative("whiterect")) / obj:GetDistanceToPlayer("whiterect"))
21+
audio:PlayLocal("hit", math.min(0.8, 64 / obj:GetDistanceToPlayer("whiterect")), 1, obj:GetSoundPanning("whiterect"))
22+
end
23+
if obj:GetScalarVelocity("whiterect") > 128 then
24+
audio:SetParameters("slide", math.min(0.8, 64 / obj:GetDistanceToPlayer("whiterect")), 1, obj:GetSoundPanning("whiterect"))
25+
audio:Play("slide")
26+
else
27+
audio:Stop("slide")
1628
end
1729
--error("Something is creating script errors")
1830
end

0 commit comments

Comments
 (0)