Bunch of new sounds by Omniary, some charactersound & soundplayer refactoring

This commit is contained in:
Regalis
2016-12-15 21:42:15 +02:00
parent 3797233839
commit b01b38da68
53 changed files with 315 additions and 101 deletions

View File

@@ -57,7 +57,15 @@ namespace Barotrauma
}
else
{
PlaySound((aiController == null) ? AIController.AiState.None : aiController.State);
switch (aiController.State)
{
case AIController.AiState.Attack:
PlaySound(CharacterSound.SoundType.Attack);
break;
default:
PlaySound(CharacterSound.SoundType.Idle);
break;
}
soundTimer = soundInterval;
}

View File

@@ -121,10 +121,7 @@ namespace Barotrauma
private float bleeding;
private Sound[] sounds;
private float[] soundRange;
//which AIstate each sound is for
private AIController.AiState[] soundStates;
private List<CharacterSound> sounds;
private float attackCoolDown;
@@ -574,28 +571,13 @@ namespace Barotrauma
soundInterval = ToolBox.GetAttributeFloat(doc.Root, "soundinterval", 10.0f);
var soundElements = doc.Root.Elements("sound").ToList();
if (soundElements.Any())
sounds = new List<CharacterSound>();
foreach (XElement soundElement in soundElements)
{
sounds = new Sound[soundElements.Count];
soundStates = new AIController.AiState[soundElements.Count];
soundRange = new float[soundElements.Count];
int i = 0;
foreach (XElement soundElement in soundElements)
{
sounds[i] = Sound.Load(soundElement.Attribute("file").Value);
soundRange[i] = ToolBox.GetAttributeFloat(soundElement, "range", 1000.0f);
if (soundElement.Attribute("state") == null)
{
soundStates[i] = AIController.AiState.None;
}
else
{
soundStates[i] = (AIController.AiState)Enum.Parse(
typeof(AIController.AiState), soundElement.Attribute("state").Value, true);
}
i++;
}
sounds.Add(new CharacterSound(soundElement));
}
if (file == humanConfigFile)
{
@@ -1322,8 +1304,14 @@ namespace Barotrauma
private void UpdateOxygen(float deltaTime)
{
float prevOxygen = oxygen;
Oxygen += deltaTime * (oxygenAvailable < 30.0f ? -5.0f : 10.0f);
if (prevOxygen > 0.0f && Oxygen <= 0.0f && controlled == this)
{
SoundPlayer.PlaySound("drown");
}
PressureProtection -= deltaTime * 100.0f;
float hullAvailableOxygen = 0.0f;
@@ -1449,24 +1437,15 @@ namespace Barotrauma
return progressBar;
}
public void PlaySound(AIController.AiState state)
public void PlaySound(CharacterSound.SoundType soundType)
{
if (sounds == null || !sounds.Any()) return;
var matchingSoundStates = soundStates.Where(x => x == state).ToList();
if (sounds == null || sounds.Count == 0) return;
int selectedSound = Rand.Int(matchingSoundStates.Count);
var matchingSounds = sounds.FindAll(s => s.Type == soundType);
if (matchingSounds.Count == 0) return;
int n = 0;
for (int i = 0; i < sounds.Length; i++)
{
if (soundStates[i] != state) continue;
if (n == selectedSound && sounds[i]!=null)
{
sounds[i].Play(1.0f, soundRange[i], AnimController.Limbs[0].WorldPosition);
return;
}
n++;
}
var selectedSound = matchingSounds[Rand.Int(matchingSounds.Count)];
selectedSound.Sound.Play(1.0f, selectedSound.Range, AnimController.WorldPosition);
}
public virtual void AddDamage(CauseOfDeath causeOfDeath, float amount, IDamageable attacker)
@@ -1567,7 +1546,7 @@ namespace Barotrauma
// limb.Damage = 100.0f;
}
SoundPlayer.PlayDamageSound(DamageSoundType.Implode, 50.0f, AnimController.Collider);
SoundPlayer.PlaySound("implode", 1.0f, 150.0f, WorldPosition);
for (int i = 0; i < 10; i++)
{
@@ -1620,11 +1599,9 @@ namespace Barotrauma
GameServer.Log(Name+" has died (Cause of death: "+causeOfDeath+")", Color.Red);
if (OnDeath != null) OnDeath(this, causeOfDeath);
//CoroutineManager.StartCoroutine(DeathAnim(GameMain.GameScreen.Cam));
//health = 0.0f;
PlaySound(CharacterSound.SoundType.Die);
isDead = true;
this.causeOfDeath = causeOfDeath;
AnimController.movement = Vector2.Zero;
@@ -1635,7 +1612,7 @@ namespace Barotrauma
if (selectedItems[i] != null) selectedItems[i].Drop(this);
}
if (aiTarget!=null)
if (aiTarget != null)
{
aiTarget.Remove();
aiTarget = null;

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Barotrauma
{
class CharacterSound
{
public enum SoundType
{
Idle, Attack, Die
}
public readonly Sound Sound;
public readonly SoundType Type;
public readonly float Range;
public CharacterSound(XElement element)
{
Sound = Sound.Load(element.Attribute("file").Value);
Range = ToolBox.GetAttributeFloat(element, "range", 1000.0f);
Enum.TryParse<SoundType>(ToolBox.GetAttributeString(element, "state", "Idle"), true, out Type);
}
}
}

View File

@@ -293,7 +293,7 @@ namespace Barotrauma.Tutorials
"Content/Characters/Moloch/moloch.xml",
steering.Item.WorldPosition + new Vector2(3000.0f, -500.0f));
moloch.PlaySound(AIController.AiState.Attack);
moloch.PlaySound(CharacterSound.SoundType.Attack);
yield return new WaitForSeconds(1.0f);

View File

@@ -15,8 +15,7 @@ namespace Barotrauma
{
None,
StructureBlunt, StructureSlash,
LimbBlunt, LimbSlash, LimbArmor,
Implode, Pressure
LimbBlunt, LimbSlash, LimbArmor
}
public struct DamageSound
@@ -54,25 +53,30 @@ namespace Barotrauma
static class SoundPlayer
{
public static Sound[] flowSounds = new Sound[3];
public static Sound[] SplashSounds = new Sound[10];
private static ILookup<string, Sound> miscSounds;
//music
public static float MusicVolume = 1.0f;
private const float MusicLerpSpeed = 0.1f;
private static Sound[] waterAmbiences = new Sound[2];
private static int[] waterAmbienceIndexes = new int[2];
private static DamageSound[] damageSounds;
private static BackgroundMusic currentMusic;
private static BackgroundMusic targetMusic;
private static BackgroundMusic[] musicClips;
private static float currMusicVolume;
//ambience
private static Sound[] waterAmbiences = new Sound[2];
private static int[] waterAmbienceIndexes = new int[2];
private static float ambientSoundTimer;
private static Vector2 ambientSoundInterval = new Vector2(20.0f, 40.0f); //x = min, y = max
//misc
public static Sound[] flowSounds = new Sound[3];
public static Sound[] SplashSounds = new Sound[10];
private static List<DamageSound> damageSounds;
private static Sound startDrone;
public static bool Initialized;
@@ -133,39 +137,43 @@ namespace Barotrauma
i++;
}
}
List<KeyValuePair<string, Sound>> miscSoundList = new List<KeyValuePair<string, Sound>>();
damageSounds = new List<DamageSound>();
var xDamageSounds = doc.Root.Elements("damagesound").ToList();
if (xDamageSounds.Any())
foreach (XElement subElement in doc.Root.Elements())
{
damageSounds = new DamageSound[xDamageSounds.Count];
int i = 0;
foreach (XElement element in xDamageSounds)
yield return CoroutineStatus.Running;
switch (subElement.Name.ToString().ToLowerInvariant())
{
yield return CoroutineStatus.Running;
Sound sound = Sound.Load(ToolBox.GetAttributeString(element, "file", ""), false);
if (sound == null) continue;
case "music":
continue;
case "damagesound":
Sound damageSound = Sound.Load(ToolBox.GetAttributeString(subElement, "file", ""), false);
if (damageSound == null) continue;
DamageSoundType damageSoundType = DamageSoundType.None;
DamageSoundType damageSoundType = DamageSoundType.None;
try
{
damageSoundType = (DamageSoundType)Enum.Parse(typeof(DamageSoundType),
ToolBox.GetAttributeString(element, "damagesoundtype", "None"));
}
catch
{
damageSoundType = DamageSoundType.None;
}
Enum.TryParse<DamageSoundType>(ToolBox.GetAttributeString(subElement, "damagesoundtype", "None"), false, out damageSoundType);
damageSounds.Add(new DamageSound(
damageSound, ToolBox.GetAttributeVector2(subElement, "damagerange", new Vector2(0.0f, 100.0f)), damageSoundType));
break;
default:
Sound sound = Sound.Load(ToolBox.GetAttributeString(subElement, "file", ""), false);
if (sound != null)
{
miscSoundList.Add(new KeyValuePair<string, Sound>(subElement.Name.ToString().ToLowerInvariant(), sound));
}
damageSounds[i] = new DamageSound(
sound, ToolBox.GetAttributeVector2(element, "damagerange", new Vector2(0.0f,100.0f)), damageSoundType);
i++;
break;
}
}
miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value);
Initialized = true;
yield return CoroutineStatus.Success;
@@ -177,7 +185,7 @@ namespace Barotrauma
{
UpdateMusic();
if (startDrone!=null && !startDrone.IsPlaying)
if (startDrone != null && !startDrone.IsPlaying)
{
startDrone.Remove();
startDrone = null;
@@ -229,11 +237,20 @@ namespace Barotrauma
movementSoundVolume = Math.Max(movementSoundVolume, movementFactor);
}
//if (Submarine.MainSub != null)
//{
// movementFactor = (Submarine.MainSub.Velocity == Vector2.Zero) ? 0.0f : Submarine.MainSub.Velocity.Length() / 5.0f;
// movementFactor = MathHelper.Clamp(movementFactor, 0.0f, 1.0f);
//}
if (ambientSoundTimer > 0.0f)
{
ambientSoundTimer -= (float)Timing.Step;
}
else
{
PlaySound(
"ambient",
Rand.Range(0.5f, 1.0f),
1000.0f,
new Vector2(Sound.CameraPos.X, Sound.CameraPos.Y) + Rand.Vector(100.0f));
ambientSoundTimer = Rand.Range(ambientSoundInterval.X, ambientSoundInterval.Y);
}
SoundManager.LowPassHFGain = lowpassHFGain;
waterAmbienceIndexes[0] = waterAmbiences[0].Loop(waterAmbienceIndexes[0], ambienceVolume * (1.0f - movementSoundVolume));
@@ -241,6 +258,26 @@ namespace Barotrauma
}
public static Sound GetSound(string soundTag)
{
var matchingSounds = miscSounds[soundTag].ToList();
if (matchingSounds.Count == 0) return null;
return matchingSounds[Rand.Int(matchingSounds.Count)];
}
public static void PlaySound(string soundTag, float volume = 1.0f)
{
var sound = GetSound(soundTag);
if (sound != null) sound.Play(volume);
}
public static void PlaySound(string soundTag, float volume, float range, Vector2 position)
{
var sound = GetSound(soundTag);
if (sound != null) sound.Play(volume, range, position);
}
private static void UpdateMusic()
{
if (musicClips == null) return;
@@ -276,7 +313,7 @@ namespace Barotrauma
}
catch (FileNotFoundException e)
{
DebugConsole.ThrowError("Music clip " + targetMusic.file + " not found!");
DebugConsole.ThrowError("Music clip " + targetMusic.file + " not found!", e);
}
currentMusic = targetMusic;