-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunAR-v12.js
498 lines (422 loc) · 12.7 KB
/
lunAR-v12.js
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// lunAR SXSW '16 version
// 02/2016
// Joao Craveiro
var explosionSound = new Asset("explosionSound");
var meteorSound = new Asset("meteorSound");
var shotSound = new Asset("shotSound");
var shotZone = new Asset("shotZone");
var moon = new Asset ("moon");
var life1 = new Asset ("life1");
var life2 = new Asset ("life2");
var life3 = new Asset ("life3");
var levelupSound = new Asset ("scoreNum1");
var scoreNum2 = new Asset ("scoreNum2");
var gameOver = new Asset ("gameOver");
var levelup = new Asset ("scoreBoard");
var pendingTimeouts = [];
var loadCompleted = false;
var numMeteors = 8;
var loads = numMeteors*2+8;
var meteors = [];
var meteorRotation = 0.2;
var safetyZoneSize = 0.35;
//var moonRadius = 0.115;
var moonRadius = 0.078;
var moonRadiusExtra = 0.024;
var difficulty = 1;
var loader = 1;
var levelTime;
var exploding = false;
var score = 0;
var meteorStartPositionAnchors;
var moonscale = 14;
var meteorscale = 0.7;
var moonspeed = 2;
var spawnDelay = 15;
var spawnRadius = 0.75;
var lives = 3;
var levelingup = false;
var freeze = false;
//linear animations
var animAnchors;
var laStartTime=-1;
var lastTime;
var anim = false;
var animAsset;
for(var i = 0; i < numMeteors; i++)
{
var meteor = {
meteor: new Asset("meteor"+i),
hitBox: new Asset("hitBox"+i),
hit: false,
//exploding: false,
speed: 0,
dx: 0,
dy: 0,
spawning: true
};
meteor.meteor.on('load', loaded);
meteor.hitBox.on('load', loaded);
meteors[i] = meteor;
}
function loaded()
{
if(--loads === 0)
{
aura.showSpinner = false;
meteorStartPositionAnchors = meteors[0].meteor.anchors;
moon.layer = 2;
//scale(moon,moonscale);
//updateMoonRadius();
meteors.forEach(function(meteor)
{
meteor.meteor.layer = 2;
meteor.hitBox.layer = 0;
setStartPosition(meteor);
meteor.hitBox.on('tap', function()
{
shotMeteor(meteor);
});
meteor.hitBox.on('dbltap', function()
{
shotMeteor(meteor);
});
starter(meteor.meteor);
meteor.hitBox.start();
meteor.meteor.on('end', function(){
setStartPosition(meteor);
restarter(meteor.meteor);
});
});
shotZone.layer = 1;
shotZone.start();
gameOver.layer = 0;
move(gameOver,0,0,15);
loadCompleted = true;
starter(moon);
addTimeout('', 20, function(asst){ levelTimeout(); });
}
}
function levelTimeout(){
levelingup = true;
difficulty++;
}
function shotMeteor(meteor)
{
meteor.hit = true;
meteor.meteor.start();
//shotSound.start();
//meteorSound.start();
score += 100*difficulty;
if(levelingup){
levelupSound.start();
levelingup = false;
//levelup.start();
freeze = true;
//scale(moon,1.2);
//updateMoonRadius();
playAll();
if(lives<3) lives++;
addTimeout('', 30+difficulty*difficulty, function(asst){ levelTimeout(); });
addTimeout('', 1, function(asst){
freeze = false;
});
}
//rotate(scoreNum2,'z',-37);
//if(score%10===0)
// rotate(scoreNum1,'z',-37);
//setStartPosition(meteor);
}
function updateMoonRadius(){
//moonRadius = Math.sqrt(moon.anchors[0].x*moon.anchors[0].x+moon.anchors[0].y*moon.anchors[0].y);
moonRadius = Math.abs(moon.anchors[0].z*2) + moonRadiusExtra;
}
life3.on('load', loaded);
life3.load();
levelup.on('load', loaded);
levelup.load();
// Moon
moon.on('load', loaded);
moon.load();
// Sound effects
explosionSound.on('load', loaded);
explosionSound.load();
explosionSound.on('end', function(){explosionSound.stop();});
meteorSound.on('load', loaded);
meteorSound.load();
meteorSound.on('end', function(){meteorSound.stop();});
shotSound.on('load', loaded);
shotSound.load();
shotSound.on('end', function(){shotSound.stop();});
levelupSound.on('load', loaded);
levelupSound.load();
levelupSound.on('end', function(){levelupSound.stop();});
shotZone.on('load', loaded);
shotZone.load();
shotZone.on('tap', function(){
//shotSound.start();
});
shotZone.on('dbltap', function(){
//shotSound.start();
});
// Meteors
meteors.forEach(function(meteor){
meteor.meteor.load();
meteor.hitBox.load();
});
function updateLives()
{
lives--;
/*if(lives === 2)
life3.stop();
if(lives === 1)
life2.stop();*/
if(lives === 0){
moon.start();
meteor.meteor.on('end', function(){
});
playAll();
//life1.stop();
//loadCompleted = false;
gameOver.start();
addTimeout('',2,function(asset){
loadUrl("https://customanalytics.herokuapp.com/register/LunAR%20Scoreboard/score/" + score)
});
}
}
function playAll(){
meteors.forEach(function(meteor){
meteor.meteor.start();
});
}
aura.on('tick', function()
{
if(loadCompleted)
{
// timeouts
var now = hrtime();
pendingTimeouts = pendingTimeouts.filter(function(timeout){
if (timeout.at < now){
timeout.cb(timeout.asset);
}
else{
return true;
}
});
// animations
if(anim)
{
linearBump(animAsset);
}
// game objects
meteors.forEach(function(meteor){
if(!freeze && !meteor.spawning){
if(!meteor.hit){
hitTest(meteor);
if(meteor.hit)
{
meteor.meteor.start();
//explosionSound.start();
updateLives();
triggerLinearBump(moon);
}
//rotate(meteor.meteor,'x',2);
}
//rotate(meteor.meteor,'x',2);
//rotate(moon,'y',moonspeed);
rotate(meteor.hitBox,'x',15);
move(meteor.meteor, meteor.dx, meteor.dy, 0);
move(meteor.hitBox, meteor.dx, meteor.dy, 0);
}
});
}
});
// Collisions
function hitTest(meteor)
{
var anchors = meteor.meteor.anchors;
var meteorMidPoint = {};
var moonMidPoint = {};
meteorMidPoint.x = (anchors[0].x + anchors[1].x) / 2;
meteorMidPoint.y = (anchors[0].y + anchors[2].y) / 2;
var meteorRadius = Math.abs(Math.sqrt(meteorMidPoint.x*meteorMidPoint.x+meteorMidPoint.y*meteorMidPoint.y));
if(meteorRadius<moonRadius)
{
meteor.hit = true;
}
/* reset if too far away
if(anchors[2].x>1.5 || anchors[2].y>1.5 || anchors[2].x<-1.5 || anchors[2].y<-1.5)
{
setStartPosition(meteor);
}*/
}
function setStartPosition(meteor)
{
meteor.meteor.anchors = meteorStartPositionAnchors;
scale(meteor.meteor,meteorscale);
fitSize(meteor.meteor, meteor.hitBox);
scale(meteor.hitBox,4);
move(meteor.hitBox,0,0,0.01)
//rotate(meteor.hitBox,'z',45);
meteor.spawning = true;
addTimeout(meteor, Math.random()*spawnDelay, function(asst){ asst.spawning = false; });
// level conditions
// speed
var rdm = Math.random();
if(rdm<0.1+difficulty/10)
meteor.speed = 0.003+difficulty/10000;
else if(rdm<0.35+difficulty/10)
meteor.speed = 0.0025+difficulty/10000;
else if(rdm<0.85)
meteor.speed = 0.002+difficulty/10000;
else
meteor.speed = 0.001+difficulty/10000;
if(meteor.speed > 0.004) meteor.speed = 0.004;
//size
rdm = Math.random();
if(rdm<0.1+difficulty/10)
scale(meteor.meteor,1-difficulty/20)
else if(rdm<0.5)
scale(meteor.meteor,1+1/difficulty);
// generate along circumference w/ r=2
var posX = Math.random()*2;
while(posX===0) posX = Math.random()*2;
//spawnRadius
var posY = Math.sqrt((1)-((posX-1)*(posX-1)));
if(Math.random()>0.5)
posY*=-1;
posX-=1;
// meteor
var anchors = meteor.meteor.anchors;
anchors[0].x = anchors[0].x + posX;
anchors[1].x = anchors[1].x + posX;
anchors[2].x = anchors[2].x + posX;
anchors[0].y = anchors[0].y + posY;
anchors[1].y = anchors[1].y + posY;
anchors[2].y = anchors[2].y + posY;
meteor.meteor.anchors = anchors;
//rotate(meteor.meteor,1,true);
// hitBox
anchors = meteor.hitBox.anchors;
anchors[0].x = anchors[0].x + posX;
anchors[1].x = anchors[1].x + posX;
anchors[2].x = anchors[2].x + posX;
anchors[0].y = anchors[0].y + posY;
anchors[1].y = anchors[1].y + posY;
anchors[2].y = anchors[2].y + posY;
meteor.hitBox.anchors = anchors;
var angle = Math.atan(posY/posX);
meteor.dx = Math.abs(Math.cos(angle)*meteor.speed);
meteor.dy = Math.abs(Math.sin(angle)*meteor.speed);
if(anchors[2].x > 0) meteor.dx*=-1;
if(anchors[2].y > 0) meteor.dy*=-1;
meteor.hit = false;
}
// animations
function triggerLinearBump(asset)
{
if(!anim)
{
anim = true;
animAsset = moon;
laStartTime = hrtime();
animAnchors = asset.anchors;
}
}
function linearBump(asset)
{
if(hrtime()-laStartTime <= 0.1)
move(asset,0.0025,0,0);
else if(hrtime()-laStartTime <= 0.3)
move(asset,-0.0025,0,0);
else if(hrtime()-laStartTime <= 0.4)
move(asset,0.0025,0,0);
else if(hrtime()-laStartTime >= 0.5)
{
asset.anchors = animAnchors;
laStartTime === -1;
anim = false;
}
}
// AURASMA ASSET FUNCTIONS
// 3D Manipulation
function move(asset,dx,dy,dz)
{
var anchors = asset.anchors;
anchors[0].x = anchors[0].x + dx;
anchors[1].x = anchors[1].x + dx;
anchors[2].x = anchors[2].x + dx;
anchors[0].y = anchors[0].y + dy;
anchors[1].y = anchors[1].y + dy;
anchors[2].y = anchors[2].y + dy;
anchors[0].z = anchors[0].z + dz;
anchors[1].z = anchors[1].z + dz;
anchors[2].z = anchors[2].z + dz;
asset.anchors = anchors;
}
function scale(asset, delta)
{
var anchors = asset.anchors;
var midPoint = {};
midPoint.x = (anchors[0].x + anchors[1].x) / 2;
midPoint.y = (anchors[0].y + anchors[2].y) / 2;
anchors[0].x = midPoint.x+(anchors[0].x-midPoint.x)*delta;
anchors[1].x = midPoint.x+(anchors[1].x-midPoint.x)*delta;
anchors[2].x = midPoint.x+(anchors[2].x-midPoint.x)*delta;
anchors[0].y = midPoint.y+(anchors[0].y-midPoint.y)*delta;
anchors[1].y = midPoint.y+(anchors[1].y-midPoint.y)*delta;
anchors[2].y = midPoint.y+(anchors[2].y-midPoint.y)*delta;
asset.anchors = anchors;
}
function rotate(asset,axis,theta)
{
var anchors = asset.anchors;
var newAnchors = [{},{},{}];
var midPoint = {};
midPoint.x = (anchors[1].x + anchors[2].x) / 2;
midPoint.y = (anchors[1].y + anchors[2].y) / 2;
midPoint.z = (anchors[1].z + anchors[2].z) / 2;
var thetaRad = theta * (Math.PI/180);
var cosTheta = Math.cos(thetaRad);
var sinTheta = Math.sin(thetaRad);
var axis1;
var axis2;
if(axis=='x') { axis1='y'; axis2='z'; }
if(axis=='y') { axis1='x'; axis2='z'; }
if(axis=='z') { axis1='x'; axis2='y'; }
for(var i=0;i<3;i++)
{
newAnchors[i][axis] = anchors[i][axis];
newAnchors[i][axis1] = (anchors[i][axis1] - midPoint[axis1]) * cosTheta - (anchors[i][axis2] - midPoint[axis2]) * sinTheta + midPoint[axis1];
newAnchors[i][axis2] = (anchors[i][axis1] - midPoint[axis1]) * sinTheta + (anchors[i][axis2] - midPoint[axis2]) * cosTheta + midPoint[axis2];
}
asset.anchors = newAnchors;
}
function fitSize(asset1, asset2)
{
var anchors = asset1.anchors;
/*var anchors2 = asset2.anchors;
anchors2[0].x = anchors1[0].x;
anchors2[0].y = anchors1[0].y;
anchors2[1].x = anchors1[1].x;
anchors2[1].y = anchors1[1].y;
anchors2[2].x = anchors1[2].x;
anchors2[2].y = anchors1[2].y;*/
asset2.anchors = anchors;
}
function starter(asset){
asset.start();
addTimeout(asset,0.8,function(a){ a.pause(); })
}
function restarter(asset){
asset.stop();
addTimeout(asset,0.3,function(assetToStart){
assetToStart.start();
});
addTimeout(asset,0.8,function(assetToPause){
assetToPause.pause();
});
}
function addTimeout(asst, seconds, callback){
pendingTimeouts.push({asset: asst, at: hrtime() + seconds, cb: callback });
}