-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.js
484 lines (446 loc) · 12.2 KB
/
sketch.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
var tilewidth = 32;
var spritewidth = 32; // number of pixel width in a tile sprite
var canvaswidth = 20 * tilewidth;
var canvasheight = 12 * tilewidth;
var playerone;
var dialogsplayer;
var doorsopened;
var itemfound;
var touchedclue;
var devil;
var devilspoke;
var playerspoke;
var next;
var coinspicked;
var coinsused;
// Map, each name corresponds to map number
// (corresponds to numbers in level-data.json)
const stage_names = {
"orange": 1,
"red": 2,
"purple": 3,
"blue": 4,
"green": 5,
"yellow": 6,
"gates_1": 7, // first scene, with both gates
"doors_corridor": 8, // corridor with colored doors
"gates_2": 9 // last scene, with both gates
}
var numberOfLevels = Object.keys(stage_names).length;
var level_info;
var tileset;
var tileset_row_blocks = 6;
var tileset_col_blocks = 18;
var tile_info;
var itemset;
var itemset_row_blocks = 6;
var itemset_col_blocks = 37;
var item_info;
var playerset;
var playerset_row_blocks = 6;
var playerset_col_blocks = 6;
var tiles;
var floors;
var walls;
var doors;
var tile;
var maps = {};
var map_data;
var item_data;
var tempMap;
var stage = 0;
var changeStage = false;
var gameMap;
var interract_blocks = []; // interractive blocks for each map
var items_names;
var speter_img; // saint peter image
var devil_img; // devil image
var intro_screen_img; // image for intro screen (stage 0)
/**
* Preload asset files.
*/
function preload() {
intro_screen_img = loadImage('/assets/images/backgrounds/intro.png');
tileset = loadImage('/assets/images/map tiles/tiles-' + tileset_row_blocks + '-' + tileset_col_blocks + '-200.png');
tile_info = loadJSON('/assets/images/map tiles/tileset.json');
itemset = loadImage('/assets/images/map tiles/items-' + itemset_row_blocks + '-' + itemset_col_blocks + '-200.png');
item_info = loadJSON('/assets/images/map tiles/itemset.json');
level_info = loadJSON('/assets/level-data.json');
playerset = loadImage('/assets/images/characters/ghost.png');
dialogs_info = loadJSON('/assets/dialogsset.json');
speter_img = loadImage('/assets/images/characters/speter.png');
devil_img = loadImage('/assets/images/characters/devil.png');
}
/**
* Set up game.
*/
function setup() {
frameRate(100);
// Put canvas in center of window
var cnv = createCanvas(canvaswidth, canvasheight);
var x = (windowWidth - width) / 2;
var y = (windowHeight - height) / 2;
cnv.position(x, y);
playerone = new Player(100, 300, 1);
dialogsplayer = new Dialogs();
devil = true;
next = false;
devilspoke = false;
playerspoke=false;
doorsopened = [false, false, false, false, false, false, false];
itemfound = [false, false, false, false, false, false, false, false];
playerOut = false;
touchedclue = [false, false, false, false, false, false, false, false];
items_names = ["fridge", "bed", "chest", "t_paper", "suitcase", "book_leaves", "chair_purple", "desk_purple"]
//items in touched clue : 0:fridge 1:bed 2:chest 3:toiletpaper 4:suitcase 5:book_leaves 6:chair 7:desk
cantchangerooms = true;
coinspicked=[false, false, false, false, false, false]
coinsused=[false, false, false, false, false, false]
// Create Maps
createMaps();
}
/**
* Create each map based on asset files.
*/
function createMaps() {
// for stage 0
let map = new Array(canvasheight / tilewidth);
let items = new Array(canvasheight / tilewidth);
for (let i = 0; i < canvasheight / tilewidth; i++) {
map[i] = new Array(canvaswidth / tilewidth).fill(61);
items[i] = new Array(canvaswidth / tilewidth).fill(1);
}
tempMap = new GameMap(0, map, items);
tempMap.create();
maps[0] = tempMap
// for the rest stages
for (let i = 1; i <= numberOfLevels; i++) {
tempMap = new GameMap(i, level_info[`map_${i}`], level_info[`items_${i}`]);
tempMap.create();
maps[i] = tempMap;
}
}
function draw() {
background(0, 0, 0);
if (stage == 0) {
image(intro_screen_img, 0, 0);
gameMap = maps[stage];
gameMap.draw();
if (kb.pressing("x")) {
changeStage = true;
}
if (changeStage) {
stage = stage_names["gates_1"];
}
return;
}
//TODO ADD DIALOGS and set the global var as true to draw the icons
if (changeStage) {
changeStage = false;
if (stage == 7) { //entry paradise/hell door
entryStage();
} else if (stage == 8) { //6 doors for the room
doorsStage();
} else if (stage == 1) { //bedroom
callDoubleClueStage(2, 1, 12, 13, 2, 14);
} else if (stage == 2) { //kitchen
calltStage(1, 0, 10, 11);
} else if (stage == 3) { //office
callDoubleClueStage(6, 6, 21, 26, 7, 25)
} else if (stage == 4) { //wc
calltStage(3, 3, 15, 16);
} else if (stage == 5) { //garden
if (devil) {
talkWithTheDevil();
}
if (next) {
calltStage(5, 5, 19, 20);
}
} else if (stage == 6) { //living room
calltStage(4, 4, 17, 18);
}
}
if (stage != 0 && stage != 7 && stage != 8) {
needHint();
canIHelp();
}
enterTheRoom();
leaveTheRoom();
gameMap = maps[stage];
gameMap.draw();
playerone.draw();
playerone.update();
}
function needHint() {
if (kb.released("g")) {
changeStage = false;
cantchangerooms = true;
playerone.sprite.changeAnimation("pray");
dialogsplayer.tell("player", 35);
setTimeout(() => {
cantchangerooms = false;
}, "5000");
if (itemfound[6]) {
cantchangerooms = true;
dialogsplayer.tell("petros", 46);
setTimeout(() => {
changeStage = true;
cantchangerooms = false;
}, "5000");
}
for (let i = 0; i < 7; i++) {
num = 36 + i;
if (!itemfound[i]) {
if (playerone.doIhaveCoins()) {
//TODO tell the right hint
cantchangerooms = true;
dialogsplayer.tell("petros", num);
setTimeout(() => {
changeStage = true;
cantchangerooms = false;
}, "5000");
} else {
cantchangerooms = true;
dialogsplayer.tell("petros", 45);
setTimeout(() => {
changeStage = true;
cantchangerooms = false;
}, "5000");
}
return;
}
}
}
}
function canIHelp() {
if (kb.released("f")) {
changeStage = false;
cantchangerooms = true;
dialogsplayer.tell("petros", 44);
setTimeout(() => {
changeStage = true;
cantchangerooms = false;
}, "15000");
}
}
function talkWithTheDevil() {
changeStage = false;
if (!playerspoke){
cantchangerooms = true;
dialogsplayer.tell("devil", 30);
dialogsplayer.tell("player", 29);
setTimeout(() => {
devilspoke = true;
changeStage = true;
playerspoke=true;
}, "6000");
}
if (!devilspoke) {
cantchangerooms = true;
dialogsplayer.tell("devil", 31);
setTimeout(() => {
devilspoke = true;
changeStage = true;
}, "15000");
}
if (devilspoke) {
changeStage = true;
if (kb.released("1") || kb.released("3")) {
changeStage = false;
dialogsplayer.tell("devil", 34);
setTimeout(() => {
playerone.takeallthecoins();
changeStage = true;
cantchangerooms = false;
next = true;
devil = false;
}, "3000");
} else if (kb.released("2")) {
changeStage = false;
dialogsplayer.tell("devil", 32);
setTimeout(() => {
changeStage = true;
cantchangerooms = false;
next = true;
devil = false;
}, "3000");
} else if (kb.released("4")) {
changeStage = false;
dialogsplayer.tell("player", 47);
setTimeout(() => {
devilspoke = false;
return talkWithTheDevil();
}, "2000");
}
}
}
function enterTheRoom() {
////////////////Enter the rooms
if (stage == 8 && !cantchangerooms) {
if (playerone.checkPosition(124, 164, 0, 110)) {
stage = 2;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
} else if (playerone.checkPosition(204, 228, 0, 110)) {
if (itemfound[0]) {
stage = 1;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
}
} else if (playerone.checkPosition(268, 284, 0, 110)) {
if (itemfound[2]) {
stage = 4;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
}
} else if (playerone.checkPosition(332, 348, 0, 110)) {
if (itemfound[3]) {
stage = 6;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
}
} else if (playerone.checkPosition(396, 420, 0, 110)) {
if (itemfound[4]) {
stage = 5;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
}
} else if (playerone.checkPosition(452, 484, 0, 110)) {
if (itemfound[5]) {
stage = 3;
coinsBoolean = true;
changeStage = true;
playerone.setPosition(240, 340);
}
}
}
}
function leaveTheRoom() {
//LEAVE THE ROOMs
if (stage != 8 && stage != 7 && stage != 9 && !cantchangerooms) {
if (playerone.checkPosition(200, 260, 370, 420)) {
if (stage == 1) {
playerone.setPosition(220, 110);
} else if (stage == 2) {
playerone.setPosition(140, 110);
} else if (stage == 3) {
playerone.setPosition(460, 110);
} else if (stage == 4) {
playerone.setPosition(270, 110);
} else if (stage == 5) {
playerone.setPosition(400, 110);
} else if (stage == 6) {
playerone.setPosition(340, 110);
}
stage = 8;
changeStage = true;
}
}
}
function entryStage() {
dialogsplayer.tell("petros", 1);
dialogsplayer.tell("player", 2);
dialogsplayer.tell("petros", 3);
setTimeout(() => {
stage = 8;
changeStage = true;
}, "30000");
}
function doorsStage() {
if (!doorsopened[0]) {
doorsopened[0] = true;
cantchangerooms = true;
dialogsplayer.tell("petros", 4);
dialogsplayer.tell("player", 5);
dialogsplayer.tell("petros", 6);
setTimeout(() => {
cantchangerooms = false;
}, "30000");
}
}
function callDoubleClueStage(doornum, cluenum, dialog1, dialog2, cluenum2, dialog3) {
if (!doorsopened[doornum]) {
doorsopened[doornum] = true;
cantchangerooms = true;
dialogsplayer.tell("informer", dialog1);
setTimeout(() => {
cantchangerooms = false;
changeStage = true;
}, "7000");
}
if (!itemfound[cluenum]) {
if (touchedclue[cluenum] && (kb.released('e'))) {
animateItem(cluenum);
cantchangerooms = true;
dialogsplayer.tell("petros", dialog2);
setTimeout(() => {
//player found the key.
cantchangerooms = false;
itemfound[cluenum] = true;
changeStage = true;
}, "6000");
} else {
changeStage = true;
}
}
if (itemfound[cluenum] && !itemfound[cluenum2]) {
if (touchedclue[cluenum2] && (kb.released('e'))) {
animateItem(cluenum2);
cantchangerooms = true;
dialogsplayer.tell("petros", dialog3);
setTimeout(() => {
//player found the key.
cantchangerooms = false;
itemfound[cluenum2] = true;
changeStage = true;
}, "6000");
} else {
changeStage = true;
}
}
}
function calltStage(doornum, cluenum, dialog1, dialog2) {
if (!doorsopened[doornum]) {
doorsopened[doornum] = true;
cantchangerooms = true;
dialogsplayer.tell("informer", dialog1);
setTimeout(() => {
cantchangerooms = false;
changeStage = true;
}, "7000");
}
if (!itemfound[cluenum]) {
if (touchedclue[cluenum] && (kb.released('e'))) {
animateItem(cluenum);
cantchangerooms = true;
dialogsplayer.tell("petros", dialog2);
setTimeout(() => {
//player found the key.
cantchangerooms = false;
itemfound[cluenum] = true;
changeStage = true;
}, "6000");
} else {
changeStage = true;
}
}
}
/**
* Animate clue item. To be called when player interracts with item.
* @param {number} cluenum number of clue to be animated
*/
function animateItem(cluenum){
let itemName=items_names[cluenum];
let item;
for (i of interract_blocks[stage]){
if (i.name.startsWith(itemName)){
item = i;
}
}
item.sprite.changeAnimation("animation");
}