Repository navigation
Expand file tree
/
Copy pathGameManager.cs
More file actions
149 lines (130 loc) · 3.29 KB
/
Copy pathGameManager.cs
File metadata and controls
149 lines (130 loc) · 3.29 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using UnityEngine;
using StarterAssets;
public class GameManager : MonoBehaviour
{
public int score;
public bool HasEnded { get; private set; }
private ThirdPersonController player;
private AudioPlaybackHandle windHandle;
private void OnEnable()
{
player = Object.FindAnyObjectByType<ThirdPersonController>();
if (player == null)
{
Debug.LogWarning("GameManager cannot connect movement audio without a ThirdPersonController.", this);
return;
}
player.Footstepped += PlayFootstep;
player.Jumped += PlayJump;
player.Landed += PlayLanding;
}
void Start()
{
if (!HasEnded)
{
windHandle = AudioEvents.PlayControlled(new AudioRequest(AudioID.AmbientWind));
}
}
private void OnDisable()
{
if (player != null)
{
player.Footstepped -= PlayFootstep;
player.Jumped -= PlayJump;
player.Landed -= PlayLanding;
}
AudioEvents.Stop(windHandle, allowFadeout: false);
}
private void PlayFootstep()
{
PlayMovementSound(AudioID.PlayerFootsteps);
}
private void PlayJump()
{
PlayMovementSound(AudioID.PlayerJump);
}
private void PlayLanding()
{
PlayMovementSound(AudioID.PlayerLand);
}
private void PlayMovementSound(AudioID id)
{
if (!HasEnded && player != null)
{
AudioEvents.Play(new AudioRequest(id, player.transform.position));
}
}
public void CollectCoin(Vector3 position, bool isFalseCoin = false)
{
if (HasEnded)
{
return;
}
AudioEvents.Play(new AudioRequest(
isFalseCoin ? AudioID.FalseCoinPickup : AudioID.CoinPickup, position));
if (isFalseCoin)
{
GameOver();
}
else
{
AddScore(1);
}
}
public void AddScore(int amount)
{
if (HasEnded)
{
return;
}
score += amount;
if (AudioManager.Instance == null)
{
Debug.LogWarning("Cannot update music parameter because there is no active AudioManager.", this);
}
else
{
AudioEvents.SetParameter(
AudioManager.Instance.CurrentMusic,
"Volume",
Mathf.Clamp01(score / 4f)
);
}
if (score >=4)
{
Win();
}
}
public void GameOver()
{
if (HasEnded)
{
return;
}
HasEnded = true;
Debug.Log("Game Over!");
AudioEvents.Play(new AudioRequest(AudioID.GameLose));
StopSceneMusic();
}
void Win()
{
if (HasEnded)
{
return;
}
HasEnded = true;
Debug.Log("You win!");
AudioEvents.Play(new AudioRequest(AudioID.GameWin));
StopSceneMusic();
}
private void StopSceneMusic()
{
AudioEvents.Stop(windHandle, allowFadeout: false);
if (AudioManager.Instance == null)
{
Debug.LogWarning("Cannot stop scene music because there is no active AudioManager.", this);
return;
}
AudioManager.Instance.StopMusic();
}
}