1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-24 17:24:19 +00:00
gravity-fusion/Assets/Components/MusicManager.cs

84 lines
2.3 KiB
C#
Raw Permalink Normal View History

2019-10-05 13:59:17 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-10-06 17:04:47 +00:00
public class MusicManager : MonoBehaviour
{
public AudioSource baseLayer;
2019-10-06 17:40:54 +00:00
public float changeSpeed = 1f;
[BeforeStart]
public AudioSource[] layers;
protected float baseLayerTargetVolume;
protected float[] targetVolume;
2019-10-05 13:59:17 +00:00
2019-10-06 17:04:47 +00:00
protected bool neverStarted;
2019-10-05 13:59:17 +00:00
2019-10-06 17:04:47 +00:00
protected void Start()
{
foreach(AudioSource audioSource in layers) {
audioSource.volume = 0;
2019-10-05 13:59:17 +00:00
}
2019-10-06 17:40:54 +00:00
targetVolume = new float[layers.Length];
for(int i = 0; i < layers.Length; i++) {
targetVolume[i] = 0f;
}
baseLayerTargetVolume = 1f;
2019-10-06 17:04:47 +00:00
neverStarted = true;
2019-10-05 13:59:17 +00:00
}
2019-10-06 17:04:47 +00:00
public void UpdateLayers(int maxTier) {
if(maxTier == -1) {
2019-10-06 17:40:54 +00:00
baseLayerTargetVolume = 1f;
2019-10-06 17:04:47 +00:00
}
else {
2019-10-06 17:40:54 +00:00
baseLayerTargetVolume = 0f;
2019-10-06 17:04:47 +00:00
}
2019-10-05 13:59:17 +00:00
2019-10-06 17:04:47 +00:00
if(neverStarted) {
foreach(AudioSource layer in layers) {
layer.Play();
}
neverStarted = false;
}
2019-10-05 13:59:17 +00:00
2019-10-06 17:40:54 +00:00
if(maxTier >= layers.Length) {
for(int i = 0; i < layers.Length; i++) {
targetVolume[i] = 1f;
2019-10-06 17:04:47 +00:00
}
}
else {
2019-10-06 17:40:54 +00:00
for(int i = 0; i < layers.Length; i++) {
2019-10-06 17:04:47 +00:00
if(maxTier >= i) {
2019-10-06 17:40:54 +00:00
targetVolume[i] = 1f;
2019-10-06 17:04:47 +00:00
}
else {
2019-10-06 17:40:54 +00:00
targetVolume[i] = 0f;
2019-10-06 17:04:47 +00:00
}
}
2019-10-05 13:59:17 +00:00
}
}
2019-10-06 17:40:54 +00:00
protected void Update() {
if(baseLayer.volume > baseLayerTargetVolume) {
baseLayer.volume = Mathf.Clamp(baseLayer.volume - Time.deltaTime * changeSpeed, baseLayerTargetVolume, float.PositiveInfinity);
}
else if(baseLayer.volume < baseLayerTargetVolume) {
baseLayer.volume = Mathf.Clamp(baseLayer.volume + Time.deltaTime * changeSpeed, 0f, baseLayerTargetVolume);
}
for(int i = 0; i < layers.Length; i++) {
if(layers[i].volume > targetVolume[i]) {
layers[i].volume = Mathf.Clamp(layers[i].volume - Time.deltaTime * changeSpeed, targetVolume[i], float.PositiveInfinity);
}
else if(layers[i].volume < targetVolume[i]) {
layers[i].volume = Mathf.Clamp(layers[i].volume + Time.deltaTime * changeSpeed, 0f, targetVolume[i]);
}
}
}
2019-10-05 13:59:17 +00:00
}