-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomeGuy.js
519 lines (454 loc) · 14 KB
/
SomeGuy.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//SomeGuy Adventure
//By Branden Rodgers
//VARIABLES
GameSpeed = 50;
player_selection = null; //For inventory and other selections
current_screen = "home"; //To decide which screen/menu to display
//Player Variabled
max_health = 30; //Total player health
max_water = 5; //Total player water
//for delaying sleep screen
sleep_timer = 30;
//////////////// LOAD IMAGES //////////////////////////////////
//Load screens
if (document.images)
{
var img = new Array();
img[0] = new Image();
img[0].src = 'screens/main_menu.png';
img[1] = new Image();
img[1].src = 'screens/adventure_screen.png';
img[2] = new Image();
img[2].src = 'screens/path_activity_screen.png';
img[3] = new Image();
img[3].src = 'screens/fight_screen.png';
img[4] = new Image();
img[4].src = 'screens/sleep_screen.png';
img[5] = new Image();
img[5].src = 'screens/armory_screen.png';
img[6] = new Image();
img[6].src = 'screens/weapon_screen.png';
img[7] = new Image();
img[7].src = 'screens/inventory_screen.png';
img[8] = new Image();
img[8].src = 'screens/post_fight_screen.png';
img[9] = new Image();
img[9].src = 'screens/pre_fight_screen.png';
}
//Load weapon images
if (document.images)
{
var weapon_img = new Array();
img[0] = new Image();
img[0].src = 'weapon_images/stick.png';
img[1] = new Image();
img[1].src = 'weapon_images/wood_spear.png';
img[2] = new Image();
img[2].src = 'weapon_images/wood_sword.png';
img[3] = new Image();
img[3].src = 'weapon_images/wood_club.png';
img[4] = new Image();
img[4].src = 'weapon_images/iron_club.png';
img[5] = new Image();
img[5].src = 'weapon_images/iron_spear.png';
img[6] = new Image();
img[6].src = 'weapon_images/wood_axe.png';
img[7] = new Image();
img[7].src = 'weapon_images/iron_axe.png';
img[8] = new Image();
img[8].src = 'weapon_images/iron_sword.png';
}
//////////////////// OBJECTS ///////////////////////////////
function guy(name, weapon, health, gold, water, level){
this.name = name;
this.weapon = weapon;
this.health = health;
this.gold = gold;
this.water = water;
this.level = level;
}
function enemy(name, health, damage, item){
this.name = name;
this.health = health;
this.damage = damage;
this.item = item;
}
function weapon(name, damage, cond, maxCond, wquantity, image){
this.name = name;
this.damage = damage;
this.cond = cond;
this.maxCond = maxCond;
this.wquantity = wquantity;
this.image = image;
}
function item(name, price, quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}
//////////////////// WEAPONS //////////////////////////////
//Weapom images
var stick_image = 'weapon_images/stick.png';
var wspear_image = 'weapon_images/wood_spear.png';
var wsword_image = 'weapon_images/wood_sword.png';
var wclub_image = 'weapon_images/wood_club.png';
var waxe_image = 'weapon_images/wood_axe.png';
var isword_image = 'weapon_images/iron_sword.png';
var ispear_image = 'weapon_images/iron_spear.png';
var iclub_image = 'weapon_images/iron_club.png';
var iaxe_image = 'weapon_images/iron_axe.png';
//name, damage, condition, max condition, quantity
var stick = new weapon("stick", 5, 10, 10, 1, stick_image);
var wsword = new weapon("wood sword", 10, 20, 20, 1, wsword_image);
var isword = new weapon("iron sword", 25, 40, 40, 1, isword_image);
var wclub = new weapon("wood club", 7, 30, 30, 1, wclub_image);
var iclub = new weapon("iron club", 15, 50, 50, 1, iclub_image);
var waxe = new weapon("wood axe", 10, 25, 25, 1, waxe_image);
var iaxe = new weapon("iron axe", 20, 40, 40, 1, iaxe_image);
var wspear = new weapon("wood spear", 13, 15, 15, 1, wspear_image);
var ispear = new weapon("iron spear", 30, 25, 25, 1, ispear_image);
/////////////////// ITEMS ////////////////////////////////
//name, price, quantity
var pelt = new item("pelt", 5, 1);
var claw = new item("claw", 2, 1);
var pelt5 = new item("pelt", 5, 5);
/////////////////// ENEMIES ////////////////////////////////
//name, health, damage, item
var wolf = new enemy("wolf", 10, 10, pelt);
var boar = new enemy("boar", 15, 5, claw);
var bear = new enemy("bear", 25, 10, pelt5);
var enemies = [wolf, boar, bear]; //Array of all enemies
//All enemy Health (for health reset)
//wolf, boar, bear
health_reset = [10, 15, 25]
////////////////// TEMP PLAYER /////////////////////////////
//name, weapon, health, gold, water, level
var player = new guy("buttFace", stick, 30, 10, 5, 1);
////////////////// KEY LISTENERS ////////////////////////////
//Menu selections
function clickListener(event) {
var canvas = document.getElementById("main_screen");
var xPos = event.x;
var yPos = event.y;
//xPos -= canvas.offsetLeft;
//yPos -= canvas.offsetTop;
//Menu Options
//Home Screen Navigation
if (current_screen == "home"){
//To Adventure
if (xPos >= 20 && xPos <= 250 && yPos >= 230 && yPos <= 300) {
current_screen = "adventure";
}
else if (xPos >= 250 && xPos <= 500 && yPos >= 230 && yPos <= 300) {
current_screen = "sleep";
}
else if (xPos >= 20 && xPos <= 250 && yPos >= 300 && yPos <= 370) {
current_screen = "armory";
}
else if (xPos >= 250 && xPos <= 500 && yPos >= 300 && yPos <= 370) {
current_screen = "inventory";
}
else if (xPos >= 20 && xPos <= 250 && yPos >= 370 && yPos <= 440) {
current_screen = "landMap";
}
}
//Adventure Screen Navigation
else if (current_screen == "adventure"){
//To Go Home
if (xPos >= 210 && xPos <= 315 && yPos >= 465 && yPos <= 505) {
homex = 9;
homey = 17;
current_screen = "home";
}
//To Go Left
else if (xPos >= 70 && xPos <= 135 && yPos >= 170 && yPos <= 235) {
current_screen = "path";
homex -= 1;
map_blocks[homey][homex] = true;
}
//To Go Straight
else if (xPos >= 235 && xPos <= 300 && yPos >= 170 && yPos <= 235) {
current_screen = "path";
homey -= 1;
map_blocks[homey][homex] = true;
}
//To Go Right
else if (xPos >= 395 && xPos <= 460 && yPos >= 170 && yPos <= 235) {
current_screen = "path";
homex += 1;
map_blocks[homey][homex] = true;
}
}
//Map Screen Navigation
else if (current_screen == "landMap"){
//To Go Home
if (xPos >= 200 && xPos <= 315 && yPos >= 460 && yPos <= 500) {
current_screen = "home";
}
}
//Armory Screen Navigation
else if (current_screen == "armory"){
//To Go Home
if (xPos >= 200 && xPos <= 315 && yPos >= 455 && yPos <= 495) {
current_screen = "home";
}
//Selecting the first weapon (array[0])
else if (xPos >= 0 && xPos <= 260 && yPos >= 85 && yPos <= 135) {
player_selection = 0;
}
//Selecting the second weapon (array[1])
else if (xPos >= 0 && xPos <= 260 && yPos >= 135 && yPos <= 185) {
player_selection = 1;
}
//Selecting the third weapon (array[2])
else if (xPos >= 0 && xPos <= 260 && yPos >= 185 && yPos <= 235) {
player_selection = 2;
}
//Selecting the fourth weapon (array[3])
else if (xPos >= 0 && xPos <= 260 && yPos >= 235 && yPos <= 285) {
player_selection = 3;
}
//Selecting the fifth weapon (array[4])
else if (xPos >= 0 && xPos <= 260 && yPos >= 285 && yPos <= 335) {
player_selection = 4;
}
//Selecting the sixth weapon (array[5])
else if (xPos >= 0 && xPos <= 260 && yPos >= 335 && yPos <= 385) {
player_selection = 5;
}
//Selecting the seventh weapon (array[6])
else if (xPos >= 0 && xPos <= 260 && yPos >= 385 && yPos <= 435) {
player_selection = 6;
}
//Selecting the eigth weapon (array[7])
else if (xPos >= 260 && xPos <= 500 && yPos >= 85 && yPos <= 135) {
player_selection = 7;
}
//Selecting the ninth weapon (array[8])
else if (xPos >= 260 && xPos <= 500 && yPos >= 135 && yPos <= 185) {
player_selection = 8;
}
//Selecting the tenth weapon (array[9])
else if (xPos >= 260 && xPos <= 500 && yPos >= 185 && yPos <= 235) {
player_selection = 9;
}
//Selectig the eleventh weapon (array[10])
else if (xPos >= 260 && xPos <= 500 && yPos >= 235 && yPos <= 285) {
player_selection = 10;
}
//Selectig the twelveth weapon (array[11])
else if (xPos >= 260 && xPos <= 500 && yPos >= 285 && yPos <= 335) {
player_selection = 11;
}
//Selectig the thirteenth weapon (array[12])
else if (xPos >= 260 && xPos <= 500 && yPos >= 335 && yPos <= 385) {
player_selection = 12;
}
//Selectig the fourteenth weapon (array[13])
else if (xPos >= 260 && xPos <= 500 && yPos >= 385 && yPos <= 435) {
player_selection = 13;
}
}
//Inventory Screen Navigation
else if (current_screen == "inventory"){
//To Go Home
if (xPos >= 200 && xPos <= 315 && yPos >= 455 && yPos <= 495) {
current_screen = "home";
}
}
//Weapon Display Screen Navigation
else if (current_screen == "weapon_display"){
//To Go back to armory
if (xPos >= 30 && xPos <= 132 && yPos >= 455 && yPos <= 495) {
player_selection = null;
current_screen = "armory";
}
//To equip the current weapon
if (xPos >= 350 && xPos <= 426 && yPos >= 425 && yPos <= 455) {
player.weapon = armory[wList[player_selection]].weapon;
}
}
//Fight Display Screen Navigation
else if (current_screen == "fight" && fight_timer < 50){
//To Attack
if (xPos >= 195 && xPos <= 325 && yPos >= 100 && yPos <= 140) {
player_move = "attack";
}
//To Trick
if (xPos >= 195 && xPos <= 325 && yPos >= 150 && yPos <= 190) {
player_move = "trick";
}
//To Defend
if (xPos >= 195 && xPos <= 325 && yPos >= 200 && yPos <= 240) {
player_move = "defend";
}
}
}
//Not used right now, but just in case
function keyListener(e) {
if(!e)
{
e = window.event;
}
var ypos;
if(e.keyCode == 49)
{
yPos = 1;
}
else if(e.keyCode == 50)
{
yPos = 1;
}
else if(e.keyCode == 51)
{
yPos = 1;
}
else if(e.keyCode == 52)
{
yPos = 1;
}
}
///////////////// GAME MENU HANDLER /////////////////////////////
function game_handler(){
if (current_screen == "home"){
home();
}
else if (current_screen == "adventure"){
adventure();
}
else if (current_screen == "path"){
path_activity();
}
else if (current_screen == "sleep"){
sleep();
}
else if (current_screen == "pre_fight"){
pre_fight();
}
else if (current_screen == "fight"){
fight();
}
else if (current_screen == "post_fight"){
post_fight();
}
else if (current_screen == "fight_attack"){
fight_attack();
}
else if (current_screen == "broken_weapon"){
broken_weapon();
}
else if (current_screen == "armory"){
armoryHandler(player_selection);
}
else if (current_screen == "weapon_display"){
weaponDisplay(player_selection);
}
else if (current_screen == "inventory"){
inventoryHandler();
}
else if (current_screen == "landMap"){
landMap();
}
}
////////////////// HOME MENU ///////////////////////////////////
function home(){
//Refill water when at home
player.water = max_water;
//locate main canvas in document and clear
var main_canvas = document.getElementById("main_screen");
var context = main_canvas.getContext("2d");
context.clearRect(0,0,500,500);
//Draw main background
var main_menu1 = new Image();
main_menu1.src = "screens/main_menu.png";
context.drawImage(main_menu1,0,0);
context.font = "30px Papyrus";
context.fillText(player.name, (240 - (player.name.length * 6)), 103);
context.fillText(player.level, 245, 190);
context.font = "30px Copperplate";
context.fillText(player.health + " / " + max_health, 55, 195);
context.fillText(player.gold, 375, 195);
//Menu Options
context.font = "40px Papyrus";
context.fillText("Adventure", 30, 265);
context.fillText("Sleep", 320, 262);
context.fillText("Armory", 50, 335);
context.fillText("Inventory", 290, 335);
context.fillText("Map", 75, 402);
}
/////////////////// SLEEP ////////////////////////////////////////
function sleep(){
//locate main canvas in document and clear
var main_canvas = document.getElementById("main_screen");
var context = main_canvas.getContext("2d");
context.clearRect(0,0,500,500);
//Draw main background
var sleep_screen1 = new Image();
sleep_screen1.src = "screens/sleep_screen.png";
context.drawImage(sleep_screen1,0,0);
context.font = "30px Papyrus";
context.fillText("You wake up feeling rejuvinated", 40, 150);
sleep_timer -= 1;
if (sleep_timer <= 0){
current_screen = "home";
player.health = max_health;
sleep_timer = 30;
}
}
///////////////// STEP AND GAME INIT /////////////////////////////
//Calls draw function
//Checks game end condition
//Waits for next step
function step()
{
game_handler(); //Draw to screen and update status
if(player.health <= 0)
{
//locate main canvas in document and clear
var main_canvas = document.getElementById("main_screen");
var context = main_canvas.getContext("2d");
context.clearRect(0,0,500,500);
context.font = "50px Georgia";
context.fillText("You Died", 220, 250);
}
else
{
wait_for_step();
}
}
//Timer til next draw
function wait_for_step()
{
setTimeout('step()', GameSpeed); //Wait (int) GameSpeed, call step()
}
//Main
function game()
{
//Hide start button once clicked
document.getElementById("button").style.visibility = 'hidden';
//Add event listeners to get clicks and button presses
document.getElementById("main_screen").addEventListener("mousedown", clickListener, false);
document.onkeydown = keyListener;
//Initialize player with inputted name
pname1 = document.getElementById("the-textbox").value;
pname = pname1[0].toUpperCase() + pname1.substring(1, pname1.length);
document.getElementById("the-textbox").value = "Goodluck, " + pname;
player.name = pname;
//Adding to Armory for testing
addWeapon(stick);
addWeapon(wsword);
addWeapon(wsword);
addWeapon(isword);
addWeapon(wclub);
addWeapon(iclub);
addWeapon(waxe);
addWeapon(iaxe);
addWeapon(wspear);
addWeapon(ispear);
addWeapon(ispear);
//Adding to inventory for testing
addItem(pelt);
addItem(claw);
//Start wait-for-step cycle (run game)
wait_for_step();
}