-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaScript.js
executable file
·393 lines (356 loc) · 13.3 KB
/
JavaScript.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
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var east;
var west;
var north;
var south;
var id;
var options;
var pacman;
var score = 0;
var currPosition;
var overlay;
var map;
var centerCoords;
var lat;
var lng;
var buttonSize = .00006;
var horizontalMax = .0005;
var horizontalSpacing = horizontalMax;
var verticalMax = .0005;
var verticalSpacing = verticalMax;
var dotLocations;
var ghostTol = .000005;
var tol = .0002;
var actualDotLocations = [];
var overlayArray = [];
var temp;
var redGhostOverlay;
var pinkGhostOverlay;
var myPacY;
var myPacX;
var myPacXPrev;
var myPacYPrev;
/*
Initializes Map
*/
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 17
});
// East, West, North, and South are 4 different pictures for
// different location directions facing
east = {
url: "east.png",
size: new google.maps.Size(30,30),
scaledSize: new google.maps.Size(30,30)
};
west = {
url: "west.png",
size: new google.maps.Size(30,30),
scaledSize: new google.maps.Size(30,30)
};
north = {
url: "north.png",
size: new google.maps.Size(30,30),
scaledSize: new google.maps.Size(30,30)
};
south = {
url: "south.png",
size: new google.maps.Size(30,30),
scaledSize: new google.maps.Size(30,30)
};
//Initializes the pacman for the maps as a Marker object
pacman = new google.maps.Marker({
map: map,
icon: east
});
// Gets Location and moves map and pacman onto the map at the current location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
centerCoords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
walkersStuff();
currPosition = centerCoords;
pacman.setPosition(pos);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
//Specifies the options for the geo watching detection method below
options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
};
id = navigator.geolocation.watchPosition(success, error, options);
}
/*
Success function for when a movement is detected
*/
function success(position) {
currPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var lat = position.coords.latitude;
var lng = position.coords.longitude;
myPacX = lng;
myPacY = lat;
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
if (actualDotLocations.length > 0) {
for (i = 0; i < actualDotLocations.length; i++) {
if (((Math.abs(actualDotLocations[i].lat() - lat)) < tol)
&& (Math.abs(actualDotLocations[i].lng() - lng)) < tol) {
score += 100;
document.getElementById("score").innerHTML = score.toString();
actualDotLocations.splice(i,1);
updateYellowDots(i);
//console.log(actualDotLocations);
}
}
}
pacman.setPosition(pos);
navigator.geolocation.clearWatch(id);
id = navigator.geolocation.watchPosition(success, error, options);
}
/*
Error method for when no movement is detected
*/
function error(err) {
console.warn("Hey it didn't work");
navigator.geolocation.clearWatch(id);
id = navigator.geolocation.watchPosition(success, error, options);
}
/*
Hanles location error in case the browser does not support our functionality
*/
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
/*
Walker's Big Function
*/
function walkersStuff() {
var markerArray = [];
lat = centerCoords.lat();
lng = centerCoords.lng();
// Instantiate a directions service.
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
// Instantiate an info window to hold step text.
var stepDisplay = new google.maps.InfoWindow;
dotLocations = [];
var toAdd;
for (y = centerCoords.lat() - verticalMax; y <= centerCoords.lat() + verticalMax; y = y + verticalSpacing) {
for (x = centerCoords.lng() - horizontalMax; x <= centerCoords.lng() + horizontalMax; x = x + horizontalSpacing) {
if ((Math.abs(y - centerCoords.lat()) > .0001) && (Math.abs(x
- centerCoords.lng()) > .0001)) {
toAdd = new google.maps.LatLng(y, x);
dotLocations.push(toAdd);
//console.log((actualDotLocations[actualDotLocations.length - 1]).lat());
}
}
}
for (i = 0; i < dotLocations.length; i++) {
calculateAndDisplayRoute(
directionsDisplay, directionsService, markerArray, stepDisplay, map, dotLocations[i]);
//makeYellowDots(actualDotLocations[i]);
}
// Listen to change events from the start and end lists.
var onChangeHandler = function() {
calculateAndDisplayRoute(
directionsDisplay, directionsService, markerArray, stepDisplay, map);
};
// console.log('hey!');
// for (i = 0; i < actualDotLocations.length; i++) {
// console.log(i);
// makeYellowDots(actualDotLocations[i]);
// console.log(actualDotLocations[i]);
// }
}
/*
Walker's function
*/
function makeYellowDots(latitudeLongitude) {
var imageBounds = {
north: latitudeLongitude.lat() + .666*buttonSize,
south: latitudeLongitude.lat() - .666*buttonSize,
east: latitudeLongitude.lng() + buttonSize,
west: latitudeLongitude.lng() - buttonSize
};
overlayBefore = new google.maps.GroundOverlay('yellowdot.png',imageBounds);
overlayBefore.setMap(map);
overlayArray.push(overlayBefore);
//console.log(dotLocations);
}
function updateYellowDots(i) {
//console.log(actualDotLocations.length);
overlayArray[i].setMap(null);
overlayArray.splice(i,1);
// for (i = 0; i < actualDotLocations.length; i++) {
// makeYellowDots(actualDotLocations[i]);
// }
}
/*
Walker's function
*/
function showSteps(directionResult, markerArray, stepDisplay, map) {
var myRoute = directionResult.routes[0].legs[0];
var endPoint = myRoute.steps[myRoute.steps.length - 1].end_location
//############makeYellowDots(endPoint);
actualDotLocations.push(endPoint);
console.log((actualDotLocations[actualDotLocations.length - 1]).lat());
makeYellowDots(endPoint);
}
/*
Walker's function
*/
function calculateAndDisplayRoute(directionsDisplay, directionsService,
markerArray, stepDisplay, map, thisDestination) {
directionsService.route({
origin: centerCoords,
destination: thisDestination,
travelMode: google.maps.TravelMode.WALKING
}, function(response, status) {
// Route the directions and pass the response to a function to create
// markers for each step.
if (status === google.maps.DirectionsStatus.OK) {
document.getElementById('warnings-panel').innerHTML =
'<b>' + response.routes[0].warnings + '</b>';
showSteps(response, markerArray, stepDisplay, map);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
function makePinkGhost(ghostY, ghostX) {
var ghostYUp = ghostY + .00002;
var ghostYDown = ghostY - .00002;
var ghostXUp = ghostX + .000025;
var ghostXDown = ghostX - .000025;
var imageBounds = {
north: ghostYUp,
south: ghostYDown,
east: ghostXUp,
west: ghostXDown
};
if (pinkGhostOverlay)
pinkGhostOverlay.setMap(null);
pinkGhostOverlay = new google.maps.GroundOverlay('pacman_ghost_pink.png', imageBounds);
pinkGhostOverlay.setMap(map);
}
function makeRedGhost(ghostY, ghostX) {
var ghostYUp = ghostY + .00002;
var ghostYDown = ghostY - .00002;
var ghostXUp = ghostX + .000025;
var ghostXDown = ghostX - .000025;
var imageBounds = {
north: ghostYUp,
south: ghostYDown,
east: ghostXUp,
west: ghostXDown
};
if (redGhostOverlay)
redGhostOverlay.setMap(null);
redGhostOverlay = new google.maps.GroundOverlay('pacman_ghost_red.png', imageBounds);
redGhostOverlay.setMap(map);
}
function movePinkGhost(pacX, pacY, pacXPrev, pacYPrev) {
deltaX = pacX - pacXPrev;
deltaY = pacY - pacYPrev;
pacX = pacX + (2 * deltaX);
pacY = pacY + (2 * deltaY);
var ghostX = pinkGhostOverlay.getBounds().getCenter().lng();
var ghostY = pinkGhostOverlay.getBounds().getCenter().lat();
if(pacY > ghostY) {
ghostY = ghostY + .00001;
} else if (pacY < ghostY) {
ghostY = ghostY - .00001;
}
if(pacX > ghostX) {
ghostX = ghostX + .00001;
} else if(pacX < ghostX) {
ghostX = ghostX - .00001;
}
makePinkGhost(ghostY, ghostX);
}
function moveRedGhost(pacX, pacY) {
var ghostX = redGhostOverlay.getBounds().getCenter().lng();
var ghostY = redGhostOverlay.getBounds().getCenter().lat();
if(pacY > ghostY) {
ghostY = ghostY + .00001;
} else if (pacY < ghostY) {
ghostY = ghostY - .00001;
}
if(pacX > ghostX) {
ghostX = ghostX + .00001;
} else if(pacX < ghostX) {
ghostX = ghostX - .00001;
}
makeRedGhost(ghostY, ghostX);
checkCollision(ghostX, pacX, ghostY, pacY);
}
function moveGhosts(pacX, pacY, pacXPrev, pacYPrev) {
moveRedGhost(pacX, pacY);
//movePinkGhost(pacX, pacY, pacXPrev, pacYPrev);
}
// Use the DOM setInterval() function to change the offset of the symbol
// at fixed intervals.
function startGame() {
var count = 0;
window.setInterval(function() {
moveGhosts(myPacX, myPacY, myPacXPrev, myPacYPrev);
}, 1000);
}
/*
Start Method
*/
function start() {
makeRedGhost(lat - .0003, lng - .00007);
//makePinkGhost(lat + .0003, lng + .00007);
myPacX = lng;
myPacY = lat;
var audio = document.getElementById("chomp");
audio.play();
startGame();
}
/*
Checks Collision of Pacman and Ghosts and ends game if you lose.
*/
function checkCollision(gX, pX, gY, pY) {
if ((Math.abs(gX - pX) < ghostTol) && (Math.abs(gY - pY)) < ghostTol) {
gameOver();
}
}
/*
Game Over
*/
function gameOver() {
var audio = document.getElementById("death");
window.location.href = "GameOver.html";
audio.play();
}
function somethingReallyCool() {
if (actualDotLocations.length > 0) {
temp = actualDotLocations.length - 1;
score += 100;
document.getElementById("score").innerHTML = score.toString();
actualDotLocations.splice(temp,1);
updateYellowDots(temp);
console.log(actualDotLocations);
}
}