-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMomo.pde
450 lines (369 loc) · 11.6 KB
/
Momo.pde
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//================================================================================================================
// Momo
// Author: David Hanna
//
// Top-level game loop of Momo.
//================================================================================================================
// Includes are available project-wide, so they are collected here.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Date;
import java.util.Comparator;
import java.util.Collections;
import java.text.SimpleDateFormat;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.Contact;
import org.jbox2d.collision.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.callbacks.ContactImpulse;
import sprites.utils.*;
import sprites.maths.*;
import sprites.*;
import java.awt.event.WindowListener;
import java.awt.Frame;
// The main class needs to be available globally for some subsystems.
Momo mainObject;
// The Event system.
IEventManager eventManager;
// Physics
Vec2 gravity;
World physicsWorld;
FalldownContactListener contactListener;
int velocityIterations; // Fewer iterations increases performance but accuracy suffers.
int positionIterations; // More iterations decreases performance but improves accuracy.
// Box2D recommends 8 for velocity and 3 for position.
World bonusPhysicsWorld;
// The controller for the current game state (i.e. main menu, in-game, etc)
IGameStateController gameStateController;
// Handles EMG input
IEmgManager emgManager;
// Poll EMG globally once per frame to ensure consistency across all visualizations
HashMap<String, Float> rawReadings;
HashMap<String, Float> processedReadings;
// Manages the save_data.xml file.
IOptions options;
// Top-level game variables.
final String LEFT_DIRECTION_LABEL = "LEFT";
final String RIGHT_DIRECTION_LABEL = "RIGHT";
final String JUMP_DIRECTION_LABEL = "JUMP";
// For performance reasons, we want to make sure that only a single MyoAPI is
// every instantiated. Since the constructor can abort half way through, this
// variable is intended to keep track of when an instance has been initalized
// successfully.
boolean MYO_API_SUCCESSFULLY_INITIALIZED = false;
int lastFrameTime;
int lastFrameWidth;
int lastFrameHeight;
int currentFrameWidth;
int currentFrameHeight;
boolean mouseHand;
PShape bg;
PShape opbg;
PShape wbg;
PShape bbg;
PShape obbg;
Table tableFittsStats;
Table tableInput;
Table tableRawData;
Table tableBonusInput;
int bonusInputCounter;
int totalRowCountInput;
ArrayList<ArrayList<Integer>> platformLevels;
//This is used to keep track of the gap position and width for Fitts Law Mode
ArrayList<PVector> platformGapPosition;
PlatformManagerControllerComponent pc;
FittsStatsComponent fsc;
BonusPlatformManager bpc;
BonusScoreComponent bsc;
//Sensor Applet setup
SensorGraphApplet sa;
SoundManager soundManager;
//For still platform mode
int stillPlatformCounter = 0;
boolean isRising = false;
SoundObject buttonClickedSound;
SoundObject coinCollectedSound;
boolean fittsLawRecorded;
boolean bonusLevel;
boolean isBonusPractice;
SoundObject bonusMusic;
SoundObject bonusSplash;
SoundObject swingSound;
HashMap<String,PImage> allImages;
Table imageSources;
Zone zone;
Actions actions;
// used for manual calibration
CalibrationMode calibrationMode;
int leftSensor = -1;
int rightSensor = -1;
enum Forearm {
LEFT,
RIGHT,
}
enum Zone {
DANGER,
NEUTRAL,
HAPPY,
}
enum Actions{
SWING,
NORMAL,
}
enum CalibrationMode {
AUTO,
MANUAL,
}
void setup()
{
size(500, 500);
surface.setAlwaysOnTop(true);
surface.setResizable(true);
surface.setTitle("The Falling of Momo");
surface.setSize(800,800);
rectMode(CENTER);
ellipseMode(CENTER);
imageMode(CENTER);
shapeMode(CENTER);
mainObject = this;
eventManager = new EventManager();
gravity = new Vec2(0.0, 10.0);
physicsWorld = new World(gravity); // gravity
bonusPhysicsWorld = new World(gravity);
contactListener = new FalldownContactListener();
physicsWorld.setContactListener(contactListener);
bonusPhysicsWorld.setContactListener(contactListener);
velocityIterations = 3; // Our simple games probably don't need as much iteration.
positionIterations = 1;
gameStateController = new GameStateController();
gameStateController.pushState(new GameState_UserLogin());
gameStateController.pushState(new GameState_ArmbandConnectMenu());
emgManager = new NullEmgManager();
// initialize with empty values incase armband is never calibrated
rawReadings = emgManager.pollRaw();
processedReadings = emgManager.poll();
options = new Options();
lastFrameTime = millis();
currentFrameWidth = width;
currentFrameHeight = height;
mouseHand = false;
fittsLawRecorded = false;
bonusLevel = false;
bg = loadShape("images/background/videoBackground.svg");
opbg = loadShape("images/background/opacityVideoBackground.svg");
wbg = loadShape("images/background/whiteBG.svg");
bbg = loadShape("images/background/underWater.svg");
obbg = loadShape("images/background/opacityUnderWater.svg");
soundManager = new SoundManager(this);
buttonClickedSound = soundManager.loadSoundFile("sound_effects/click.mp3");
buttonClickedSound.setPan(0.0);
coinCollectedSound = soundManager.loadSoundFile("sound_effects/coin01.mp3");
coinCollectedSound.setPan(0.0);
bonusMusic = soundManager.loadSoundFile("music/bonusLevel.mp3");
bonusMusic.setPan(0.0);
bonusSplash = soundManager.loadSoundFile("sound_effects/splash2.mp3");
bonusSplash.setPan(0.0);
swingSound = soundManager.loadSoundFile("sound_effects/hammer.mp3");
swingSound.setPan(0.0);
allImages = new HashMap<String,PImage>();
loadAllImages();
actions = Actions.NORMAL;
bonusInputCounter = 0;
}
void draw()
{
bg = loadShape(options.getCustomizeOptions().getBackground());
opbg = loadShape(options.getCustomizeOptions().getOpacityBackground());
int currentFrameTime = millis();
int deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime;
lastFrameWidth = currentFrameWidth;
lastFrameHeight = currentFrameHeight;
currentFrameWidth = width;
currentFrameHeight = height;
if (width < 250)
{
surface.setSize(250, height);
}
if (height < 250)
{
surface.setSize(width, 250);
}
if (currentFrameWidth != lastFrameWidth || currentFrameHeight != lastFrameHeight)
{
return;
}
scale(width / 500.0, height / 500.0);
if (deltaTime > 100)
{
deltaTime = 16;
}
// poll emg-system exactly once per frame for consistency
if (emgManager.isCalibrated()) {
rawReadings = emgManager.pollRaw();
processedReadings = emgManager.poll();
}
eventManager.update();
gameStateController.update(deltaTime);
if (mouseHand)
{
cursor(HAND);
}
else
{
cursor(ARROW);
}
mouseHand = false;
}
void keyPressed()
{
Event event;
if (key == ESC)
{
event = new Event(EventType.ESCAPE_PRESSED);
eventManager.queueEvent(event);
key = 0; // override so that signal is not propogated on to kill window
return;
}
else if (key == CODED)
{
switch (keyCode)
{
case UP:
event = new Event(EventType.UP_BUTTON_PRESSED);
eventManager.queueEvent(event);
return;
case LEFT:
event = new Event(EventType.LEFT_BUTTON_PRESSED);
eventManager.queueEvent(event);
return;
case RIGHT:
event = new Event(EventType.RIGHT_BUTTON_PRESSED);
eventManager.queueEvent(event);
return;
case DOWN:
event = new Event(EventType.DOWN_BUTTON_PRESSED);
eventManager.queueEvent(event);
return;
}
}
else if (key == ' ')
{
event = new Event(EventType.SPACEBAR_PRESSED);
eventManager.queueEvent(event);
return;
}
else if (( key == 't' || key == 'T') &&
(gameStateController.getCurrentState() instanceof GameState_InGame || gameStateController.getCurrentState() instanceof GameState_FittsBonusGame)) {
// TODO the knowledge that this only occurs in certain games states should really
// be pushed into the respective game states.
event = new Event(EventType.TOGGLE_CALIBRATION_DISPLAY);
eventManager.queueEvent(event);
}
else if (( key == 'g' || key == 'G') &&
(gameStateController.getCurrentState() instanceof GameState_InGame || gameStateController.getCurrentState() instanceof GameState_FittsBonusGame)) {
//add second window for Graph
event = new Event(EventType.TOGGLE_GRAPH_DISPLAY);
eventManager.queueEvent(event);
}
}
void keyReleased()
{
Event event;
if (key == ESC)
{
key = 0; // override so that signal is not propogated on to kill window
return;
}
if (key == CODED)
{
switch (keyCode)
{
case UP:
event = new Event(EventType.UP_BUTTON_RELEASED);
eventManager.queueEvent(event);
return;
case LEFT:
event = new Event(EventType.LEFT_BUTTON_RELEASED);
eventManager.queueEvent(event);
return;
case RIGHT:
event = new Event(EventType.RIGHT_BUTTON_RELEASED);
eventManager.queueEvent(event);
return;
}
}
}
void mousePressed()
{
Event event = new Event(EventType.MOUSE_PRESSED);
event.addIntParameter("mouseX", mouseX);
event.addIntParameter("mouseY", mouseY);
eventManager.queueEvent(event);
}
void mouseClicked()
{
Event event = new Event(EventType.MOUSE_CLICKED);
event.addIntParameter("mouseX", mouseX);
event.addIntParameter("mouseY", mouseY);
eventManager.queueEvent(event);
}
void mouseDragged()
{
Event event = new Event(EventType.MOUSE_DRAGGED);
event.addIntParameter("mouseX", mouseX);
event.addIntParameter("mouseY", mouseY);
eventManager.queueEvent(event);
}
void mouseReleased()
{
Event event = new Event(EventType.MOUSE_RELEASED);
event.addIntParameter("mouseX", mouseX);
event.addIntParameter("mouseY", mouseY);
eventManager.queueEvent(event);
}
void mouseMoved()
{
Event event = new Event(EventType.MOUSE_MOVED);
event.addIntParameter("mouseX", mouseX);
event.addIntParameter("mouseY", mouseY);
eventManager.queueEvent(event);
return;
}
class FalldownContactListener implements ContactListener
{
@Override public void beginContact(Contact contact)
{
IGameObject objectA = (IGameObject)contact.getFixtureA().getUserData();
IGameObject objectB = (IGameObject)contact.getFixtureB().getUserData();
RigidBodyComponent rigidBodyA = (RigidBodyComponent)objectA.getComponent(ComponentType.RIGID_BODY);
RigidBodyComponent rigidBodyB = (RigidBodyComponent)objectB.getComponent(ComponentType.RIGID_BODY);
rigidBodyA.onCollisionEnter(objectB);
rigidBodyB.onCollisionEnter(objectA);
}
@Override public void endContact(Contact contact)
{
IGameObject objectA = (IGameObject)contact.getFixtureA().getUserData();
IGameObject objectB = (IGameObject)contact.getFixtureB().getUserData();
RigidBodyComponent rigidBodyA = (RigidBodyComponent)objectA.getComponent(ComponentType.RIGID_BODY);
RigidBodyComponent rigidBodyB = (RigidBodyComponent)objectB.getComponent(ComponentType.RIGID_BODY);
rigidBodyA.onExitEvent(objectB);
rigidBodyB.onExitEvent(objectA);
}
@Override public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override public void postSolve(Contact contact, ContactImpulse impulse)
{
}
}
void loadAllImages(){
imageSources = loadTable("images/imagesSource.csv");
for(int i = 1; i<imageSources.getRowCount();i++){
TableRow row = imageSources.getRow(i);
allImages.put(row.getString(0), loadImage(row.getString(0)));
}
}