-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJukeBox.cs
115 lines (100 loc) · 2.4 KB
/
JukeBox.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2016 Raymond Neilson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using System.Collections;
public class JukeBox : MonoBehaviour {
public AudioClip[] trackList;
public int menuTrack = 0;
public int defaultTrack = 0;
public float maxVolume = 0.5f;
public bool playMenu = true;
private int currentTrack;
private AudioSource audioPlayer;
public int TrackCount {
get { return trackList.Length; }
}
public int CurrentTrack {
get { return currentTrack; }
set {
int newTrack = ((value < 0) || (value > trackList.Length)) ? 0 : value;
currentTrack = newTrack;
}
}
public float Volume {
get {
return audioPlayer.volume / maxVolume;
}
set {
float newVol;
if (value > 1.0f) {
newVol = 1.0f;
}
else if (value < 0.0f) {
newVol = 0.0f;
}
else {
newVol = value;
}
audioPlayer.volume = newVol * maxVolume;
}
}
void Awake () {
audioPlayer = GetComponent<AudioSource>();
if (menuTrack > trackList.Length) {
menuTrack = 0;
}
if (playMenu) {
PlayTrack(menuTrack);
}
currentTrack = defaultTrack;
}
void Update () {
}
void PlayTrack (int trackNum) {
// Stop current
audioPlayer.Stop();
// Set new track
if ((trackNum == 0) || (trackNum > trackList.Length)) {
audioPlayer.clip = null;
}
else {
audioPlayer.clip = trackList[trackNum - 1];
audioPlayer.loop = true;
audioPlayer.Play();
}
}
void PlayTrack () {
PlayTrack(currentTrack);
}
public void SetTrack (int trackNum) {
CurrentTrack = trackNum;
if (GameSettings.Restarted) {
PlayTrack();
}
}
public string GetTrackName (int trackNum) {
if ((trackNum > 0) && (trackNum <= trackList.Length)) {
return trackList[trackNum - 1].name;
}
else {
return "[None]";
}
}
public string GetTrackName () {
return GetTrackName(currentTrack);
}
public void GameStarted () {
PlayTrack(currentTrack);
}
}