-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.js
More file actions
466 lines (425 loc) · 15.2 KB
/
map.js
File metadata and controls
466 lines (425 loc) · 15.2 KB
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
//Starting Map Class - This class will create a new Hero and keep track of him and his
// stats. Some of the functions in this map class will call functions in the hero class.
// This map class will communicate extensively with the hero class.
var game_map;
var text;
//map class constructor
function Map(width, height, starting_x, starting_y, starting_energy, starting_whiffles) {
//default constructor
if(width === undefined){
//This creates a new hero, and passes the hero constructor the parameters
this.hero = new Hero(0, 0, 10000,10000, false);
this.width = 25;
this.height = 25;
this.diamond_x = 2;
this.diamond_y = 2;
this.cells = [[]];
for (var i = 0; i < this.width; ++i) {
this.cells[i] = [];
for (var j = 0; j < this.height; ++j) {
this.cells[i][j] = new mapCell();
}
}
this.place_chests()
this.cells[this.diamond_x][this.diamond_y].object = "Royal Diamonds";
return;
}
//copy constructor
if(height === undefined ){
var state = width;
this.hero = new Hero(state.hero.x, state.hero.y,
state.hero.energy, state.hero.whiffles,
state.hero.binoculars,
state.hero.inventory);
this.width = state.width;
this.height = state.height;
this.diamond_x = state.diamond_x;
this.diamond_y = state.diamond_y;
this.cells = state.cells;
this.cells[this.diamond_x][this.diamond_y].object = "Royal Diamonds";
return;
}
else{
//This creates a new hero, and passes the hero constructor the parameters
this.hero = new Hero(starting_x, starting_y, starting_energy, starting_whiffles, false);
this.width = parseInt(width);
this.height = parseInt(height);
this.diamond_x = 2;
this.diamond_y = 2;
this.cells = [[]];
for (var i = 0; i < this.width; ++i) {
this.cells[i] = [];
for (var j = 0; j < this.height; ++j) {
this.cells[i][j] = new mapCell();
}
}
this.place_chests()
this.cells[this.diamond_x][this.diamond_y].object = "Royal Diamonds";
return;
}
}
//Member Functions:
//These functions move the hero. They call the hero's move functions, and they
// check to see if the hero needs to wrap to the other side of the map.
//MOVE NORTH
Map.prototype.move_north = function()
{
this.move(0,1);
};
// MOVE SOUTH
Map.prototype.move_south = function()
{
this.move(0,-1);
};
//MOVE EAST
Map.prototype.move_east = function()
{
this.move(1,0);
};
// MOVE WEST
Map.prototype.move_west = function()
{
this.move(-1,0);
};
Map.prototype.move = function(x,y)
{
nextx = (this.hero.x + x) % this.width;
nexty = (this.hero.y + y) % this.height;
if(nextx < 0)
nextx = this.width -1;
if(nexty < 0)
nexty = this.height -1;
if ( !this.isWater(nextx, nexty) ){
this.hero.x = nextx;
this.hero.y = nexty;
}
// Message needs to come before checking for (and removing) chests.
document.getElementById("message").value = message(this.hero, this.cells[this.hero.x][this.hero.y]);
//update balances if hero PURCHASES a POWER BAR
if(this.cells[this.hero.x][this.hero.y].object === "PowerBar") {
this.powerBar();
}
// Check for binoculars
if(this.cells[this.hero.x][this.hero.y].object === "Binoculars") {
this.binoculars();
}
// Check for tool
if(this.cells[this.hero.x][this.hero.y].object === "Axe") {
this.purchase_item("Axe", 30);
}
if(this.cells[this.hero.x][this.hero.y].object === "Shears") {
this.purchase_item("Shears", 35);
}
if(this.cells[this.hero.x][this.hero.y].object === "Rock") {
this.purchase_item("Rock", 1);
}
if(this.cells[this.hero.x][this.hero.y].object === "Machete") {
this.purchase_item("Machete", 25);
}
if(this.cells[this.hero.x][this.hero.y].object === "Hatchet") {
this.purchase_item("Hatchet", 15);
}
if(this.cells[this.hero.x][this.hero.y].object === "Chainsaw") {
this.purchase_item("Chainsaw", 60);
}
if(this.cells[this.hero.x][this.hero.y].object === "Chisel") {
this.purchase_item("Chisel", 5);
}
if(this.cells[this.hero.x][this.hero.y].object === "Sledge") {
this.purchase_item("Sledge", 25);
}
if(this.cells[this.hero.x][this.hero.y].object === "Jackhammer") {
this.purchase_item("Jackhammer", 100);
}
// Compare hero's current cell terrain with bog value
// and calls update hero stats tp deduct energy by 2
if(this.cells[this.hero.x][this.hero.y].terrain === 4) {
this.hero.update_energy(-2);
}
else{
//update energy for one step
this.hero.update_energy(-1);
}
this.isObstacle();
//Update the Map.
this.update();
};
// checking if the cell contains water
Map.prototype.isWater = function(x,y)
{
return this.cells[x][y].terrain === 2;
};
//checking for obstacle then removing said obstacle and decrementing hero's energy
Map.prototype.isObstacle = function()
{
let currentObject = this.cells[this.hero.x][this.hero.y].object;
if(currentObject === "Tree")
{
this.hero.energy -= 10;
this.cells[this.hero.x][this.hero.y].object = "None";
}
else if(currentObject === "Boulder")
{
this.hero.energy -= 16;
this.cells[this.hero.x][this.hero.y].object = "None";
}
else if(currentObject === "BlackberryBushes")
{
this.hero.energy -= 4;
this.cells[this.hero.x][this.hero.y].object = "None";
}
if(this.hero.energy <= 0)
this.player_lost();
};
//This function will be called when the player has won the game. It
// will do an end-game sequence.
Map.prototype.player_won = function()
{
window.location.replace("win.html");
localStorage.clear();
};
//This function will be called when the player has lost the game. It
// will do an end-game sequence.
Map.prototype.player_lost = function()
{
window.location.replace("lose.html");
localStorage.clear();
};
// update the web page's information with the current information about the hero.
// It will also update the map's visibility.
Map.prototype.update = function()
{
var view_distance = 1;
if(this.hero.binoculars) {
view_distance = 2;
}
//Update the map to set the tiles around the hero to be visible:
var start_i = this.hero.x - view_distance;
if (start_i < 0) {
start_i = 0;
}
var start_j = this.hero.y-view_distance;
if (start_j < 0) {
start_j = 0;
}
for (var i = start_i; (i <= this.hero.x + view_distance) && (i < this.width); ++i) {
for (var j = start_j; (j <= this.hero.y + view_distance) && (j < this.height); ++j) {
this.cells[i][j].isVisible = true;
}
}
//check for treasure chests
this.check_chests();
//Update the map displayed on the page:
document.getElementById("map_box").innerHTML = this.map_string();
// Update the game state information displayed on the page:
document.getElementById("location").value = this.hero.display_location();
document.getElementById("energy").value = this.hero.display_energy();
document.getElementById("whiffles").value = this.hero.display_whiffles();
document.getElementById("inventory").innerHTML = this.hero.inventory.display_inventory();
localStorage.setItem('map', JSON.stringify(game_map) );
//check diamonds
if ((this.hero.x === this.diamond_x) && (this.hero.y === this.diamond_y))
this.player_won();
//check energy level
if (this.hero.energy <= 0)
this.player_lost();
};
// Places a number of treasure chests on the map cells randomly
Map.prototype.place_chests = function(){
//chests in the lower left corner for testing
this.cells[0][1].object = "Chest 1";
this.cells[0][2].object = "Chest 2";
var amount = 5;
var x, y, type;
for (var i = 0; i < amount; ++i){
x = Math.floor(Math.random() * this.width);
y = Math.floor(Math.random() * this.height);
type = Math.floor(Math.random() * 2);
if (type == 1){
this.cells[x][y].object = "Chest 1";
}
else{
this.cells[x][y].object = "Chest 2";
}
}
}
// Formats the map array as the contents of an HTML table.
Map.prototype.map_string = function() {
result = "";
for (var j = this.height-1; j >= 0; --j) {
for (var i = 0; i < this.width; ++i) {
var cell = this.cells[i][j];
if (j === this.hero.y && i === this.hero.x) {
result += "<b>@</b>";
} else if(cell.isVisible) {
switch(cell.object) {
case "Tree":
// Tree
result += "<span style=\"color:red;\">T</span>";
break;
case "Boulder":
// Rock
result += "<span style=\"color:red;\">R</span>";
break;
case "BlackberryBushes":
// Bushes
result += "<span style=\"color:red;\">B</span>";
break;
case "Binoculars":
// Binoculars = "Field Glasses"
result += "F";
break;
case "Royal Diamonds":
// Diamonds
result += "<span style=\"color:blue;\">D</span>";
break;
case "Chest 1":
//chest type 1
result += "<span style=\"color:orange;\">C</span>";
break;
case "Chest 2":
//chest type 2 looks the same as 1
result += "<span style=\"color:orange;\">C</span>";
break;
case "PowerBar":
// Power Bar
result += "<span style=\"color:purple;\">P</span>";
break;
case "Axe":
//Axe
result += "<span style=\"color:green;\">A</span>";
break;
case "Shears":
//Sheers
result += "<span style=\"color:green;\">S</span>";
break;
case "Rock":
//Rock
result += "<span style=\"color:green;\">R</span>";
break;
case "Machete":
//Machete
result += "<span style=\"color:green;\">M</span>";
break;
case "Chainsaw":
//Chainsaw
result += "<span style=\"color:green;\">X</span>";
break;
case "Jackhammer":
//Jackhammer
result += "<span style=\"color:green;\">J</span>";
break;
case "Chisel":
//Chisel
result += "<span style=\"color:green;\">H</span>";
break;
case "Sledge":
//Sledge
result += "<span style=\"color:green;\">L</span>";
break;
case "Hatchet":
//Hatchet
result += "<span style=\"color:green;\">T</span>";
break;
case "None":
switch(cell.terrain) {
case 0:
// Meadow
result += "-";
break;
case 1:
// Forest
result += ";";
break;
case 2:
// Water
result += "~";
break;
case 3:
// Wall
result += "#";
break;
case 4:
// Bog
result += "%";
break;
case 5:
// Swamp
result += ",";
break;
default:
result += "?";
break;
}
break;
default:
result += "?";
break;
}
} else {
result += " ";
}
}
result += "<br>";
}
return result;
}
Map.prototype.powerBar = function ()
{
//check if hero has enough whiffles
if (this.hero.check_balance(1) === false){
alert("You do not have enough whiffles for a Power Bar.")
} else {
//prompt user
var result = window.confirm("Would You like to purchase a POWER BAR (20 energy units) for 1 Whiffle?");
if (result) {
//if purchased, remove from mapCell
this.cells[this.hero.x][this.hero.y].object = "None";
this.hero.update_energy(20);
this.hero.update_whiffles(-1);
}
}
}
Map.prototype.binoculars = function ()
{
//check if hero has enough whiffles
if (this.hero.check_balance(50) === false){
alert("You do not have enough whiffles for Binoculars.")
} else {
//prompt user
var result = window.confirm("Would You like to purchase a pair of BINOCULARS for 50 Whiffle?");
if (result) {
//if purchased, remove from mapCell
this.cells[this.hero.x][this.hero.y].object = "None";
this.hero.binoculars = true;
this.hero.update_whiffles(-50);
this.hero.inventory.add_item("Binoculars");
}
}
}
Map.prototype.check_chests = function () {
//check chests
if(this.cells[this.hero.x][this.hero.y].object == "Chest 1"){
this.hero.update_whiffles(100);
this.cells[this.hero.x][this.hero.y].object = "None";
}
if(this.cells[this.hero.x][this.hero.y].object == "Chest 2"){
this.hero.whiffles = 0;
this.cells[this.hero.x][this.hero.y].object = "None";
}
}
Map.prototype.purchase_item = function(item_type, item_cost) {
if(this.hero.check_balance(item_cost) === false)
{
alert("You do not have enough whiffles for " + item_type);
}
else
{
var result = window.confirm("Would you like to purchase " + item_type + " for " + item_cost + " Whiffles?");
if(result){
this.cells[this.hero.x][this.hero.y].object = "None";
this.hero.inventory.add_item(item_type);
this.hero.update_whiffles(-item_cost);
}
}
}