diff --git a/Assets/Audio/WaveMusic.mp3 b/Assets/Audio/WaveMusic.mp3 new file mode 100644 index 0000000..fa3cc31 Binary files /dev/null and b/Assets/Audio/WaveMusic.mp3 differ diff --git a/Assets/Audio/WaveMusic.mp3.meta b/Assets/Audio/WaveMusic.mp3.meta new file mode 100644 index 0000000..aab7aab --- /dev/null +++ b/Assets/Audio/WaveMusic.mp3.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: 12a913a054576d14581bfce71dbf4512 +AudioImporter: + externalObjects: {} + serializedVersion: 8 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Island.unity b/Assets/Scenes/Island.unity index a755c85..6a84aa3 100644 --- a/Assets/Scenes/Island.unity +++ b/Assets/Scenes/Island.unity @@ -11343,6 +11343,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: '::' _wallet: {fileID: 1030979928} + _gameManager: {fileID: 1875710079} + _levelBackgroundMusic: {fileID: 1897118789} + _buildBackgroundMusic: {fileID: 0} _grassBlockDestroySound: - {fileID: 1641086313} - {fileID: 1127156137} @@ -11368,6 +11371,7 @@ Transform: - {fileID: 1128613021} - {fileID: 1573731473} - {fileID: 1087951941} + - {fileID: 1897118788} m_Father: {fileID: 481586150} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &1439091071 @@ -14385,6 +14389,135 @@ Transform: m_CorrespondingSourceObject: {fileID: 5368672011475721328, guid: 8dd20c91f0d7a7b419f6bedcd1870cba, type: 3} m_PrefabInstance: {fileID: 1894735328} m_PrefabAsset: {fileID: 0} +--- !u!1 &1897118787 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1897118788} + - component: {fileID: 1897118789} + m_Layer: 0 + m_Name: WaveBgMusic + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1897118788 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1897118787} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1431145624} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!82 &1897118789 +AudioSource: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1897118787} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 0} + m_Resource: {fileID: 8300000, guid: 12a913a054576d14581bfce71dbf4512, type: 3} + m_PlayOnAwake: 0 + m_Volume: 0.703 + m_Pitch: 1 + Loop: 1 + Mute: 0 + Spatialize: 0 + SpatializePostEffects: 0 + Priority: 128 + DopplerLevel: 1 + MinDistance: 1 + MaxDistance: 500 + Pan2D: 0 + rolloffMode: 0 + BypassEffects: 0 + BypassListenerEffects: 0 + BypassReverbZones: 0 + rolloffCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + - serializedVersion: 3 + time: 1 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + panLevelCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + spreadCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + reverbZoneMixCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 --- !u!1001 &1897891005 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/AudioManager.cs b/Assets/Scripts/AudioManager.cs index 3d28799..94b656a 100644 --- a/Assets/Scripts/AudioManager.cs +++ b/Assets/Scripts/AudioManager.cs @@ -3,6 +3,9 @@ using UnityEngine; public class AudioManager : MonoBehaviour { [SerializeField] private Wallet _wallet; + [SerializeField] private GameManager _gameManager; + [SerializeField] private AudioSource _levelBackgroundMusic; + [SerializeField] private AudioSource _buildBackgroundMusic; [SerializeField] private AudioSource[] _grassBlockDestroySound; [SerializeField] private AudioSource[] _moneyChangeSound; @@ -10,17 +13,21 @@ public class AudioManager : MonoBehaviour private int _moneyChangeSoundIndex = 0; private int _money; + private GameManager.GameState _state; private void OnEnable() { _money = _wallet.Money; + _state = _gameManager.CurrentState; _wallet.OnMoneyChanged += OnMoneyChanged; + _gameManager.OnStateChanged += OnStateChanged; } private void OnDisable() { _wallet.OnMoneyChanged -= OnMoneyChanged; + _gameManager.OnStateChanged -= OnStateChanged; } public void PlayBlockDestroySound(BlockScript block) @@ -29,11 +36,7 @@ public class AudioManager : MonoBehaviour { case BlockScript.BlockType.Grass: case BlockScript.BlockType.PathFinish: - if (_grassBlockDestroySound == null) - return; - - _grassBlockDestroySound[_grassSoundIndex].Play(); - _grassSoundIndex = (_grassSoundIndex + 1) % _grassBlockDestroySound.Length; + Play(_grassBlockDestroySound, ref _grassSoundIndex); break; } } @@ -46,7 +49,48 @@ public class AudioManager : MonoBehaviour if (_moneyChangeSound.Length == 0 || oldMoney <= money) return; - _moneyChangeSound[_moneyChangeSoundIndex].Play(); - _moneyChangeSoundIndex = (_moneyChangeSoundIndex + 1) % _moneyChangeSound.Length; + Play(_moneyChangeSound, ref _moneyChangeSoundIndex); + } + + private void OnStateChanged(GameManager.GameState newState) + { + if (newState == _state || + _state == GameManager.GameState.BuildingPath && newState == GameManager.GameState.BuildingTurrets) + return; + + _state = newState; + switch (newState) + { + case GameManager.GameState.Level: + Stop(_buildBackgroundMusic); + Play(_levelBackgroundMusic); + break; + case GameManager.GameState.BuildingPath: + case GameManager.GameState.BuildingTurrets: + Stop(_levelBackgroundMusic); + Play(_buildBackgroundMusic); + break; + } + } + + private static void Play(AudioSource audioSource) + { + if (audioSource != null) + audioSource.Play(); + } + + private static void Play(AudioSource[] audioSources, ref int index) + { + if (audioSources == null || index >= audioSources.Length) + return; + + Play(audioSources[index]); + index = (index + 1) % audioSources.Length; + } + + private static void Stop(AudioSource audioSource) + { + if (audioSource != null && audioSource.isPlaying) + audioSource.Stop(); } }