diff --git a/Barotrauma/BarotraumaClient/BarotraumaClient.csproj b/Barotrauma/BarotraumaClient/BarotraumaClient.csproj index c47ac50f9..b36eb2f25 100644 --- a/Barotrauma/BarotraumaClient/BarotraumaClient.csproj +++ b/Barotrauma/BarotraumaClient/BarotraumaClient.csproj @@ -70,9 +70,9 @@ - - - + + + @@ -151,8 +151,8 @@ - - + + diff --git a/Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreature.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreature.cs similarity index 100% rename from Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreature.cs rename to Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreature.cs diff --git a/Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreatureManager.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreatureManager.cs similarity index 100% rename from Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreatureManager.cs rename to Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreatureManager.cs diff --git a/Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreaturePrefab.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreaturePrefab.cs similarity index 100% rename from Barotrauma/BarotraumaClient/Source/Characters/BackgroundSprite/BackgroundCreaturePrefab.cs rename to Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundCreaturePrefab.cs diff --git a/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpriteManager.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpriteManager.cs similarity index 85% rename from Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpriteManager.cs rename to Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpriteManager.cs index 31c726aeb..bae50b970 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpriteManager.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpriteManager.cs @@ -9,6 +9,7 @@ namespace Barotrauma partial class BackgroundSprite { public ParticleEmitter ParticleEmitter; + public Sound Sound; } partial class BackgroundSpriteManager @@ -21,21 +22,24 @@ namespace Barotrauma { foreach (BackgroundSprite s in visibleSprites) { - if (s.Prefab.ParticleEmitterPrefab != null) + if (s.ParticleEmitter != null) { - Vector2 emitterPos = new Vector2(s.Prefab.EmitterPosition.X, s.Prefab.EmitterPosition.Y) * s.Scale; + Vector2 emitterPos = s.LocalToWorld(new Vector2(s.Prefab.EmitterPosition.X, s.Prefab.EmitterPosition.Y)); + s.ParticleEmitter.Emit(deltaTime, emitterPos); + } - if (s.Rotation != 0.0f || s.Prefab.SwingAmount != 0.0f) + if (s.Sound != null) + { + int sourceIndex; + Vector2 soundPos = s.LocalToWorld(new Vector2(s.Prefab.SoundPosition.X, s.Prefab.SoundPosition.Y)); + if (!Sounds.SoundManager.IsPlaying(s.Sound, out sourceIndex)) { - var ca = (float)Math.Cos(s.Rotation + swingState * s.Prefab.SwingAmount); - var sa = (float)Math.Sin(s.Rotation + swingState * s.Prefab.SwingAmount); - - emitterPos = new Vector2( - ca * emitterPos.X + sa * emitterPos.Y, - -sa * emitterPos.X + ca * emitterPos.Y); + s.Sound.Play(soundPos); + } + else + { + s.Sound.UpdatePosition(soundPos); } - - s.ParticleEmitter.Emit(deltaTime, new Vector2(s.Position.X, s.Position.Y) + emitterPos); } } } diff --git a/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpritePrefab.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpritePrefab.cs similarity index 67% rename from Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpritePrefab.cs rename to Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpritePrefab.cs index 0249978aa..b14c0bac5 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSpritePrefab.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/Levels/BackgroundSprite/BackgroundSpritePrefab.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using System.Xml.Linq; namespace Barotrauma { @@ -6,5 +7,8 @@ namespace Barotrauma { public readonly Particles.ParticleEmitterPrefab ParticleEmitterPrefab; public readonly Vector2 EmitterPosition; + + public readonly XElement SoundElement; + public readonly Vector2 SoundPosition; } } diff --git a/Barotrauma/BarotraumaClient/Source/Sounds/Sound.cs b/Barotrauma/BarotraumaClient/Source/Sounds/Sound.cs index bf6eab0e1..816b294d8 100644 --- a/Barotrauma/BarotraumaClient/Source/Sounds/Sound.cs +++ b/Barotrauma/BarotraumaClient/Source/Sounds/Sound.cs @@ -4,6 +4,7 @@ using Microsoft.Xna.Framework; using System; using System.Collections.Generic; using System.IO; +using System.Xml.Linq; namespace Barotrauma { @@ -17,17 +18,21 @@ namespace Barotrauma private OggSound oggSound; - string filePath; + private readonly string filePath; + private readonly bool destroyOnGameEnd; + + private float baseVolume; + private float range; private int alSourceId; - - private bool destroyOnGameEnd; - - //public float Volume - //{ - // set { SoundManager.Volume(sourceIndex, value); } - //} + public bool IsPlaying + { + get + { + return SoundManager.IsPlaying(alSourceId); + } + } private Sound(string file, bool destroyOnGameEnd) { @@ -52,11 +57,14 @@ namespace Barotrauma ALHelper.Check(); } + baseVolume = 1.0f; + range = 1000.0f; + this.destroyOnGameEnd = destroyOnGameEnd; loadedSounds.Add(this); } - + public string FilePath { get { return filePath; } @@ -83,6 +91,20 @@ namespace Barotrauma return new Sound(file, destroyOnGameEnd); } + public static Sound Load(XElement element, bool destroyOnGameEnd = true) + { + string filePath = ToolBox.GetAttributeString(element, "file", ""); + + var newSound = new Sound(filePath, destroyOnGameEnd); + if (newSound != null) + { + newSound.baseVolume = ToolBox.GetAttributeFloat(element, "volume", 1.0f); + newSound.range = ToolBox.GetAttributeFloat(element, "range", 1000.0f); + } + + return newSound; + } + public int Play(float volume = 1.0f) { if (volume <= 0.0f) return -1; @@ -91,77 +113,53 @@ namespace Barotrauma return alSourceId; } + public int Play(Vector2 position) + { + return Play(baseVolume, range, position); + } + public int Play(float baseVolume, float range, Vector2 position) { - //position = new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y); - - //volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(volume * (range - position.Length())/range, 0.0f, 1.0f); - Vector2 relativePos = GetRelativePosition(position); float volume = GetVolume(relativePos, range, baseVolume); if (volume <= 0.0f) return -1; - alSourceId = SoundManager.Play(this, relativePos/100.0f, volume); + alSourceId = SoundManager.Play(this, relativePos, volume); return alSourceId; - - //if (newIndex == -1) return -1; - - //return UpdatePosition(newIndex, position, range, volume); } - //public int Play(float volume, float range, Body body) - //{ - // //Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position); - // //bodyPosition.Y = -bodyPosition.Y; - - - // alSourceId = Play(volume, range, ConvertUnits.ToDisplayUnits(body.Position)); - - // return alSourceId; - //} + public void UpdatePosition(Vector2 position) + { + int sourceIndex = -1; + if (SoundManager.IsPlaying(this, out sourceIndex)) + { + Vector2 relativePos = GetRelativePosition(position); + float volume = GetVolume(relativePos, range, baseVolume); + if (volume <= 0.0f) + { + SoundManager.Stop(this); + return; + } + + SoundManager.UpdateSoundPosition(sourceIndex, relativePos, volume); + } + } + private float GetVolume(Vector2 relativePosition, float range, float baseVolume) { - float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - relativePosition.Length()) / range, 0.0f, 1.0f); + float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - (relativePosition.Length() * 100.0f)) / range, 0.0f, 1.0f); return volume; } private Vector2 GetRelativePosition(Vector2 position) { - return new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y); + return new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y) / 100.0f; } - //public static int UpdatePosition(int sourceIndex, Vector2 position, float range, float baseVolume = 1.0f) - //{ - // position = new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y); - // float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - position.Length())/range, 0.0f, 1.0f); - - // if (volume <= 0.0f) - // { - // if (sourceIndex > 0) - // { - // SoundManager.Stop(sourceIndex); - // sourceIndex = -1; - // } - - // return sourceIndex; - // } - - // SoundManager.UpdateSoundPosition(sourceIndex, position, volume, volume); - - // return sourceIndex; - //} - - //public int UpdatePosition(int sourceIndex, Body body, float range, float baseVolume = 1.0f) - //{ - // Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position); - // bodyPosition.Y = -bodyPosition.Y; - // return UpdatePosition(sourceIndex, bodyPosition, range, baseVolume); - //} - public int Loop(int sourceIndex, float volume) { if (volume <= 0.0f) @@ -196,18 +194,10 @@ namespace Barotrauma return sourceIndex; } - alSourceId = SoundManager.Loop(this, sourceIndex, relativePos / 100.0f, volume); + alSourceId = SoundManager.Loop(this, sourceIndex, relativePos, volume); return alSourceId; } - public bool IsPlaying - { - get - { - return SoundManager.IsPlaying(alSourceId); - } - } - public static void OnGameEnd() { List removableSounds = loadedSounds.FindAll(s => s.destroyOnGameEnd); diff --git a/Barotrauma/BarotraumaClient/Source/Sounds/SoundManager.cs b/Barotrauma/BarotraumaClient/Source/Sounds/SoundManager.cs index 2f6185b66..301583f3c 100644 --- a/Barotrauma/BarotraumaClient/Source/Sounds/SoundManager.cs +++ b/Barotrauma/BarotraumaClient/Source/Sounds/SoundManager.cs @@ -184,7 +184,7 @@ namespace Barotrauma.Sounds if (Disabled) return; if (sourceIndex < 1) return; - + var state = AL.GetSourceState(alSources[sourceIndex]); if (state == ALSourceState.Playing || state == ALSourceState.Paused) { @@ -195,6 +195,20 @@ namespace Barotrauma.Sounds } } + public static void Stop(Sound sound) + { + if (Disabled) return; + + for (int i = 0; i < soundsPlaying.Length; i++) + { + if (soundsPlaying[i] == sound) + { + Stop(i); + } + } + } + + public static Sound GetPlayingSound(int sourceIndex) { if (Disabled) return null; @@ -215,6 +229,29 @@ namespace Barotrauma.Sounds return AL.GetSourceState(alSources[sourceIndex]) == ALSourceState.Playing; } + public static bool IsPlaying(Sound sound) + { + int temp; + return IsPlaying(sound, out temp); + } + + public static bool IsPlaying(Sound sound, out int sourceIndex) + { + sourceIndex = -1; + if (Disabled) return false; + + for (int i = 0; i < soundsPlaying.Length; i++) + { + if (soundsPlaying[i] == sound && AL.GetSourceState(alSources[i]) == ALSourceState.Playing) + { + sourceIndex = i; + return true; + } + } + + return false; + } + public static bool IsPaused(int sourceIndex) { if (Disabled) return false; diff --git a/Barotrauma/BarotraumaShared/BarotraumaShared.projitems b/Barotrauma/BarotraumaShared/BarotraumaShared.projitems index 9fa382616..3681a9335 100644 --- a/Barotrauma/BarotraumaShared/BarotraumaShared.projitems +++ b/Barotrauma/BarotraumaShared/BarotraumaShared.projitems @@ -1228,6 +1228,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/Barotrauma/BarotraumaShared/Content/Items/Artifacts/artifacts.xml b/Barotrauma/BarotraumaShared/Content/Items/Artifacts/artifacts.xml index a481216b2..65e9ee3dd 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Artifacts/artifacts.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Artifacts/artifacts.xml @@ -103,7 +103,8 @@ - + + @@ -168,7 +169,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Diving/divinggear.xml b/Barotrauma/BarotraumaShared/Content/Items/Diving/divinggear.xml index be3e97cd8..8eecb1d41 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Diving/divinggear.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Diving/divinggear.xml @@ -16,7 +16,8 @@ - + + @@ -133,7 +134,6 @@ - diff --git a/Barotrauma/BarotraumaShared/Content/Items/Fabricators/materials.xml b/Barotrauma/BarotraumaShared/Content/Items/Fabricators/materials.xml index 489349d65..633d4b186 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Fabricators/materials.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Fabricators/materials.xml @@ -86,7 +86,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Medical/medical.xml b/Barotrauma/BarotraumaShared/Content/Items/Medical/medical.xml index bc86ed1ee..6da2cc456 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Medical/medical.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Medical/medical.xml @@ -16,7 +16,8 @@ - + + @@ -42,8 +43,10 @@ - - + + + + @@ -275,7 +278,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Reactor/reactor.xml b/Barotrauma/BarotraumaShared/Content/Items/Reactor/reactor.xml index 80fbf39b4..5ac35b6f1 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Reactor/reactor.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Reactor/reactor.xml @@ -27,7 +27,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Tools/tools.xml b/Barotrauma/BarotraumaShared/Content/Items/Tools/tools.xml index 674134f40..0d6c81251 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Tools/tools.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Tools/tools.xml @@ -117,7 +117,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Weapons/depthcharge.xml b/Barotrauma/BarotraumaShared/Content/Items/Weapons/depthcharge.xml index 58663800c..8a6955b6d 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Weapons/depthcharge.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Weapons/depthcharge.xml @@ -81,7 +81,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Weapons/explosives.xml b/Barotrauma/BarotraumaShared/Content/Items/Weapons/explosives.xml index 83ccd80fd..5c04a0589 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Weapons/explosives.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Weapons/explosives.xml @@ -12,7 +12,8 @@ - + + @@ -30,7 +31,8 @@ - + + @@ -49,7 +51,8 @@ - + + @@ -68,7 +71,8 @@ - + + @@ -120,7 +124,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Weapons/railgun.xml b/Barotrauma/BarotraumaShared/Content/Items/Weapons/railgun.xml index 1379bf89d..27abb571a 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Weapons/railgun.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Weapons/railgun.xml @@ -12,7 +12,8 @@ - + + @@ -117,7 +118,8 @@ - + + diff --git a/Barotrauma/BarotraumaShared/Content/Items/Weapons/weapons.xml b/Barotrauma/BarotraumaShared/Content/Items/Weapons/weapons.xml index 9b78208f0..98b5f1c6c 100644 --- a/Barotrauma/BarotraumaShared/Content/Items/Weapons/weapons.xml +++ b/Barotrauma/BarotraumaShared/Content/Items/Weapons/weapons.xml @@ -66,7 +66,8 @@ - + + @@ -83,7 +84,8 @@ - + + @@ -110,11 +112,13 @@ - + + - + + diff --git a/Barotrauma/BarotraumaShared/Content/Sounds/Water/BlackSmoker.ogg b/Barotrauma/BarotraumaShared/Content/Sounds/Water/BlackSmoker.ogg new file mode 100644 index 000000000..817dd7e29 Binary files /dev/null and b/Barotrauma/BarotraumaShared/Content/Sounds/Water/BlackSmoker.ogg differ diff --git a/Barotrauma/BarotraumaShared/Source/Characters/StatusEffect.cs b/Barotrauma/BarotraumaShared/Source/Characters/StatusEffect.cs index 10c73a8e0..117c11f29 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/StatusEffect.cs @@ -26,6 +26,7 @@ namespace Barotrauma private List particleEmitters; private Sound sound; + private bool loopSound; #endif public string[] propertyNames; @@ -132,11 +133,10 @@ namespace Barotrauma case "duration": duration = ToolBox.GetAttributeFloat(attribute, 0.0f); break; -#if CLIENT case "sound": - sound = Sound.Load(attribute.Value.ToString()); + DebugConsole.ThrowError("Error in StatusEffect " + element.Parent.Name.ToString() + + " - sounds should be defined as child elements of the StatusEffect, not as attributes."); break; -#endif default: propertyAttributes.Add(attribute); break; @@ -181,6 +181,10 @@ namespace Barotrauma case "particleemitter": particleEmitters.Add(new ParticleEmitter(subElement)); break; + case "sound": + sound = Sound.Load(subElement); + loopSound = ToolBox.GetAttributeBool(subElement, "loop", false); + break; #endif } } @@ -227,7 +231,24 @@ namespace Barotrauma protected void Apply(float deltaTime, Entity entity, List targets) { #if CLIENT - if (sound != null) sound.Play(1.0f, 1000.0f, entity.WorldPosition); + if (sound != null) + { + if (loopSound) + { + if (!Sounds.SoundManager.IsPlaying(sound)) + { + sound.Play(entity.WorldPosition); + } + else + { + sound.UpdatePosition(entity.WorldPosition); + } + } + else + { + sound.Play(entity.WorldPosition); + } + } #endif if (useItem) diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpriteManager.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpriteManager.cs index ce56ba5da..cb709f188 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpriteManager.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpriteManager.cs @@ -53,8 +53,30 @@ namespace Barotrauma { this.ParticleEmitter = new ParticleEmitter(prefab.ParticleEmitterPrefab); } -#endif + if (prefab.SoundElement != null) + { + Sound = Sound.Load(prefab.SoundElement, true); + } +#endif + } + + public Vector2 LocalToWorld(Vector2 localPosition, float swingState = 0.0f) + { + Vector2 emitterPos = localPosition * Scale; + + if (Rotation != 0.0f || Prefab.SwingAmount != 0.0f) + { + float rot = Rotation + swingState * Prefab.SwingAmount; + + var ca = (float)Math.Cos(rot); + var sa = (float)Math.Sin(rot); + + emitterPos = new Vector2( + ca * emitterPos.X + sa * emitterPos.Y, + -sa * emitterPos.X + ca * emitterPos.Y); + } + return new Vector2(Position.X, Position.Y) + emitterPos; } } diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpritePrefab.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpritePrefab.cs index 05456d28e..620dfb804 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpritePrefab.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/BackgroundSpritePrefab.cs @@ -97,6 +97,10 @@ namespace Barotrauma ParticleEmitterPrefab = new Particles.ParticleEmitterPrefab(subElement); EmitterPosition = ToolBox.GetAttributeVector2(subElement, "position", Vector2.Zero); break; + case "sound": + SoundElement = subElement; + SoundPosition = ToolBox.GetAttributeVector2(subElement, "position", Vector2.Zero); + break; #endif } }