add a looping sound trait

This commit is contained in:
Matthias Mailänder
2015-04-05 11:08:07 +02:00
parent 3f48c649f0
commit 7cbdc07ca3
3 changed files with 42 additions and 6 deletions

View File

@@ -101,7 +101,7 @@ namespace OpenRA
soundEngine.SetListenerPosition(position); soundEngine.SetListenerPosition(position);
} }
static ISound Play(Player player, string name, bool headRelative, WPos pos, float volumeModifier) static ISound Play(Player player, string name, bool headRelative, WPos pos, float volumeModifier = 1f, bool loop = false)
{ {
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
return null; return null;
@@ -109,16 +109,18 @@ namespace OpenRA
return null; return null;
return soundEngine.Play2D(sounds[name], return soundEngine.Play2D(sounds[name],
false, headRelative, pos, loop, headRelative, pos,
InternalSoundVolume * volumeModifier, true); InternalSoundVolume * volumeModifier, true);
} }
public static ISound Play(string name) { return Play(null, name, true, WPos.Zero, 1); } public static ISound Play(string name) { return Play(null, name, true, WPos.Zero, 1f); }
public static ISound Play(string name, WPos pos) { return Play(null, name, false, pos, 1); } public static ISound Play(string name, WPos pos) { return Play(null, name, false, pos, 1f); }
public static ISound Play(string name, float volumeModifier) { return Play(null, name, true, WPos.Zero, volumeModifier); } public static ISound Play(string name, float volumeModifier) { return Play(null, name, true, WPos.Zero, volumeModifier); }
public static ISound Play(string name, WPos pos, float volumeModifier) { return Play(null, name, false, pos, volumeModifier); } public static ISound Play(string name, WPos pos, float volumeModifier) { return Play(null, name, false, pos, volumeModifier); }
public static ISound PlayToPlayer(Player player, string name) { return Play(player, name, true, WPos.Zero, 1); } public static ISound PlayToPlayer(Player player, string name) { return Play(player, name, true, WPos.Zero, 1f); }
public static ISound PlayToPlayer(Player player, string name, WPos pos) { return Play(player, name, false, pos, 1); } public static ISound PlayToPlayer(Player player, string name, WPos pos) { return Play(player, name, false, pos, 1f); }
public static ISound PlayLooped(string name) { return PlayLooped(name, WPos.Zero); }
public static ISound PlayLooped(string name, WPos pos) { return Play(null, name, true, pos, 1f, true); }
public static void PlayVideo(byte[] raw, int channels, int sampleBits, int sampleRate) public static void PlayVideo(byte[] raw, int channels, int sampleBits, int sampleRate)
{ {

View File

@@ -415,6 +415,7 @@
<Compile Include="Traits\ShakeOnDeath.cs" /> <Compile Include="Traits\ShakeOnDeath.cs" />
<Compile Include="Traits\SmokeTrailWhenDamaged.cs" /> <Compile Include="Traits\SmokeTrailWhenDamaged.cs" />
<Compile Include="Traits\Sound\ActorLostNotification.cs" /> <Compile Include="Traits\Sound\ActorLostNotification.cs" />
<Compile Include="Traits\Sound\AmbientSound.cs" />
<Compile Include="Traits\Sound\AnnounceOnBuild.cs" /> <Compile Include="Traits\Sound\AnnounceOnBuild.cs" />
<Compile Include="Traits\Sound\AnnounceOnKill.cs" /> <Compile Include="Traits\Sound\AnnounceOnKill.cs" />
<Compile Include="Traits\Sound\AnnounceOnSeen.cs" /> <Compile Include="Traits\Sound\AnnounceOnSeen.cs" />

View File

@@ -0,0 +1,33 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Plays a looping audio file at the actor position. Attach this to the `World` actor to cover the whole map.")]
class AmbientSoundInfo : ITraitInfo
{
public readonly string SoundFile = null;
public object Create(ActorInitializer init) { return new AmbientSound(init.Self, this); }
}
class AmbientSound
{
public AmbientSound(Actor self, AmbientSoundInfo info)
{
if (self == self.World.WorldActor)
Sound.PlayLooped(info.SoundFile);
else
Sound.PlayLooped(info.SoundFile, self.CenterPosition);
}
}
}