Add AudioManager + Evolutive Background music

This commit is contained in:
Antoine H 2021-02-05 13:00:19 +01:00
parent 9f73ff8cb6
commit 5b73956cfa
14 changed files with 380 additions and 38 deletions

Binary file not shown.

View file

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 3274177b211a55c42a0a2ce4a6ae61ba
AudioImporter:
externalObjects: {}
serializedVersion: 6
defaultSettings:
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
preloadAudioData: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View file

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 67990e289ee555c4182dabba061eebcd
AudioImporter:
externalObjects: {}
serializedVersion: 6
defaultSettings:
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
preloadAudioData: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View file

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 271965f1fde0f9f41933e807059de2be
AudioImporter:
externalObjects: {}
serializedVersion: 6
defaultSettings:
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
preloadAudioData: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

View file

@ -61,8 +61,9 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1361501530690105541}
- component: {fileID: 5363602638374141029}
m_Layer: 0
m_Name: Audio
m_Name: AudioManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@ -83,6 +84,20 @@ Transform:
m_Father: {fileID: 2157073180203202247}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &5363602638374141029
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 422892530533236511}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5e5c517d6d8a28a47969320717cb0627, type: 3}
m_Name:
m_EditorClassIdentifier:
ready: 0
BGMusic: {fileID: 6898396145254654029}
--- !u!1 &2157073178696562865
GameObject:
m_ObjectHideFlags: 0
@ -395,6 +410,7 @@ GameObject:
- component: {fileID: 5023819249935670292}
- component: {fileID: 4688376570334067842}
- component: {fileID: 3569788005944565417}
- component: {fileID: 5441382446788087128}
- component: {fileID: 6898396145254654029}
m_Layer: 0
m_Name: BackgroundMusic
@ -514,6 +530,102 @@ AudioSource:
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!82 &3569788005944565417
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6575613771290711696}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0}
m_PlayOnAwake: 1
m_Volume: 1
m_Pitch: 1
Loop: 0
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!82 &5441382446788087128
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@ -621,5 +733,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e845a10d8c5009048af900f3e22daa91, type: 3}
m_Name:
m_EditorClassIdentifier:
AudioIntensity0: {fileID: 0}
AudioIntensity1: {fileID: 8300000, guid: 849452323efae574d8141c82f7b28f55, type: 3}
intensity: 0
AudioIntensity0: {fileID: 8300000, guid: 67990e289ee555c4182dabba061eebcd, type: 3}
AudioIntensity1: {fileID: 8300000, guid: 271965f1fde0f9f41933e807059de2be, type: 3}
AudioIntensity2: {fileID: 8300000, guid: 3274177b211a55c42a0a2ce4a6ae61ba, type: 3}

View file

@ -32664,6 +32664,36 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5363602638374141029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: clientWeight
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5363602638374141029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: HardObsWeight
value: 1.5
objectReference: {fileID: 0}
- target: {fileID: 5363602638374141029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: SoftObsWeight
value: 0.3
objectReference: {fileID: 0}
- target: {fileID: 6898396145254654029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: changeTime
value: 2
objectReference: {fileID: 0}
- target: {fileID: 6898396145254654029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: AudioIntensity0
value:
objectReference: {fileID: 8300000, guid: 67990e289ee555c4182dabba061eebcd, type: 3}
- target: {fileID: 6898396145254654029, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: AudioIntensity1
value:
objectReference: {fileID: 8300000, guid: 271965f1fde0f9f41933e807059de2be, type: 3}
- target: {fileID: 7408334756516261553, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: range

View file

@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public sealed class AudioManager : MonoBehaviour
{
public static string AudioManager_path="/GameSystem/AudioManager";
//Singleton
private static AudioManager _instance=null;
public static AudioManager Instance { get
{
if(_instance is null) //Force Awakening if needed
GameObject.Find(AudioManager_path).GetComponent<AudioManager>().Awake();
return _instance;
}
}
[HideInInspector]
public bool ready = false; //Wether the AudioManager is initialized
[SerializeField]
BGMusic BGMusic = null; //Background music script
[SerializeField]
float clientWeight=1.0f, HardObsWeight=1.0f, SoftObsWeight=1.0f; //Importance of each entity
float maxEntity; //Maximum entities (client + events) weighted by importance
float currentNbEntity; //Current number of entities (Client + events) weighted by importance
//Update audio intensity
void updateIntensity()
{
//Intensity change thresholds
float upThreshold = ((BGMusic.Intensity+1)*maxEntity)/BGMusic.Levels;
float downTreshold = ((BGMusic.Intensity)*maxEntity)/BGMusic.Levels; //add -1 or % to prevent constant change ?
// Debug.Log(gameObject.name+" - treshold : (+)"+upThreshold+"/(-)"+downTreshold);
//Update intensity
if(BGMusic.Intensity<(BGMusic.Levels-1) && currentNbEntity> upThreshold) //Increase Intensity if needed
BGMusic.Intensity++;
else if(BGMusic.Intensity>0 && currentNbEntity< downTreshold) //Decrease Intensity if needed
BGMusic.Intensity--;
}
// Start is called before the first frame update
void Awake()
{
//Singleton
if (_instance != null && _instance != this)
Destroy(this.gameObject);
else
_instance = this;
if(!ready)
{
if(BGMusic is null)
Debug.LogError(gameObject.name+" doesnt have a background music script");
//Maximum entity weighted by importance
maxEntity = clientWeight*ClientManager.Instance.MaxClients+HardObsWeight*EventManager.Instance.MaxEvents("Hard")+SoftObsWeight*EventManager.Instance.MaxEvents("Soft");
ready = true;
}
}
// Update is called once per frame
void Update()
{
//Update current number of entity (weighted)
currentNbEntity=clientWeight*ClientManager.Instance.clientList.Count+HardObsWeight*EventManager.Instance.eventCount("Hard")+SoftObsWeight*EventManager.Instance.eventCount("Soft");
updateIntensity();
}
}

View file

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

View file

@ -5,48 +5,79 @@ 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)
//Intensity levels
[SerializeField]
AudioClip AudioIntensity0, AudioIntensity1; //Must have enough clip foreach intensity levels
int intensity= 0; //Current intensity level
public int Intensity{get{return intensity;} set{changeIntensity(value);}} //Accessors intensity
public static int Levels= 3; //Number of intensity levels (differents tracks)
//Clips
[SerializeField]
AudioClip AudioIntensity0, AudioIntensity1, AudioIntensity2; //Must have enough clip foreach intensity levels
List<AudioSource> audioSources; //Sources used for each intensity clips
List<float> targetVolumes = new List<float>(Levels);
[SerializeField]
float changeTime = 1.0f; //Time to smoothly change intensity level
List<float> changeVelocity = new List<float>(Levels); //Current velocity for smooth change
//Change the intensity level of the music
//TODO: Smooth transition
public void changeIntensity(int level)
//Change the intensity level of the music. If instantChange is false, do it smoothly.
public void changeIntensity(int level, bool instantChange=false)
{
if(level>=intensityLevels)
if(level>=Levels)
{
Debug.LogWarning(gameObject.name+" doesn't have such high intensity available :"+ level+ " /"+intensityLevels);
level = intensityLevels-1; //Max level
Debug.LogWarning(gameObject.name+" doesn't have such high intensity available :"+ level+ " /"+Levels);
level = Levels-1; //Max level
}
else if(level<0)
{
Debug.LogWarning(gameObject.name+" doesn't have such low intensity available :"+ level+ " /"+Levels);
level = 0; //Min level
}
for(int i=0; i<intensityLevels; i++)
for(int i=0; i<Levels; i++)
{
if(i!=level)
audioSources[i].volume=0.0f;
{
targetVolumes[i]=0.0f;
if(instantChange)
audioSources[i].volume=0.0f;
}
else
audioSources[i].volume=1.0f;
{
targetVolumes[i]=1.0f;
if(instantChange)
audioSources[i].volume=1.0f;
}
}
intensity = level;
Debug.Log(gameObject.name+" - Intensity : "+intensity);
}
// 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);
if(audioSources.Count != Levels )
Debug.LogWarning(gameObject.name+" needs "+Levels+" audio sources. Found : "+audioSources.Count);
//Load clip
audioSources[0].clip = AudioIntensity0;
audioSources[1].clip = AudioIntensity1;
audioSources[2].clip = AudioIntensity2;
//Initialize target & velocities
for(int i=0; i<Levels; i++)
{
targetVolumes.Add(0.0f);
changeVelocity.Add(0.0f);
}
//Set initial intensity
changeIntensity(intensity);
changeIntensity(intensity, true);
//Start sources
foreach(AudioSource s in audioSources)
s.Play();
@ -55,6 +86,12 @@ public class BGMusic : MonoBehaviour
// Update is called once per frame
void Update()
{
float currVel;
for(int i=0; i<Levels; i++)
{
currVel=changeVelocity[i];
audioSources[i].volume=Mathf.SmoothDamp(audioSources[i].volume, targetVolumes[i], ref currVel, changeTime);
changeVelocity[i]=currVel;
}
}
}

View file

@ -23,7 +23,8 @@ public sealed class ClientManager : MonoBehaviour
public bool ready = false; //Wether the ClientManager is initialized
[SerializeField]
int nbMaxClients = 1; //Maximum active clients
int nbMaxClients= 1; //Maximum active clients
public int MaxClients{ get{return nbMaxClients;} private set{nbMaxClients=value;}} //Accessor nbMaxClients
[SerializeField]
float clientSpawnChance = 100.0f; //Chance of new client every request
[SerializeField]

View file

@ -19,7 +19,7 @@ public sealed class EventManager : MonoBehaviour
}
[HideInInspector]
public bool ready = false; //Wether the ClientManager is initialized
public bool ready = false; //Wether the EventManager is initialized
[SerializeField]
float SpawnRange = 1.0f; //Range of an event spawn from its origin (real max distance = 2*range)
@ -47,7 +47,30 @@ public sealed class EventManager : MonoBehaviour
float coroutineRefreshRate = 1.0f; //Time (s) before refreshing a coroutine
private Dictionary<int,IEnumerator> coroutines= new Dictionary<int,IEnumerator>(); //Dict of EventManager coroutines associated to each client ID
//Spawn an event near position with a probability of spawnChance%
//Return the maximum number of events ("Soft"; "Hard")
public int MaxEvents(string types="SoftHard")
{
int res=0;
if(types.Contains("Soft"))
res+=maxSoftObs;
if(types.Contains("Hard"))
res+=maxHardObs;
return res;
}
//Return the current number of events ("Soft"; "Hard")
public int eventCount(string types="SoftHard")
{
int res=0;
if(types.Contains("Soft"))
res+=softObsList.Count;
if(types.Contains("Hard"))
res+=hardObsList.Count;
return res;
}
//Spawn a soft obstacle near position with a probability of spawnChance%
public void spawnSoftObs(Vector2 position, float spawnChance = 100.0f)
{
Vector3 spawnPoint;
@ -63,6 +86,7 @@ public sealed class EventManager : MonoBehaviour
// eventSpawnReady=false;
}
}
//Spawn a hard obstacle near position with a probability of spawnChance%, and assign targetClients to it
public void spawnHardObs(List<Client_controller> targetClients, Vector2 position, float spawnChance = 100.0f)
{
//TODO: Orienté client vers event + prefab

View file

@ -30,9 +30,6 @@ 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;
@ -105,13 +102,6 @@ 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)
@ -123,7 +113,7 @@ public sealed class GameSystem : MonoBehaviour
UIServiceTimer=timerObj.GetComponent<UITimer>();
//Check that all systems are ready
if(ClientManager.Instance.ready && StockManager.Instance.ready && EventManager.Instance.ready)
if(ClientManager.Instance.ready && StockManager.Instance.ready && EventManager.Instance.ready && AudioManager.Instance.ready)
{
ready=true;
Debug.Log("All GameSystems are ready");
@ -162,10 +152,6 @@ public sealed class GameSystem : MonoBehaviour
toggleSlowMode(2.0f);
Debug.Log("Time scale: "+Time.timeScale);
}
//Basic background music modification
if(ClientManager.Instance.clientList.Count>2 && BGmusic.intensity<1)
BGmusic.changeIntensity(1);
}
// simple Singleton implementation