Add basic background music behavior

This commit is contained in:
Antoine H 2021-02-03 14:18:55 +01:00
parent 3bc7c9c7ee
commit 03f1bbc599
7 changed files with 395 additions and 11 deletions

View file

@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Define the background music behavior.
public class BGMusic : MonoBehaviour
{
public int intensity =0; //Current intensity level
int intensityLevels = 2; //Number of intensity levels (differents tracks)
[SerializeField]
AudioClip AudioIntensity0, AudioIntensity1; //Must have enough clip foreach intensity levels
List<AudioSource> audioSources; //Sources used for each intensity clips
//Change the intensity level of the music
//TODO: Smooth transition
public void changeIntensity(int level)
{
if(level>=intensityLevels)
{
Debug.LogWarning(gameObject.name+" doesn't have such high intensity available :"+ level+ " /"+intensityLevels);
level = intensityLevels-1; //Max level
}
for(int i=0; i<intensityLevels; i++)
{
if(i!=level)
audioSources[i].volume=0.0f;
else
audioSources[i].volume=1.0f;
}
intensity = level;
}
// Start is called before the first frame update
void Start()
{
audioSources = new List<AudioSource>(GetComponents<AudioSource>());
if(audioSources.Count != intensityLevels )
Debug.LogWarning(gameObject.name+" missing audio sources too play all intensity levels : "+ intensityLevels);
//Load clip
audioSources[0].clip = AudioIntensity0;
audioSources[1].clip = AudioIntensity1;
//Set initial intensity
changeIntensity(intensity);
//Start sources
foreach(AudioSource s in audioSources)
s.Play();
}
// Update is called once per frame
void Update()
{
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e845a10d8c5009048af900f3e22daa91
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -30,6 +30,9 @@ public sealed class GameSystem : MonoBehaviour
float slowScale = 0.5f; //Default scale for slow mode
private float fixedDeltaTime;
//Sound
BGMusic BGmusic=null; //Background Music
//TODO : Effect on gold change
//Money
private int gold;
@ -101,6 +104,15 @@ public sealed class GameSystem : MonoBehaviour
{
// Make a copy of the fixedDeltaTime, it defaults to 0.02f, but it can be changed in the editor
this.fixedDeltaTime = Time.fixedDeltaTime;
//Get BG music
GameObject musicObj = GameObject.Find("/GameSystem/AudioManager/BackgroundMusic");
if(musicObj != null)
BGmusic = musicObj.GetComponent<BGMusic>();
if(BGmusic is null)
Debug.LogWarning("No background music found");
//Get UI Service Timer
GameObject timerObj = GameObject.Find("/UI/Canvas/ServiceTimer");
if(timerObj is null)
{
@ -108,11 +120,10 @@ public sealed class GameSystem : MonoBehaviour
UIServiceTimer=null;
}
else
{
UIServiceTimer=timerObj.GetComponent<UITimer>();
}
if(ClientManager.Instance.ready && StockManager.Instance.ready)
//Check that all systems are ready
if(ClientManager.Instance.ready && StockManager.Instance.ready && EventManager.Instance.ready)
{
ready=true;
Debug.Log("All GameSystems are ready");
@ -151,7 +162,10 @@ public sealed class GameSystem : MonoBehaviour
toggleSlowMode(2.0f);
Debug.Log("Time scale: "+Time.timeScale);
}
// Debug.Log("Service timer : "+(int)serviceTimer);
//Basic background music modification
if(ClientManager.Instance.clientList.Count>2 && BGmusic.intensity<1)
BGmusic.changeIntensity(1);
}
// simple Singleton implementation