Music implementation, cloud art, and bug fixes
Implemented music Added cloud tiles Made players be able to stand on each others' heads Synced the music and day/night cycle Added a transition in to the gameplay scene Fixed blocking while dying bug
This commit is contained in:
107
Assets/Scripts/Music/MusicManager.cs
Normal file
107
Assets/Scripts/Music/MusicManager.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions.Must;
|
||||
using UnityEngine.Playables;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class MusicManager : MonoBehaviour
|
||||
{
|
||||
public static MusicManager Instance;
|
||||
public List<Playlist> playlists;
|
||||
private Dictionary<string, Playlist> sceneToPlaylist = new Dictionary<string, Playlist>();
|
||||
public GameObject songPrefab;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
|
||||
foreach (Playlist playlist in playlists)
|
||||
{
|
||||
foreach (string scene in playlist.trackScenes)
|
||||
{
|
||||
sceneToPlaylist.Add(scene, playlist);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartPlaylist()
|
||||
{
|
||||
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
|
||||
StopAllCoroutines();
|
||||
foreach (Transform child in transform)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
StartCoroutine(PlayPlaylist(sceneToPlaylist[GetActiveSceneNotTitleScreen()]));
|
||||
}
|
||||
|
||||
public void StartPlaylist(string scene)
|
||||
{
|
||||
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
|
||||
StopAllCoroutines();
|
||||
foreach (Transform child in transform)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
StartCoroutine(PlayPlaylist(sceneToPlaylist[scene]));
|
||||
}
|
||||
|
||||
private IEnumerator PlayPlaylist(Playlist playlist)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
List<AudioClip> randomized = new List<AudioClip>(playlist.songs);
|
||||
for (int i = 0; i < randomized.Count; i++)
|
||||
{
|
||||
AudioClip temp = randomized[i];
|
||||
int randomIndex = Random.Range(i, randomized.Count);
|
||||
randomized[i] = randomized[randomIndex];
|
||||
randomized[randomIndex] = temp;
|
||||
}
|
||||
|
||||
foreach (AudioClip song in randomized)
|
||||
{
|
||||
AudioSource songInstance = Instantiate(songPrefab, transform).GetComponent<AudioSource>();
|
||||
songInstance.clip = song;
|
||||
songInstance.volume = playlist.volume;
|
||||
songInstance.Play();
|
||||
if (playlist.shuffleTime > 0f)
|
||||
{
|
||||
yield return new WaitForSeconds(playlist.shuffleTime);
|
||||
float time = 0f;
|
||||
while (time < 5f)
|
||||
{
|
||||
songInstance.volume = playlist.volume * (1 - time / 5f);
|
||||
time += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return new WaitForSeconds(song.length);
|
||||
}
|
||||
Destroy(songInstance.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetActiveSceneNotTitleScreen()
|
||||
{
|
||||
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
|
||||
{
|
||||
if (SceneManager.GetSceneAt(sceneIndex).name != "Title Screen")
|
||||
{
|
||||
return SceneManager.GetSceneAt(sceneIndex).name;
|
||||
}
|
||||
}
|
||||
return "Title Screen";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user