From 2d1c00546f1d224692c2d3c6ab415f78905991b0 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 23 Aug 2017 14:42:24 +0300 Subject: [PATCH] Reimplemented switching to repair & monster music types (now handled by SoundPlayer) --- .../BarotraumaClient/Source/GameMain.cs | 2 +- .../Source/Sounds/SoundPlayer.cs | 140 +++++++++++++----- .../Source/Characters/AI/EnemyAIController.cs | 10 ++ 3 files changed, 111 insertions(+), 41 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/GameMain.cs b/Barotrauma/BarotraumaClient/Source/GameMain.cs index 872dade03..ed0f3f3b6 100644 --- a/Barotrauma/BarotraumaClient/Source/GameMain.cs +++ b/Barotrauma/BarotraumaClient/Source/GameMain.cs @@ -349,7 +349,7 @@ namespace Barotrauma } else if (hasLoaded) { - SoundPlayer.Update(); + SoundPlayer.Update((float)Timing.Step); if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu(); diff --git a/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs b/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs index b30ba86e0..ef0de733a 100644 --- a/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs +++ b/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs @@ -1,4 +1,5 @@ -using Barotrauma.Sounds; +using Barotrauma.Items.Components; +using Barotrauma.Sounds; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; @@ -59,13 +60,16 @@ namespace Barotrauma //music public static float MusicVolume = 1.0f; - private const float MusicLerpSpeed = 0.1f; + private const float MusicLerpSpeed = 1.0f; + private const float UpdateMusicInterval = 5.0f; private static BackgroundMusic currentMusic; private static BackgroundMusic targetMusic; private static BackgroundMusic[] musicClips; private static float currMusicVolume; + private static float updateMusicTimer; + //ambience private static Sound[] waterAmbiences = new Sound[2]; private static int[] waterAmbienceIndexes = new int[2]; @@ -187,9 +191,9 @@ namespace Barotrauma } - public static void Update() + public static void Update(float deltaTime) { - UpdateMusic(); + UpdateMusic(deltaTime); if (startDrone != null && !startDrone.IsPlaying) { @@ -284,29 +288,34 @@ namespace Barotrauma if (sound != null) sound.Play(volume, range, position); } - private static void UpdateMusic() + private static void UpdateMusic(float deltaTime) { if (musicClips == null) return; - - List suitableMusic = GetSuitableMusicClips(); - if (suitableMusic.Count == 0) + updateMusicTimer -= deltaTime; + if (updateMusicTimer <= 0.0f) { - targetMusic = null; - } - else if (!suitableMusic.Contains(currentMusic)) - { - int index = Rand.Int(suitableMusic.Count); + List suitableMusic = GetSuitableMusicClips(); - if (currentMusic == null || suitableMusic[index].file != currentMusic.file) + if (suitableMusic.Count == 0) { - targetMusic = suitableMusic[index]; + targetMusic = null; + } + else if (!suitableMusic.Contains(currentMusic)) + { + int index = Rand.Int(suitableMusic.Count); + + if (currentMusic == null || suitableMusic[index].file != currentMusic.file) + { + targetMusic = suitableMusic[index]; + } } + updateMusicTimer = UpdateMusicInterval; } if (targetMusic == null || currentMusic == null || targetMusic.file != currentMusic.file) { - currMusicVolume = MathHelper.Lerp(currMusicVolume, 0.0f, MusicLerpSpeed); + currMusicVolume = MathHelper.Lerp(currMusicVolume, 0.0f, MusicLerpSpeed * deltaTime); if (currentMusic != null) Sound.StreamVolume(currMusicVolume); if (currMusicVolume < 0.01f) @@ -327,7 +336,7 @@ namespace Barotrauma } else { - currMusicVolume = MathHelper.Lerp(currMusicVolume, MusicVolume, MusicLerpSpeed); + currMusicVolume = MathHelper.Lerp(currMusicVolume, MusicVolume, MusicLerpSpeed * deltaTime); Sound.StreamVolume(currMusicVolume); } } @@ -344,35 +353,86 @@ namespace Barotrauma private static List GetSuitableMusicClips() { + string musicType = GetCurrentMusicType(); + return musicClips.Where(music => music != null && music.type == musicType).ToList(); + } - Submarine targetSubmarine = null; - if (Character.Controlled != null) - { - targetSubmarine = Character.Controlled.Submarine; - } - - string musicType = "default"; - if (OverrideMusicType != null) - { - musicType = OverrideMusicType; - } - else if (Character.Controlled != null && + private static string GetCurrentMusicType() + { + if (OverrideMusicType != null) return OverrideMusicType; + + if (Character.Controlled != null && Level.Loaded != null && Level.Loaded.Ruins != null && Level.Loaded.Ruins.Any(r => r.Area.Contains(Character.Controlled.WorldPosition))) { - musicType = "ruins"; - } - else if ((targetSubmarine != null && targetSubmarine.AtDamageDepth) || - (Screen.Selected == GameMain.GameScreen && GameMain.GameScreen.Cam.Position.Y < SubmarineBody.DamageDepth)) - { - musicType = "deep"; - } - else if (targetSubmarine != null) - { - //TODO: determine whether to switch to monster or repair musictype + return "ruins"; } - return musicClips.Where(music => music != null && music.type == musicType).ToList(); + Submarine targetSubmarine = Character.Controlled?.Submarine; + + if ((targetSubmarine != null && targetSubmarine.AtDamageDepth) || + (Screen.Selected == GameMain.GameScreen && GameMain.GameScreen.Cam.Position.Y < SubmarineBody.DamageDepth)) + { + return "deep"; + } + + if (targetSubmarine != null) + { + List reactors = new List(); + foreach (Item item in Item.ItemList) + { + if (item.Submarine != targetSubmarine) continue; + var reactor = item.GetComponent(); + if (reactor != null) + { + reactors.Add(reactor); + } + } + + if (reactors.All(r => r.Temperature < 1.0f)) return "repair"; + + float floodedArea = 0.0f; + float totalArea = 0.0f; + foreach (Hull hull in Hull.hullList) + { + if (hull.Submarine != targetSubmarine) continue; + floodedArea += hull.Volume; + totalArea += hull.FullVolume; + } + + if (totalArea > 0.0f && floodedArea / totalArea > 0.25f) return "repair"; + } + + + float enemyDistThreshold = 5000.0f; + + if (targetSubmarine != null) + { + enemyDistThreshold = Math.Max(enemyDistThreshold, Math.Max(targetSubmarine.Borders.Width, targetSubmarine.Borders.Height) * 2.0f); + } + + foreach (Character character in Character.CharacterList) + { + EnemyAIController enemyAI = character.AIController as EnemyAIController; + if (enemyAI == null || (enemyAI.AttackHumans < 0.0f && enemyAI.AttackRooms < 0.0f)) continue; + + if (targetSubmarine != null) + { + if (Vector2.DistanceSquared(character.WorldPosition, targetSubmarine.WorldPosition) < enemyDistThreshold * enemyDistThreshold) + { + return "monster"; + } + } + else if (Character.Controlled != null) + { + if (Vector2.DistanceSquared(character.WorldPosition, Character.Controlled.WorldPosition) < enemyDistThreshold * enemyDistThreshold) + { + return "monster"; + } + } + } + + return "default"; } public static void PlaySplashSound(Vector2 worldPosition, float strength) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/EnemyAIController.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/EnemyAIController.cs index 392a5f96d..2647088a1 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/EnemyAIController.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/EnemyAIController.cs @@ -64,6 +64,16 @@ namespace Barotrauma { get { return selectedAiTarget; } } + + public float AttackHumans + { + get { return attackHumans; } + } + + public float AttackRooms + { + get { return attackRooms; } + } public EnemyAIController(Character c, string file) : base(c) {