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

91 lines
2.6 KiB
C#
Raw 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
{
2019-10-06 17:40:54 +00:00
public float changeSpeed = 1f;
2019-10-07 11:44:26 +00:00
[BeforeStart]
public AudioSource baseLayer;
2019-10-06 17:40:54 +00:00
[BeforeStart]
public AudioSource[] layers;
2019-10-07 11:44:26 +00:00
protected bool neverStarted;
2019-10-06 17:40:54 +00:00
protected float baseLayerTargetVolume;
protected float[] targetVolume;
2019-10-05 13:59:17 +00:00
2019-10-06 17:04:47 +00:00
protected void Start()
{
foreach(AudioSource audioSource in layers) {
2019-10-07 11:44:26 +00:00
audioSource.volume = 0f;
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) {
2019-10-07 11:44:26 +00:00
if(layer != null) {
layer.Play();
}
2019-10-06 17:04:47 +00:00
}
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() {
2019-10-07 11:44:26 +00:00
if(baseLayer != null) {
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);
}
2019-10-06 17:40:54 +00:00
}
for(int i = 0; i < layers.Length; i++) {
2019-10-07 11:44:26 +00:00
if(layers[i] != null) {
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-06 17:40:54 +00:00
}
}
}
2019-10-05 13:59:17 +00:00
}