forked from AGrosse/Succulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSucculus.html
553 lines (490 loc) · 17.4 KB
/
Succulus.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;background-color: black;}
#panel {
position: absolute;
left: 67%;
top: 5px;
/*margin-left: -180px;*/
z-index: 5;
}
p.score{
font-family: "Lucidia Console", "Monaco", monospace;
position: relative;
color: #c8cb2c ;
font-size: 2.19em;
/*left: 100%;*/
bottom: -1em;
margin:auto;
}
p.time{
font-family: "Lucidia Console", "Monaco", monospace;
position: relative;
color: #c8cb2c ;
font-size: 2.19em;
/*left: 100%;*/
bottom: -2.5em;
margin:auto;
}
p.distance{
font-family:"Lucidia Console", "Monaco", monospace;
position: relative;
color: #c8cb2c ;
font-size: 2.19em;
/*left: 100%;*/
bottom: 100%;
margin:auto;
}
#inst{
position: fixed;
right: 10px;
bottom: 0;
}
p.text{
font-family:"Lucidia Console", "Monaco", monospace;
position: relative;
color: #c8cb2c ;
font-size: 1.59em;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB2gQzDsta9YsfhK4Xtptb70IKZZkL4zKU">
</script>
<script type="text/javascript">
// initialize a bunch of global variables
var directionsService = new google.maps.DirectionsService();
var map;
var opponent;
var vacuum;
// one of these will be displayed as the marker to be caught
var opponents = ["stevie_p", "dln", "jeff", "amy", "andy", "jadrian", "dave", "anna", "sherri", "putin", "obama", "schiller"];
// pick one randomly
var cur_opponent_index = Math.floor(Math.random() * opponents.length);
// used in movement functions
// this is based on a starting zoom level of 18
var vacuum_movement = 0.00002;
var opponent_movement = vacuum_movement*50;
// score
var catch_counter = 0;
// opponent has a small window of immunity after being caught
var immune = false;
// used in timedMode
var timeCounter = 120;
// get user input
// returns true if they clicked OK for timed mode
// if false ("Cancel"), assume zen mode (no timer)
var timedMode = confirm("Control the vacuum and chase the presidents and CS professors. Click OK for timed mode and Cancel for zen mode. Gotta catch 'em all!");
// options for displaying the route
var line_options = {
// color based on the current opponent
strokeColor: getLineColor(),
strokeOpacity: 0.7,
strokeWeight: 5,
}
// stores an array of points on a path between the vacuum and the opponent
// gets updated each time either of them moves (and is redisplayed)
var vacuum_to_opponent = new google.maps.Polyline(line_options);
// function to pick route color based on the current opponent
function getLineColor() {
if (cur_opponent_index == 0) {
return "#4092ef";
}
else if (cur_opponent_index == 1) {
return "#FF0000";
}
else if (cur_opponent_index == 2) {
return "#8F006B";
}
else if (cur_opponent_index == 3) {
return "#FF6600";
}
else if (cur_opponent_index == 4) {
return "#FFFF00";
}
else if (cur_opponent_index == 5) {
return "#00FFFF";
}
else if (cur_opponent_index == 6) {
return "#FF3591";
}
else if (cur_opponent_index == 7) {
return "#8AB800";
}
else if (cur_opponent_index == 8) {
return "#C56BE7";
}
else if (cur_opponent_index == 9) {
// Soviet red for Putin
return "#A60202";
}
else if (cur_opponent_index == 10) {
return "#0052A5";
}
else {
return "#FFFFFF";
}
}
// set up the game
function initialize() {
// set up map options
// disable most input, constrain zoom levels, center on Northfield
var mapOptions = {
center: { lat: 44.461, lng: -93.153},
zoom: 18,
minZoom: 13,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: false,
draggable: false,
keyboardShortcuts: false,
panControl: false,
scaleControl: false,
zoomControl: false,
streetViewControl: false
};
var mapDiv = document.getElementById('map-canvas');
map = new google.maps.Map(mapDiv, mapOptions);
// set up the player controlled object, a hungry hungry vacuum!
vacuum = new google.maps.Marker({
position: map.getCenter(),
map: map,
icon: "vacuum.gif",
anchorPoint: {
x: 0,
y: -10
}
});
// set up the object to be chased by the vacuum
opponent = new google.maps.Marker({
//position: { lat: 44.4605, lng: -93.1525},
position: findNewStart(),
map: map,
icon: opponents[cur_opponent_index] + "gif"
})
// every 150ms, attempt to recalculate shortest path between vacuum and opponent and redisplay
// this is going to result in a lot of errors/failed requests because of API usage limits
// oh well!!
setInterval(calcRoute, 150);
// initialize the distance and time remaining displays
updateDistanceDisplay();
updateTimeDisplay();
// initial calculation of vacuum_to_opponent route
calcRoute();
// set up the first opponent movement. will call itself recursively so the opponent never stops
moveOpponent();
// every second, update time counter and time display
setInterval(function() {
timeCounter--;
// if not timed mode, nothing is displayed
updateTimeDisplay();
// end game and reload page to start over
if (timeCounter === 0 && timedMode) {
alert("GAME OVER\nYour score is: " + catch_counter);
location.reload();
}
}, 1000);
// if the zoom level on the map is changed, adjust the movement values
google.maps.event.addListener(map, "zoom_changed", function() {
updateVacuumMovement();
opponent_movement = vacuum_movement * 50;
});
}
// return index number of new opponent (pick randomly from the others)
function newOpponent() {
new_opp = Math.floor(Math.random()*opponents.length);
// recursion because it's easier than being smart?
if (new_opp == cur_opponent_index) {
return newOpponent();
}
return new_opp;
}
// if timed mode, update time display with new time counter value
// if not, display infinity
function updateTimeDisplay() {
var x = document.getElementsByClassName("time");
if (timedMode) {
x[0].innerHTML = ("Time Remaining: " + timeCounter);
}
else {
x[0].innerHTML = ("Time Remaining: ∞");
}
if (timeCounter <= 10) {
x[0].style.color = "red";
}
}
// recalculate distance between vacuum and opponent and update display
function updateDistanceDisplay() {
var x = document.getElementsByClassName("distance");
// getDistance uses google maps API for spherical distance calculation
// default unit is meters
meters = Math.round(getDistance());
// if less than a mile, display in meters
if (meters < 1605) {
x[0].innerHTML = ("Distance: " + meters + "m");
}
// otherwise, display in miles
else {
miles = Math.round(getDistance()/1605*100)/100;
x[0].innerHTML = ("Distance: " + miles + "mi");
}
}
// return latitude/longitude of new goal point for opponent
function findGoal() {
var curPos = opponent.getPosition();
// to approximate normal distribution (more likely to pick central values), sum four uniform random numbers
var lat_inc = randomInc() + randomInc() + randomInc() + randomInc();
var lng_inc = randomInc() + randomInc() + randomInc() + randomInc();
var goal = new google.maps.LatLng(curPos.lat() + lat_inc, curPos.lng()+ lng_inc);
return goal;
}
// uses movement values calculated based on zoom level
function randomInc(){
return Math.random() * opponent_movement*2 - opponent_movement;
}
// set up a chain reaction of opponent movements towards a goal point
// using Google Maps driving directions
function moveOpponent() {
var start = opponent.getPosition();
var end = findGoal();
// request driving directions from current location to goal location
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
// if the request was successful
if (status == google.maps.DirectionsStatus.OK) {
// array of latitude/longitude pairs along the route between current location and goal
var goal_path = response.routes[0].overview_path;
// move to first point in goal path
// will call further movements and eventually will call moveOpponent() again
moveOneStep(0, goal_path);
}
// if the request was unsuccessful, try again after ten ms
// (API usage limits)
else {
setTimeout(moveOpponent, 10);
}
});
}
// move opponent to ith point in goal path
// and call further movement functions
function moveOneStep (i, goal_path) {
// after a specified increment of time (getStepTime())
setTimeout(function () {
// add the point to the opponent end of the vaccum to opponent path
addToRouteEnd(goal_path[i]);
// update position and distance
opponent.setPosition(goal_path[i]);
updateDistanceDisplay();
i++;
// if we're not done with the path, move to next point
if (i < goal_path.length) {
moveOneStep(i, goal_path);
}
// otherwise, calculate new path!
else if (i == goal_path.length) {
moveOpponent();
}
}, getStepTime())
}
// add the given point to the opponent end of the vacuum to opponent route
function addToRouteEnd(point) {
path = vacuum_to_opponent.getPath();
path.push(point);
vacuum_to_opponent.setPath(removeBacktracking(path));
}
// add the given point to the vacuum end of the vacuum to opponent route
function addToRouteBeginning(point) {
path = vacuum_to_opponent.getPath();
path.insertAt(0,point);
vacuum_to_opponent.setPath(removeBacktracking(path));
}
// how long opponent will take between movement steps
function getStepTime() {
if (map.getZoom() > 15) {
return 150;
}
return 100;
}
// detect and remove (some) loops in path
function removeBacktracking(MVCArray_path) {
path = MVCArray_path.getArray();
if (path.length == 0) {
return path;
}
for (var i = 0; i < path.length; i++) {
for (var j = 0; j < path.length; j++) {
if (i!= j && google.maps.geometry.spherical.computeDistanceBetween(path[i], path[j]) < 10) {
var k = j - i;
path.splice(i, k);
}
}
}
return path;
}
// use an API call to calculate walking directions from vacuum to opponent
// updates the global route
function calcRoute() {
var start = vacuum.getPosition();
var end = opponent.getPosition();
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
vacuum_to_opponent.setPath(response.routes[0].overview_path)
vacuum_to_opponent.setMap(map);
}
});
}
// pick a new location for the vacuum cleaner
function findNewStart() {
var curPos = vacuum.getPosition();
// pick random angle and use trig to get a point along a circle
angle = Math.random()*2*Math.PI;
var lat_inc = Math.sin(angle)*opponent_movement;
var lng_inc = Math.cos(angle)*opponent_movement;
var newPos = new google.maps.LatLng(curPos.lat() + lat_inc, curPos.lng()+ lng_inc);
return newPos;
}
// shorthand for vacuum to opponent distance (in meters)
function getDistance() {
return google.maps.geometry.spherical.computeDistanceBetween(vacuum.getPosition(), opponent.getPosition());
}
function getPoints() {
if (cur_opponent_index == 3) {
return 15;
}
if (cur_opponent_index == 0) {
return 25;
}
if (cur_opponent_index == 9) {
return 100;
}
if (cur_opponent_index == 10) {
return 50;
}
if (cur_opponent_index == 11) {
return 500;
}
return 5;
}
// handles vacuum/opponent collisions
function collision() {
immune = true;
// update score
catch_counter = catch_counter + getPoints();
var x = document.getElementsByClassName("score");
x[0].innerHTML = ("Score: " + catch_counter);
// get new opponent and update route color
cur_opponent_index = newOpponent();
line_options.strokeColor = getLineColor();
vacuum_to_opponent.setOptions(line_options);
opponent.setIcon(opponents[cur_opponent_index] + ".gif");
// find new position for vacuum
vacuum.setPosition(findNewStart());
updateDistanceDisplay()
map.panTo(vacuum.getPosition());
calcRoute();
// after 100ms, reset collision immunity
setTimeout(function() {
immune = false;
},100);
}
// based on zoom level
function updateVacuumMovement() {
zoom = map.getZoom();
if (zoom == 13) {
vacuum_movement = 0.00065;
}
if (zoom == 14) {
vacuum_movement = 0.00035;
}
if (zoom == 15) {
vacuum_movement = 0.00015;
}
if (zoom == 16) {
vacuum_movement = 0.000065;
}
if (zoom == 17) {
vacuum_movement = 0.00004;
}
if (zoom == 18) {
vacuum_movement = 0.00002;
}
if (zoom == 19) {
vacuum_movement = 0.000015;
}
if (zoom == 20) {
vacuum_movement = 0.000008;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
$(document).keydown(function(event) {
// zoom in
if (event.keyCode == 81){
map.setZoom(map.getZoom()-1);
}
// zoom out
else if (event.keyCode == 69){
map.setZoom(map.getZoom()+1);
}
// move the vacuum
else {
// up
if (event.keyCode == 87){
vacuum.setPosition(new google.maps.LatLng(vacuum.getPosition().lat() + vacuum_movement, vacuum.getPosition().lng() + 0));
}
// down
if (event.keyCode == 83){
vacuum.setPosition(new google.maps.LatLng(vacuum.getPosition().lat() - vacuum_movement, vacuum.getPosition().lng() + 0));
}
// right
if (event.keyCode == 68){
vacuum.setPosition(new google.maps.LatLng(vacuum.getPosition().lat() + 0, vacuum.getPosition().lng() + vacuum_movement));
}
// left
if (event.keyCode == 65){
vacuum.setPosition(new google.maps.LatLng(vacuum.getPosition().lat() + 0, vacuum.getPosition().lng() - vacuum_movement));
}
// update route and distance and recenter map on vacuum
addToRouteBeginning(vacuum.getPosition());
map.panTo(vacuum.getPosition());
updateDistanceDisplay()
// detect collision (if opponent is not currently immune)
// approximately: if within a mile in farthest out zoom
// if within 8 meters in closest zoom
if (getDistance() < 1000000*vacuum_movement && !immune) {
collision();
}
}
// alert('You pressed '+event.keyCode);
event.preventDefault();
});
// if a key is pressed
// document.onkeypress = function(){
//
// };
</script>
<div id="panel">
<p class="score">Score: 0</p>
<p class="time"> Time Remaining: </p>
<p class="distance">Distance: </p>
</div>
<div id="inst">
<p class="text"> wasd to move, qe to change zoom</p>
</div>
<audio src="soundtrack.mp3" autoplay loop>
Your broswer does not support this
</audio>
</body>
</html>