Files
OpenRA/OpenRA.Mods.Common/Traits/Voiced.cs
2015-07-12 12:44:30 -05:00

77 lines
2.0 KiB
C#

#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.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor has a voice.")]
public class VoicedInfo : ITraitInfo
{
[FieldLoader.Require]
[Desc("Which voice set to use.")]
[VoiceSetReference] public readonly string VoiceSet = null;
[Desc("Multiply volume with this factor.")]
public readonly float Volume = 1f;
public object Create(ActorInitializer init) { return new Voiced(init.Self, this); }
}
public class Voiced : IVoiced
{
public readonly VoicedInfo Info;
public Voiced(Actor self, VoicedInfo info)
{
Info = info;
}
public string VoiceSet { get { return Info.VoiceSet; } }
public bool PlayVoice(Actor self, string phrase, string variant)
{
if (phrase == null)
return false;
if (string.IsNullOrEmpty(Info.VoiceSet))
return false;
var type = Info.VoiceSet.ToLowerInvariant();
var volume = Info.Volume;
return Sound.PlayPredefined(self.World.Map.Rules, null, self, type, phrase, variant, true, WPos.Zero, volume, true);
}
public bool PlayVoiceLocal(Actor self, string phrase, string variant, float volume)
{
if (phrase == null)
return false;
if (string.IsNullOrEmpty(Info.VoiceSet))
return false;
var type = Info.VoiceSet.ToLowerInvariant();
return Sound.PlayPredefined(self.World.Map.Rules, null, self, type, phrase, variant, false, self.CenterPosition, volume, true);
}
public bool HasVoice(Actor self, string voice)
{
if (string.IsNullOrEmpty(Info.VoiceSet))
return false;
var voices = self.World.Map.Rules.Voices[Info.VoiceSet.ToLowerInvariant()];
return voices != null && voices.Voices.ContainsKey(voice);
}
}
}