-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
536 lines (453 loc) · 14 KB
/
index.html
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<!doctype html>
<head>
<title>Musk 2 Mars</title>
<meta charset="utf-8">
<meta name="author" content="sidthekid">
<meta property="og:title" content="Musk 2 Mars - Help Elon Musk Reach Mars">
<link rel="icon" type="image/png" href="public/images/favicon.png" sizes="32x32" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="public/js/pixi.min.js"></script>
<script type="text/javascript" src="public/js/bump.js"></script>
<script type="text/javascript" src="public/js/simple-popup.min.js"></script>
<link rel="stylesheet" href="public/css/style.css" type="text/css" />
</head>
<body>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-86474236-1', 'auto');
ga('send', 'pageview');
</script>
<div class="footer">
<p>High Scorer: Elon Musk</p>
</div>
<div id="popup" class="popup-wrapper hide">
<div class="popup-content">
<div class="popup-title">
<button type="button" class="popup-close">×</button>
<h3 id="alertTitle">Musk2Mars</h3>
</div>
<div class="popup-body">
<p id="alertText">Help Elon reach Mars by navigating through the maze of physical and mental obstacles! Press ENTER to begin</p>
</div>
</div>
</div>
<div id="popup-mobile" class="popup-wrapper" style="display:none;z-index:9999;position:fixed;">
<div class="popup-content">
<div class="popup-title">
<h3 id="alertTitle">Mobile/Tablets not supported!</h3>
</div>
<div class="popup-body">
<p id="alertText">Sorry!</p>
</div>
</div>
</div>
<script type="text/javascript">
//////////////////////////////
///// globals
//////////////////////////////
var d = new Date()
var musk, muskHit, space, mars, earth, enemies = [], reason = '', renderer, stage, state, gameStarted, TextureCache, resources, b, timerMonth, timerYear, timerCounter, timerMonthCounter = d.getMonth(), popupVisible = true, SPEED, divisionFactor, numberOfBlobs
if (screen.width < 400)
SPEED = 1, divisionFactor = 2, numberOfBlobs = 30
else if (screen.width >= 400 && screen.width < 768)
SPEED = 2, divisionFactor = 2, numberOfBlobs = 40
else if (screen.width >= 768 && screen.width < 1024)
SPEED = 2.5, divisionFactor = 2, numberOfBlobs = 55
else if (screen.width >= 1024 && screen.width < 1366)
SPEED = 3, divisionFactor = 3, numberOfBlobs = 50
else if (screen.width >= 1024 && screen.width < 1366)
SPEED = 3, divisionFactor = 3, numberOfBlobs = 55
else if (screen.width >= 1366 && screen.width < 1920)
SPEED = 3.5, divisionFactor = 3, numberOfBlobs = 60
else if (screen.width >= 1920)
SPEED = 5, divisionFactor = 5, numberOfBlobs = 100
console.log('speed = ' + SPEED + ' divisionFactor = ' + divisionFactor + ' numberOfBlobs = ' + numberOfBlobs)
// ideal speed should be 3
// ideal divisionFactor should be 4
// ideal noBlobs should be 50
//////////////////////////////
///// setup PIXI
//////////////////////////////
renderer = PIXI.autoDetectRenderer(256, 256)
document.body.appendChild(renderer.view)
stage = new PIXI.Container()
renderer.view.style.position = "absolute"
renderer.view.style.display = "block"
renderer.autoResize = true
renderer.resize(window.innerWidth, window.innerHeight)
TextureCache = PIXI.utils.TextureCache
resources = PIXI.loader.resources
b = new Bump(PIXI)
//////////////////////////////
///// setup assets
//////////////////////////////
PIXI.loader
.add("public/images/images.json")
.add("public/images/sprites.json")
.load(setup);
function setup() {
// background
space = new PIXI.Sprite(TextureCache['space.png'])
space.scale.set(screen.width/space.width, screen.height/space.height)
stage.addChild(space);
// mars
mars = new PIXI.Sprite(TextureCache['mars.svg'])
mars.position.set(screen.width-100, screen.height/randomInt(2,5))
mars.radius = mars.width/2
mars.vy = 1
stage.addChild(mars)
// earth
earth = new PIXI.Sprite(TextureCache['earth.png'])
earth.anchor.set(0.5, 0.5)
earth.scale.set(0.5, 0.5)
earth.position.set(-1*earth.width/4, screen.height/2)
stage.addChild(earth)
// timer
timerMonth = new PIXI.Text(
monthNames[timerMonthCounter],
{fontFamily : 'Arial', fontSize: 100, fill : 0xffffff, align : 'center'}
)
timerMonth.anchor.set(0.5, 0.5)
timerMonth.position.set(screen.width/2.5, screen.height/2 - 100)
timerMonth.alpha = 0.5
stage.addChild(timerMonth)
timerYear = new PIXI.Text(
d.getFullYear(),
{fontFamily : 'Arial', fontSize: 100, fill : 0xffffff, align : 'center'}
)
timerYear.anchor.set(0.5, 0.5)
timerYear.position.set(screen.width/1.5, screen.height/2 - 100)
timerYear.alpha = 0.5
stage.addChild(timerYear)
// musk
musk = new PIXI.Sprite(TextureCache['musk.png'])
musk.anchor.set(0.5, 0.5)
musk.position.set((earth.width/4), screen.height/2)
musk.vx = 0, musk.vy = 0
musk.radius = 16
stage.addChild(musk)
// enemies
initEnemies()
renderer.render(stage)
//////////////////////////////
///// start the game!
//////////////////////////////
timerCounter = 0
gameLoop();
}
//////////////////////////////
///// setup user input
//////////////////////////////
var left = keyboard(37, 65)
var up = keyboard(38, 87)
var right = keyboard(39, 68)
var down = keyboard(40, 83)
var enter = keyboard(13, 13)
left.press = function() {
musk.vx = -1*SPEED
musk.vy = 0
}
left.release = function() {
// if the left arrow has been released, and the right arrow isn't down,
// and the musk isn't moving vertically:
// Stop the musk
if (!right.isDown) {
musk.vx = 0
}
}
up.press = function() {
musk.vy = -1*SPEED
}
up.release = function() {
if (!down.isDown) {
musk.vy = 0
}
}
right.press = function() {
musk.vx = SPEED
}
right.release = function() {
if (!left.isDown) {
musk.vx = 0
}
}
down.press = function() {
musk.vy = SPEED
}
down.release = function() {
if (!up.isDown) {
musk.vy = 0
}
}
enter.press = function() {
popupVisible = false
popup.close()
}
enter.release = function() {
}
var keys = [left, up, right, down, enter]
//////////////////////////////
///// setup game loop
//////////////////////////////
function state() {
if (popupVisible) return
if (!gameStarted) return
// use velocity for moving
musk.x += musk.vx
musk.y += musk.vy
timerCounter += 1
if (timerCounter%divisionFactor == 0) {
timerCounter = 0
timerMonthCounter++
if (timerMonthCounter%11 == 0) {
timerMonthCounter = 0
timerYear.text = (parseInt(timerYear.text) + 1).toString()
}
timerMonth.text = monthNames[timerMonthCounter]
}
// keep musk from going to outer space
var inside = contain(musk, {x: 0, y: 0, width: screen.width, height: screen.height})
if (inside === "top" || inside === "bottom") {
musk.vy += -1*musk.vy
} else if (inside === "left" || inside === "right") {
musk.vx += -1*musk.vx
}
// check if musk reached mars
if (b.hitTestCircle(musk, mars)) {
popupVisible = true
document.getElementById('alertTitle').innerHTML = 'Congratulations!'
document.getElementById('alertText').innerHTML = 'You helped Elon reach Mars by ' + timerMonth.text + ' ' + timerYear.text + '!'
popup.open()
resetGame()
return
}
// check if musk hit an enemy
muskHit = false
enemies.forEach(function(enemy) {
// move along enemy
enemy.enemy.y += enemy.enemy.vy
enemy.enemy.x += enemy.enemy.vx
// randomize enemy speeds
if (Math.random() > 0.4) {
var factor = (Math.random() > 0.5)? 0.15 : 0.25
if (musk.x > screen.width/2 + 50)
factor += 0.13
enemy.enemy.vx += (Math.random() > 0.5)? -1*factor : factor
}
if (enemy.enemy.vx > 10)
enemy.enemy.vx = 1
else if (enemy.enemy.vx < -10)
enemy.enemy.vx = -1
// check the blob's screen boundaries
var blobHitsWall = contain(enemy.enemy, {x: 0, y: 0, width: screen.width, height: screen.height})
if (blobHitsWall === "top" || blobHitsWall === "bottom") {
enemy.enemy.vy *= -1
} else if (blobHitsWall === "right" || blobHitsWall === "left") {
enemy.enemy.vx *= -1
}
if(b.hitTestCircle(musk, enemy.enemy)) {
muskHit = true
reason = enemy.text
}
})
if (muskHit) {
popupVisible = true
document.getElementById('alertTitle').innerHTML = 'Oh no!'
document.getElementById('alertText').innerHTML = reason
popup.open()
resetGame()
return
}
// move mars along
mars.y += mars.vy
if (contain(mars, {x: 0, y: 0, width: screen.width + 1000, height: screen.height})) {
mars.vy *= -1
}
}
function gameLoop() {
// 60 fps
requestAnimationFrame(gameLoop);
state()
renderer.render(stage);
}
function resetGame() {
// reset to starting state
keys.forEach(function(key) {
key.isDown = false
key.isUp = true
})
timerCounter = 0
timerMonthCounter = d.getMonth()
timerMonth.text = monthNames[timerMonthCounter]
timerYear.text = d.getFullYear()
gameStarted = false
muskHit = false
musk.vx = 0
musk.vy = 0
musk.position.set((earth.width/4), screen.height/2)
mars.position.set(screen.width-100, screen.height/randomInt(2,5))
initEnemies()
}
function initEnemies() {
enemies.forEach(function(enemy) {
stage.removeChild(enemy.enemy)
})
enemies = []
var mapping = [
{text: "Hit by a self-driving car.", sprite: 'selfdrivingcar.png'},
{text: "Eliminated by rogue AI.", sprite: 'ai.png'},
{text: "Assassinated by NASA.", sprite: 'nasa.png'},
{text: "Stabbed by Robert Downey Junior for being cooler than him.", sprite: 'ironman.png'},
{text: "Jailed for stabbing Matt Damon because he claimed to be the first Martian.", sprite: 'marrdamon.png'},
{text: "Lost wrestling match against Jeff Bezos.", sprite: 'bezos.png'},
{text: "Regulated by space consortium to only go till Moon.", sprite: 'ula.png'},
{text: "Attacked by a mob for claiming that Harambe was just a Gorilla. #DOFH", sprite: 'harambe.png'}
], index = 0
for (var i = 0 ; i < numberOfBlobs ; i++) {
var spriteName = mapping[index]['sprite']
var enemy = new PIXI.Sprite(TextureCache[spriteName])
var multiplier = 1
if (screen.width <= 400)
multiplier = 0.5
else if (screen.width <= 1366)
multiplier = 1
else if (screen.width <= 1920)
multiplier = 2
else
multiplier = 3
enemy.x = multiplier*100 + randomInt(10*multiplier, 30*multiplier) * i + randomInt(multiplier*5, multiplier*10)
enemy.y = randomInt(0, screen.height - enemy.height)
enemy.vy = 1 * ((Math.random() > 0.5)? -1 : 1)
enemy.vx = 0.3 * ((Math.random() > 0.5)? -1 : 1)
enemy.radius = 16
if (screen.width > 1366)
enemy.radius *= 2
enemies.push({enemy: enemy, text: mapping[index]['text']})
index = (index+1)%mapping.length
stage.addChild(enemy)
}
}
//////////////////////////////
///// utility functions
//////////////////////////////
function contain(sprite, container) {
var collision = undefined;
//Left
if (sprite.x < container.x) {
sprite.x = container.x;
collision = "left";
}
//Top
if (sprite.y < container.y) {
sprite.y = container.y;
collision = "top";
}
//Right
if (sprite.x + sprite.width > container.width) {
sprite.x = container.width - sprite.width;
collision = "right";
}
//Bottom
if (sprite.y + sprite.height > container.height) {
sprite.y = container.height - sprite.height;
collision = "bottom";
}
//Return the collision value
return collision;
}
function keyboard(keyCode, altKey) {
var key = {};
key.code = keyCode;
key.altcode = altKey;
key.isDown = false;
key.isUp = true;
key.press = undefined;
key.release = undefined;
//The `downHandler`
key.downHandler = function(event) {
if (event.keyCode === key.code || event.keyCode === key.altcode) {
if (popupVisible && event.keyCode != 13) return
if (key.isUp && key.press) key.press()
if (event.keyCode == 13) return
if (!gameStarted) gameStarted = true
key.isDown = true;
key.isUp = false;
}
event.preventDefault();
};
//The `upHandler`
key.upHandler = function(event) {
if (event.keyCode === key.code || event.keyCode === key.altcode) {
if (popupVisible && event.keyCode != 13) return
if (key.isDown && key.release) key.release()
if (event.keyCode == 13) return
key.isDown = false;
key.isUp = true;
}
event.preventDefault();
};
//Attach event listeners
window.addEventListener(
"keydown", key.downHandler.bind(key), false
);
window.addEventListener(
"keyup", key.upHandler.bind(key), false
);
return key;
}
function detectmob() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
return true;
}
else {
return false;
}
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
//////////////////////////////
///// setup popup
//////////////////////////////
var popup
if (detectmob()) {
document.getElementById('popup-mobile').style.display = 'block'
document.getElementById('popup').style.display = 'none'
document.getElementsByClassName('footer')[0].style.display = 'none'
document.getElementById('popup-mobile').style.height = screen.height + 'px'
document.getElementById('popup-mobile').style.width = screen.width + 'px'
} else {
popup = new Popup( document.getElementById('popup'), {
width: 600,
height: 150
})
popup.close()
popup.open()
}
</script>
</body>
</html>