-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioManager.cs
executable file
·98 lines (87 loc) · 3.24 KB
/
AudioManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
class AudioManager : MonoBehaviour
{
public AudioClip OnConnectionAdded = null;
public AudioClip OnConnectionRemoved = null;
public AudioClip OnGrabAttractor = null;
public AudioClip OnReleaseAttractor = null;
public AudioClip OnObstacleHit = null;
public AudioClip OnObstacleFreed = null;
public AudioClip OnLevelComplete = null;
private AudioSource[] musicLayers;
public void Start()
{
var audioSource = GetComponent<AudioSource>();
// setup music layers
musicLayers = transform.Find("MusicLayers").GetComponentsInChildren<AudioSource>();
for (int i = 0;i < musicLayers.Length;i++)
{
// play all layers, but only the first one unmuted
musicLayers[i].volume = (i == 0) ? 1f : 0f;
musicLayers[i].Play();
}
// setup connection change sfx
var gm = transform.Find("/GameManager")?.GetComponent<GameManager>();
if (gm != null)
{
gm.OnConnectionAdded += () =>
{
Debug.Log("Connection added");
audioSource.PlayOneShot(OnConnectionAdded);
};
gm.OnConnectionRemoved += () =>
{
Debug.Log("Connection removed");
audioSource.PlayOneShot(OnConnectionRemoved);
};
// change music tracks based on connected lines
gm.OnNumConnectedChanged += (numConnected) =>
{
Debug.Log($"Number of Connections changed to {numConnected}");
for (int i = 0; i < musicLayers.Length; i++)
{
musicLayers[i].volume = (i < numConnected) ? 1f : 0f;
};
};
gm.OnObstacleHit += () =>
{
Debug.Log("Obstacle hit");
audioSource.PlayOneShot(OnObstacleHit);
};
gm.OnObstacleFreed += () =>
{
Debug.Log("Obstacle freed");
audioSource.PlayOneShot(OnObstacleFreed);
};
gm.OnLevelComplete += () =>
{
Debug.Log("Level complete");
audioSource.PlayOneShot(OnLevelComplete);
};
}
// setup attractor sfx
var attractorGrabAudioSource = transform.Find("AttractorGrab").GetComponent<AudioSource>();
var im = transform.Find("/GameManager")?.GetComponent<InputManager>();
if (im != null)
{
im.OnGrabAttractor += (GameObject o) =>
{
Debug.Log("Attractor grabbed");
attractorGrabAudioSource.PlayOneShot(OnGrabAttractor);
attractorGrabAudioSource.Play();
};
im.OnReleaseAttractor += (GameObject o) =>
{
Debug.Log("Attractor released");
attractorGrabAudioSource.Stop();
attractorGrabAudioSource.PlayOneShot(OnReleaseAttractor);
};
}
}
}