Moved Voice-related extensions to VoiceExts.

Note: This is a work-around until Selectable can be moved to Mods.Common, which is when the voice extensions should be moved back to ActorExts.

Pulled phrase check before foreach in PlayVoice ActorExts.
Removed superflous actor parameter from PlayVoice/PlayVoiceLocal.

Simplified PlayVoice extensions.

variant is no longer customisable, as all current usages use self.Owner.Country.Race anyway.
This commit is contained in:
reaperrr
2015-05-21 15:48:30 +02:00
parent b9e8406aeb
commit 3777a8bca9
8 changed files with 80 additions and 58 deletions

View File

@@ -179,6 +179,7 @@
<Compile Include="Traits\World\Shroud.cs" /> <Compile Include="Traits\World\Shroud.cs" />
<Compile Include="World.cs" /> <Compile Include="World.cs" />
<Compile Include="WorldUtils.cs" /> <Compile Include="WorldUtils.cs" />
<Compile Include="VoiceExts.cs" />
<Compile Include="Network\ReplayRecorderConnection.cs" /> <Compile Include="Network\ReplayRecorderConnection.cs" />
<Compile Include="Traits\DebugPauseState.cs" /> <Compile Include="Traits\DebugPauseState.cs" />
<Compile Include="Network\UPnP.cs" /> <Compile Include="Network\UPnP.cs" />

View File

@@ -59,8 +59,7 @@ namespace OpenRA
var voicedActor = actors.FirstOrDefault(a => a.Owner == world.LocalPlayer && a.IsInWorld && a.HasTrait<IVoiced>()); var voicedActor = actors.FirstOrDefault(a => a.Owner == world.LocalPlayer && a.IsInWorld && a.HasTrait<IVoiced>());
if (voicedActor != null) if (voicedActor != null)
foreach (var voice in voicedActor.TraitsImplementing<IVoiced>()) voicedActor.PlayVoice("Select");
voice.PlayVoice(voicedActor, "Select", voicedActor.Owner.Country.Race);
foreach (var a in newSelection) foreach (var a in newSelection)
foreach (var sel in a.TraitsImplementing<INotifySelected>()) foreach (var sel in a.TraitsImplementing<INotifySelected>())

75
OpenRA.Game/VoiceExts.cs Normal file
View File

@@ -0,0 +1,75 @@
#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 System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA
{
public static class VoiceExts
{
public static void PlayVoice(this Actor self, string phrase)
{
if (phrase == null)
return;
foreach (var voiced in self.TraitsImplementing<IVoiced>())
{
if (string.IsNullOrEmpty(voiced.VoiceSet))
return;
voiced.PlayVoice(self, phrase, self.Owner.Country.Race);
}
}
public static void PlayVoiceLocal(this Actor self, string phrase, float volume)
{
if (phrase == null)
return;
foreach (var voiced in self.TraitsImplementing<IVoiced>())
{
if (string.IsNullOrEmpty(voiced.VoiceSet))
return;
voiced.PlayVoiceLocal(self, phrase, self.Owner.Country.Race, volume);
}
}
public static bool HasVoice(this Actor self, string voice)
{
return self.TraitsImplementing<IVoiced>().Any(x => x.HasVoice(self, voice));
}
public static void PlayVoiceForOrders(this World w, Order[] orders)
{
// Find an actor with a phrase to say
foreach (var o in orders)
{
if (o == null)
continue;
var orderSubject = o.Subject;
if (orderSubject.Destroyed)
continue;
foreach (var voice in orderSubject.TraitsImplementing<IVoiced>())
foreach (var v in orderSubject.TraitsImplementing<IOrderVoice>())
{
if (voice.PlayVoice(orderSubject, v.VoicePhraseForOrder(orderSubject, o),
orderSubject.Owner.Country.Race))
return;
}
}
}
}
}

View File

@@ -42,26 +42,6 @@ namespace OpenRA
} }
} }
public static void PlayVoiceForOrders(this World w, Order[] orders)
{
// Find an actor with a phrase to say
foreach (var o in orders)
{
if (o == null)
continue;
var orderSubject = o.Subject;
if (orderSubject.Destroyed)
continue;
foreach (var voice in orderSubject.TraitsImplementing<IVoiced>())
foreach (var v in orderSubject.TraitsImplementing<IOrderVoice>())
if (voice.PlayVoice(orderSubject, v.VoicePhraseForOrder(orderSubject, o),
orderSubject.Owner.Country.Race))
return;
}
}
public static void DoTimed<T>(this IEnumerable<T> e, Action<T> a, string text) public static void DoTimed<T>(this IEnumerable<T> e, Action<T> a, string text)
{ {
// Note - manual enumeration here for performance due to high call volume. // Note - manual enumeration here for performance due to high call volume.

View File

@@ -80,39 +80,6 @@ namespace OpenRA.Mods.Common
return Target.Invalid; return Target.Invalid;
} }
public static void PlayVoice(this Actor self, Actor actor, string phrase, string variant)
{
foreach (var voiced in self.TraitsImplementing<IVoiced>())
{
if (phrase == null)
return;
if (string.IsNullOrEmpty(voiced.VoiceSet))
return;
voiced.PlayVoice(self, phrase, variant);
}
}
public static void PlayVoiceLocal(this Actor self, Actor actor, string phrase, string variant, float volume)
{
foreach (var voiced in self.TraitsImplementing<IVoiced>())
{
if (phrase == null)
return;
if (string.IsNullOrEmpty(voiced.VoiceSet))
return;
voiced.PlayVoiceLocal(self, phrase, variant, volume);
}
}
public static bool HasVoice(this Actor self, string voice)
{
return self.TraitsImplementing<IVoiced>().Any(x => x.HasVoice(self, voice));
}
public static void NotifyBlocker(this Actor self, IEnumerable<Actor> blockers) public static void NotifyBlocker(this Actor self, IEnumerable<Actor> blockers)
{ {
foreach (var blocker in blockers) foreach (var blocker in blockers)

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
public void BuildingComplete(Actor self) public void BuildingComplete(Actor self)
{ {
self.PlayVoice(self, "Build", self.Owner.Country.Race); self.PlayVoice("Build");
} }
} }
} }

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Traits
if (e.DamageState == DamageState.Dead && damaged != e.Attacker) if (e.DamageState == DamageState.Dead && damaged != e.Attacker)
{ {
if (self.World.WorldTick - lastAnnounce > info.Interval * 25) if (self.World.WorldTick - lastAnnounce > info.Interval * 25)
self.PlayVoice(self, "Kill", self.Owner.Country.Race); self.PlayVoice("Kill");
lastAnnounce = self.World.WorldTick; lastAnnounce = self.World.WorldTick;
} }

View File

@@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.DeathTypes.Contains(e.Warhead.DeathType) || (!info.DeathTypes.Any() && if (info.DeathTypes.Contains(e.Warhead.DeathType) || (!info.DeathTypes.Any() &&
!self.Info.Traits.WithInterface<DeathSoundsInfo>().Any(dsi => dsi.DeathTypes.Contains(e.Warhead.DeathType)))) !self.Info.Traits.WithInterface<DeathSoundsInfo>().Any(dsi => dsi.DeathTypes.Contains(e.Warhead.DeathType))))
self.PlayVoiceLocal(self, info.DeathSound, self.Owner.Country.Race, info.VolumeMultiplier); self.PlayVoiceLocal(info.DeathSound, info.VolumeMultiplier);
} }
} }
} }