-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.as
236 lines (189 loc) · 6.78 KB
/
Game.as
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.text.*;
import flash.events.KeyboardEvent;
import flash.events.IOErrorEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
public class Game extends MovieClip {
// Current State
private var currentState: State;
// Resources
public var resourceURLs: Array = [ "assets/images/player.png", "assets/images/ball.png", "assets/images/left_hand.png",
"assets/images/right_hand.png", "assets/images/heart.png", "assets/images/background_image.png",
"assets/images/checkbox_checked.png", "assets/images/checkbox_unchecked.png", "assets/images/point.png",
"assets/images/uparrow.png", "assets/images/downarrow.png" ];
public var soundURLs: Array = [ "assets/sounds/launch.mp3", "assets/sounds/circus.mp3", "assets/sounds/transition.mp3" ];
public var soundChannel: SoundChannel;
public var volumeAdjust: SoundTransform;
// Maps
public var resourceMap: Object;
public var keyMap: Object;
public var mouseDown: Boolean = false;
// Leap Motion
public var leapMap: Object;
public var leapMotion: LeapListener;
public var pointer: Sprite;
// Settings
public var settings: Object;
// Background Image
public var backgroundImage: Sprite;
// Server
public var serverCommunicator: ServerCommunicator;
public function Game() {
// Maps
resourceMap = new Object();
keyMap = new Object();
// Leap Motion
leapMotion = new LeapListener(this);
leapMap = new Object();
// Load Settings
settings = new Object();
loadSettings();
// Events
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, reportKeyUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, reportMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, reportMouseUp);
// Server
serverCommunicator = new ServerCommunicator();
// Sounds
soundChannel = new SoundChannel();
volumeAdjust = new SoundTransform();
// Start game on the menu
currentState = new LoadingState(this);
currentState.setup();
stage.nativeWindow.title = "Leap Juggling";
}
public function reportKeyDown(event: KeyboardEvent): void {
keyMap[event.keyCode] = true;
}
public function reportKeyUp(event: KeyboardEvent): void {
keyMap[event.keyCode] = false;
}
public function reportMouseDown(event: MouseEvent): void {
mouseDown = true;
}
public function reportMouseUp(event: MouseEvent): void {
mouseDown = false;
}
protected function enterFrameHandler(event: Event): void {
currentState.update();
}
private function clearStage() {
while (numChildren > 0) {
removeChildAt(0);
}
}
public function changeState(newState: State) {
clearStage();
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
newState.setup();
currentState = newState;
// Add background image again
this.backgroundImage = new Sprite();
this.backgroundImage.addChild(this.resourceMap["assets/images/background_image.png"]);
this.backgroundImage.width = this.stage.stageWidth;
this.backgroundImage.height = this.stage.stageHeight;
this.addChild(this.backgroundImage);
this.setChildIndex(this.backgroundImage, 0);
if (settings.leapMode && currentState.useLeapPointer) {
pointer = new Sprite();
pointer.addChild(resourceMap["assets/images/point.png"]);
this.addChild(pointer);
}
}
private function stringToBoolean(string: String): Boolean {
if (string == "true") {
return true;
} else {
return false;
}
}
public function loadSettingsFromTextFileAsArray(array: Array) {
for (var i: int = 0; i < array.length; i++) {
if (i == 0) {
settings.leapMode = stringToBoolean(array[i]);
} else if (i == 1) {
settings.volume = Number(array[i]);
}
}
}
public function loadSettings(): void {
var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
/* Called when the settings file fails to load (does not exist) */
function onLoadError(e: Event): void {
var pathToFile: String = File.applicationDirectory.resolvePath("settings.txt").nativePath;
var file: File = new File(pathToFile);
var stream: FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes("false\n0.5\n");
stream.close();
loadSettingsFromTextFileAsArray(["false", "0.5"]);
}
function onLoaded(e: Event): void {
var array: Array = e.target.data.split(/\n/);
loadSettingsFromTextFileAsArray(array);
}
loader.load(new URLRequest("settings.txt"));
}
public function saveSettings(): void {
var pathToFile: String = File.applicationDirectory.resolvePath("settings.txt").nativePath;
var file: File = new File(pathToFile);
var stream: FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(settings.leapMode.toString() + "\n" +
settings.volume.toString() + "\n");
stream.close();
}
public function getRectangleBorder(x: int, y: int, width: int, height: int, thickness: int): Shape {
var rectBorder: Shape = new Shape;
rectBorder.graphics.lineStyle(thickness, 0x000, 1);
rectBorder.graphics.moveTo(x, y);
rectBorder.graphics.lineTo(x + width, y);
rectBorder.graphics.lineTo(x + width, y + height);
rectBorder.graphics.lineTo(x, y + height);
rectBorder.graphics.lineTo(x, y);
return rectBorder;
}
public function updateVolume(): void {
volumeAdjust.volume = settings.volume;
soundChannel.soundTransform = volumeAdjust;
}
public function updateLeapPointer(): void {
if (this.settings.leapMode) {
this.pointer.x = this.leapMotion.hands.rightX + 250;
this.pointer.y = -this.leapMotion.hands.rightY + 500;
}
}
public function checkBounds(object: DisplayObject): Boolean {
if (this.leapMotion.hands.rightX + 250 >= object.x &&
this.leapMotion.hands.rightX + 250 <= object.x + object.width &&
-this.leapMotion.hands.rightY + 500 >= object.y &&
-this.leapMotion.hands.rightY + 500 <= object.y + object.height) {
return true;
} else {
return false;
}
}
}
}