-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudio.as
More file actions
206 lines (154 loc) · 4.4 KB
/
Audio.as
File metadata and controls
206 lines (154 loc) · 4.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package
{
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.utils.*;
import flash.net.SharedObject;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import net.flashpunk.utils.Key;
import net.flashpunk.*;
import net.flashpunk.tweens.misc.*;
public class Audio
{
[Embed(source="audio/rotate.mp3")]
public static var rotateSfx:Class;
[Embed(source="audio/beat.mp3")]
public static var beatSfx:Class;
[Embed(source="audio/music.mp3")]
public static var musicSfx:Class;
public static var muteOverlay:Button;
public static var rotate:Sound = new rotateSfx;
public static var complete:Sound = new beatSfx;
public static var music:Sfx = new Sfx(musicSfx);
public static var volTween:VarTween = new VarTween;
public static var channels:Object = {};
public static var v:Number = 0;
private static var _mute:Boolean = false;
private static var so:SharedObject;
private static var menuItem:ContextMenuItem;
public static function init (o:InteractiveObject):void
{
// Setup
so = SharedObject.getLocal("audio", "/");
_mute = so.data.mute;
if (! Main.touchscreen && ! Main.expoMode) {
addContextMenu(o);
}
if (o.stage) {
addStageListeners(o.stage);
} else {
o.addEventListener(Event.ADDED_TO_STAGE, stageAdd);
}
FP.tweener.addTween(volTween);
}
public static function play (sound:String):void
{
if (! _mute) {
if (sound == "rotate") {
if (! channels[sound]) {
channels[sound] = Audio[sound].play(Math.random()*1000+500, int.MAX_VALUE, new SoundTransform(0));
}
v = 2;
} else {
Audio[sound].play(0, 2);
}
}
}
public static function startMusic ():void
{
if (_mute) return;
if (! music.playing) music.loop(music.volume);
volTween.tween(music, "volume", 1.0, 30);
}
public static function stopMusic ():void
{
if (Main.isAndroid) {
music.stop();
channels["rotate"].stop();
channels["rotate"] = null;
}
volTween.tween(music, "volume", 0.0, 30);
}
public static function resumeMusic ():void
{
if (_mute /*|| FP.world is Intro*/) return;
if (! music.playing) music.loop(music.volume);
volTween.tween(music, "volume", 1.0, 30);
}
public static function update ():void
{
if (! channels["rotate"]) return;
var transform: SoundTransform = channels["rotate"].soundTransform;
var n:Number = FP.clamp(v, 0, 1);
transform.volume += (n - transform.volume) * 0.25;
channels["rotate"].soundTransform = transform;
v -= 0.04;
}
// Getter and setter for mute property
public static function get mute (): Boolean { return _mute; }
public static function set mute (newValue:Boolean): void
{
if (_mute == newValue) return;
_mute = newValue;
if (menuItem) {
menuItem.caption = _mute ? "Unmute" : "Mute";
}
so.data.mute = _mute;
so.flush();
if (muteOverlay) {
muteOverlay.visible = _mute;
}
if (_mute) {
stopMusic();
} else { //if (! (FP.world is Menu)) {
resumeMusic();
}
}
public static function toggleMute ():void
{
mute = ! _mute;
}
// Implementation details
private static function stageAdd (e:Event):void
{
addStageListeners(e.target.stage);
}
private static function addContextMenu (o:InteractiveObject):void
{
var menu:ContextMenu = o.contextMenu || new ContextMenu;
menu.hideBuiltInItems();
menuItem = new ContextMenuItem(_mute ? "Unmute" : "Mute");
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuListener);
menu.customItems.push(menuItem);
o.contextMenu = menu;
}
private static function addStageListeners (stage:Stage):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyListener);
stage.addEventListener(Event.ACTIVATE, focusGain);
stage.addEventListener(Event.DEACTIVATE, focusLost);
}
private static function keyListener (e:KeyboardEvent):void
{
if (e.keyCode == Key.M) {
mute = ! mute;
}
}
private static function menuListener (e:ContextMenuEvent):void
{
mute = ! mute;
}
private static function focusGain (e:Event):void
{
resumeMusic();
}
private static function focusLost (e:Event):void
{
if (Main.touchscreen || FP.stage.displayState != StageDisplayState.FULL_SCREEN) {
stopMusic();
}
}
}
}