forked from ShardulS18/FinalVersion-terminal-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
574 lines (507 loc) · 14.6 KB
/
script.js
File metadata and controls
574 lines (507 loc) · 14.6 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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Initialize the terminal with cursor options
const terminal = new Terminal({
cursorBlink: true,
cursorStyle: "block",
fontSize: 14,
fontFamily: "monospace",
});
terminal.open(document.getElementById("terminal-container"));
// Display a welcome message
terminal.write(
"Welcome to the dream portal!\r\nIf any doubts refer to the commands given in the hoverbar.\r\nType 'start' to begin.\r\n$ "
);
// Set up a command buffer to capture input
let commandBuffer = "";
let quoteTimer = null;
// Added initial state
let currentScene = "initial";
let images = {};
let currentSound = null;
// Handle key input in the terminal
terminal.onData((data) => {
// Convert the input to a character code
const code = data.charCodeAt(0);
// Check for arrow keys (up/down)
if (data === "\x1B[A" || data === "\x1B[B") {
// Prevent up/down arrow keys from working
return;
}
if (code === 13) {
executeCommand(commandBuffer);
commandBuffer = "";
} else if (code === 127) {
if (commandBuffer.length > 0) {
commandBuffer = commandBuffer.slice(0, -1);
terminal.write("\b \b");
}
} else {
commandBuffer += data;
terminal.write(data);
}
});
// Modified command execution function
function executeCommand(command) {
terminal.write("\r\n");
switch (command.trim().toLowerCase()) {
case "start":
if (currentScene === "initial") {
terminal.write(
"Starting your journey...\r\nUse ls command to list where you can go or what you can use\r\n"
);
showLighthouseScene();
playSound("ocean_waves");
}
break;
case "whoami":
if (currentScene === "lighthouse-moved") {
terminal.write("Initiating self-discovery...\r\n");
showQuote();
}
break;
case "ls":
switch (currentScene) {
case "lighthouse-moved":
terminal.write(
"You are in a dream type 'whoami' command to proceed.\r\n"
);
break;
case "bedroom":
terminal.write(
"Available options:\r\n- dining room (directory)\r\n- washroom (directory)\r\n- phone (file)\r\n"
);
break;
case "dining-room":
terminal.write("Need to brush teeth first.\r\n");
break;
case "washroom":
terminal.write("Available options:\r\n- brush teeth (file)\r\n");
break;
default:
terminal.write("No options available.\r\n");
}
break;
case "cat phone":
if (currentScene === "bedroom") {
loadPhoneScene();
playSound("phone_pickup");
}
break;
case "q":
if (currentScene === "phone") {
returnToBedroomScene();
}
break;
case "cd dining room":
if (currentScene === "bedroom") {
loadDiningRoomScene();
playSound("footsteps_level0");
}
break;
case "cd washroom":
if (currentScene === "bedroom") {
loadWashroomScene();
playSound("footsteps_to_washroom");
playSound("water_running");
}
break;
case "cat brush teeth":
if (currentScene === "washroom") {
loadPortalScene();
playSound("portal_opening");
}
break;
case "cd ..":
if (["dining-room"].includes(currentScene)) {
returnToBedroomScene();
playSound("footsteps_level0");
} else if (["washroom"].includes(currentScene)) {
returnToBedroomScene();
playSound("footsteps_to_washroom");
}
break;
case "clear":
terminal.clear();
terminal.scrollToTop();
terminal.write(
"Welcome to the dream portal!\r\nIf any doubts refer to the commands given in the hoverbar."
);
break;
default:
terminal.write(`Command not found: ${command}\r\n`);
}
terminal.write("\r\n$ ");
}
// Initialize PIXI application and load assets
const app = new PIXI.Application();
app.init({ background: "#000000", resizeTo: window }).then(async () => {
document.getElementById("pixi-container").appendChild(app.view);
// Load all required textures
const textures = await loadTextures();
createSprites(textures);
});
async function loadTextures() {
return {
lighthouse: await PIXI.Assets.load("Lighthouse.svg"),
background: await PIXI.Assets.load("Background.svg"),
boy: await PIXI.Assets.load("Boy.svg"),
clouds: await PIXI.Assets.load("Clouds.svg"),
rock: await PIXI.Assets.load("Rock.svg"),
backgroundrock: await PIXI.Assets.load("BackgroundRock.svg"),
backgroundrock2: await PIXI.Assets.load("BackgroundRock2.svg"),
bedroom: await PIXI.Assets.load("BedroomScene.svg"),
phone: await PIXI.Assets.load("PhoneScene.svg"),
diningRoom: await PIXI.Assets.load("DiningRoom.svg"),
washroom: await PIXI.Assets.load("WashroomScene.svg"),
portal: await PIXI.Assets.load("PortalScene.svg"),
quote: await PIXI.Assets.load("QuoteScene.svg"),
bedroomObjective: await PIXI.Assets.load("bedroomObjective.svg"),
returnBedroom: await PIXI.Assets.load("BedroomScene2.svg"),
};
}
function createSprites(textures) {
// Create sprites for each texture with default properties
Object.keys(textures).forEach((key) => {
const sprite = new PIXI.Sprite(textures[key]);
sprite.alpha = 0; // Only set alpha as a default
app.stage.addChild(sprite);
images[key] = sprite;
});
// Configure individual sprites with specific properties
// Background
images.background.anchor.set(0.5);
images.background.scale.set(1.0);
images.background.x = app.screen.width / 3;
images.background.y = app.screen.height / 2;
images.background.zIndex = 0;
// Lighthouse
images.lighthouse.anchor.set(0.2);
images.lighthouse.scale.set(0.7);
images.lighthouse.x = app.screen.width / 13;
images.lighthouse.y = app.screen.height / 5;
images.lighthouse.zIndex = 2;
// Background rock
images.backgroundrock.anchor.set(0.5);
images.backgroundrock.scale.set(1.0);
images.backgroundrock.x = app.screen.width / 4;
images.backgroundrock.y = app.screen.height / 1.63;
images.backgroundrock.zIndex = 1;
// Boy
images.boy.anchor.set(0.45);
images.boy.x = app.screen.width / 2;
images.boy.y = app.screen.height / 2;
images.boy.zIndex = 3;
// Clouds
images.clouds.anchor.set(0.5);
images.clouds.x = app.screen.width / 2.5;
images.clouds.y = app.screen.height / 2.5;
images.clouds.zIndex = 0;
// Rock
images.rock.anchor.set(0.45);
images.rock.x = app.screen.width / 20;
images.rock.y = app.screen.height / 1.1;
images.rock.zIndex = 3;
// Background rock 2
images.backgroundrock2.anchor.set(0.45);
images.backgroundrock2.x = app.screen.width / 10;
images.backgroundrock2.y = app.screen.height / 1.1;
images.backgroundrock2.zIndex = 2;
//Bedroom Objective
images.bedroomObjective.anchor.set(0.5);
images.bedroomObjective.scale.set(1.0);
images.bedroomObjective.x = app.screen.width / 1;
images.bedroomObjective.y = app.screen.height / 15;
images.bedroomObjective.alpha = 0;
images.bedroomObjective.zIndex = 0;
// Scene sprites (bedroom, phone, dining room, etc.)
const sceneSprites = [
"bedroom",
"phone",
"diningRoom",
"washroom",
"portal",
"quote",
"returnBedroom",
];
sceneSprites.forEach((key) => {
if (images[key]) {
images[key].anchor.set(0.5);
images[key].x = app.screen.width / 3;
images[key].y = app.screen.height / 2;
images[key].zIndex = 0;
}
});
}
async function initializeSounds() {
const backgroundMusic = document.getElementById("backgroundMusic0");
// Optional: Set volume
backgroundMusic.volume = 0.1;
try {
// Play the background music
await backgroundMusic.play();
} catch (error) {
console.error("Error playing background music:", error);
}
}
function playSound(soundId) {
// Stop the currently playing sound, if any
if (currentSound) {
currentSound.pause();
currentSound.currentTime = 0; // Reset to the beginning
}
// Play the new sound
currentSound = document.getElementById(soundId);
if (currentSound) {
try {
currentSound.play();
} catch (error) {
console.error(`Error playing sound ${soundId}:`, error);
}
}
}
function showLighthouseScene() {
currentScene = "lighthouse";
// Fade in lighthouse scene elements
Object.values(images).forEach((sprite) => {
sprite.alpha = 0;
});
const lighthouseElements = [
"lighthouse",
"background",
"boy",
"clouds",
"backgroundrock",
"rock",
"backgroundrock2",
];
lighthouseElements.forEach((element) => {
if (images[element]) {
gsap.to(images[element], {
alpha: 1,
duration: 1,
ease: "power1.inOut",
});
}
});
// Schedule the transition after 3 seconds
setTimeout(() => {
if (currentScene === "lighthouse") {
animateTransition();
}
}, 3000);
}
function showQuote() {
currentScene = "quote";
// Fade out lighthouse scene elements
[
"lighthouse",
"background",
"boy",
"clouds",
"backgroundrock",
"rock",
"backgroundrock2",
].forEach((element) => {
if (images[element]) {
gsap.to(images[element], {
alpha: 0,
duration: 1,
ease: "power1.inOut",
});
}
});
// Position and fade in the quote image
if (images.quote) {
images.quote.anchor.set(0.5);
images.quote.x = app.screen.width / 3;
images.quote.y = app.screen.height / 2;
// Fade in quote
gsap.to(images.quote, {
alpha: 1,
duration: 1,
ease: "power1.inOut",
});
// Schedule quote fadeout and bedroom scene transition
setTimeout(() => {
if (currentScene === "quote") {
// Fade out quote
gsap.to(images.quote, {
alpha: 0,
duration: 1,
ease: "power1.inOut",
onComplete: () => {
loadBedroomScene();
},
});
}
}, 5000); // Show quote for 5 seconds
}
}
function animateTransition() {
if (currentScene === "lighthouse") {
gsap.to([images.clouds, images.lighthouse], { x: "-=40", duration: 3 });
gsap.to([images.boy, images.rock], { x: "+=40", duration: 3 });
currentScene = "lighthouse-moved";
}
}
function loadBedroomScene() {
playSound("alarm_clock");
initializeSounds();
currentScene = "bedroom";
// First fade out all other scenes
const otherScenes = ["phone", "diningRoom", "washroom", "portal"];
otherScenes.forEach((scene) => {
if (images[scene]) {
gsap.to(images[scene], {
alpha: 0,
duration: 1,
});
}
});
// Then fade in bedroom
gsap.to(images.bedroom, {
alpha: 1,
duration: 1,
onComplete: () => {
// After bedroom is fully loaded, wait 3 seconds then animate overlay
setTimeout(() => {
if (currentScene === "bedroom" && images.bedroomObjective) {
// Position the overlay off-screen to the left
images.bedroomObjective.x = -images.bedroomObjective.width;
images.bedroomObjective.alpha = 1;
// Animate it sliding in from the left
gsap.to(images.bedroomObjective, {
x: app.screen.width / 6, // Match the position of other scenes
duration: 1.5,
ease: "power2.out",
});
}
}, 3000);
},
});
}
function returnToBedroomScene() {
currentScene = "bedroom"; // Keep the same scene state as regular bedroom
// Fade out all other scenes including the main bedroom
const otherScenes = ["phone", "diningRoom", "washroom", "portal", "bedroom"];
otherScenes.forEach((scene) => {
if (images[scene]) {
gsap.to(images[scene], {
alpha: 0,
duration: 1,
});
}
});
// Fade in returnBedroom and objective overlay
gsap.to([images.returnBedroom, images.bedroomObjective], {
alpha: 1,
duration: 1,
});
// Update the bedroom sprite reference to use returnBedroom
images.bedroom = images.returnBedroom;
}
function loadPhoneScene() {
currentScene = "phone";
// First fade out bedroom completely
gsap.to([images.bedroom, images.bedroomObjective], {
alpha: 0,
duration: 1,
onComplete: () => {
// Then fade in phone
gsap.to(images.phone, {
alpha: 1,
duration: 1,
});
},
});
}
function loadDiningRoomScene() {
currentScene = "dining-room";
// Fade out both bedroom and bedroomOverlay simultaneously
gsap.to([images.bedroom, images.bedroomObjective], {
alpha: 0,
duration: 1,
});
gsap.to(images.diningRoom, {
alpha: 1,
duration: 1,
});
}
function loadWashroomScene() {
currentScene = "washroom";
// Fade out both bedroom and bedroomOverlay simultaneously
gsap.to([images.bedroom, images.bedroomObjective], {
alpha: 0,
duration: 1,
});
gsap.to(images.washroom, {
alpha: 1,
duration: 1,
});
}
function loadPortalScene() {
currentScene = "portal";
console.log("Transitioning to portal scene...");
// Fade out washroom
gsap.to(images.washroom, {
alpha: 0,
duration: 1,
onComplete: () => {
// Fade in background first, then start animation
gsap.to(images.portal, {
alpha: 1,
duration: 0.5,
onComplete: startPortalAnimation,
});
},
});
terminal.write("WHOAAAAAAA\r\n");
}
function startPortalAnimation() {
let currentFrame = 1;
const totalFrames = 5;
const frameDuration = 0.5; // Duration each frame is shown in seconds
// Show first frame
gsap.to(images[`portal${currentFrame}`], {
alpha: 1,
duration: 0.5,
});
// Function to handle frame transitions
function nextFrame() {
if (currentFrame <= totalFrames) {
// Fade out current frame
gsap.to(images[`portal${currentFrame}`], {
alpha: 0,
duration: frameDuration / 2,
});
currentFrame++;
if (currentFrame <= totalFrames) {
// Fade in next frame
gsap.to(images[`portal${currentFrame}`], {
alpha: 1,
duration: frameDuration / 2,
});
// Schedule next frame
setTimeout(nextFrame, frameDuration * 1000);
} else {
// Fade out background and transition to next level
gsap.to(images.portalBackground, {
alpha: 0,
duration: frameDuration,
onComplete: () => {
window.location.href = "console2.html";
},
});
}
}
}
// Start the frame animation after showing first frame
setTimeout(nextFrame, frameDuration * 1000);
}
window.addEventListener("resize", () => {
app.renderer.resize(window.innerWidth, window.innerHeight);
// Update sprite positions
Object.values(images).forEach((sprite) => {
sprite.x = app.screen.width / 2;
sprite.y = app.screen.height / 2;
});
});