-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
681 lines (547 loc) · 20.4 KB
/
Copy pathscript.js
File metadata and controls
681 lines (547 loc) · 20.4 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
"use strict";
// Initialize question counter display on game load
document.addEventListener("DOMContentLoaded", () => {
const questionCounterElement = document.getElementById('question-counter');
const questionCounter = sessionStorage.getItem('questionCounter') || 0;
questionCounterElement.innerText = `Questions Asked: ${questionCounter}`;
});
document.addEventListener("DOMContentLoaded", () => {
// Initialize question counter display
const questionCounterElement = document.getElementById('question-counter');
const questionCounter = sessionStorage.getItem('questionCounter') || 0;
questionCounterElement.innerText = `Questions Asked: ${questionCounter}`;
// Add functionality to the home icon
const homeIcon = document.getElementById('home-icon');
homeIcon.addEventListener('click', () => {
location.reload(true);
// Reset the question counter
sessionStorage.setItem('questionCounter', '0');
// Redirect to index.html
window.location.href = 'index.html';
});
});
document.addEventListener("DOMContentLoaded", () => {
// Add event listener to exclamation-icon
const exclamationIcon = document.getElementById("exclamation-icon");
exclamationIcon.addEventListener("click", () => {
showDecisionScreen();
});
});
class GameScene extends Phaser.Scene {
constructor() {
super({ key: "GameScene" });
}
preload() {
this.load.tilemapTiledJSON("map", "tileset/outsidemap.json");
this.load.image("outside1", "tileset/outside1.png");
this.load.image("outside2", "tileset/outside2.png");
this.load.image("outside3", "tileset/outside3.png");
var url =
"https://cdn.jsdelivr.net/gh/rexrainbow/phaser3-rex-notes/dist/rexvirtualjoystickplugin.min.js";
this.load.plugin("rexvirtualjoystickplugin", url, true);
// Load game assets
this.load.image("player", "sprites/player1sprite.png");
this.load.image("npc", "sprites/girl1sprite.png");
this.load.image("npc2", "sprites/guy1sprite.png");
this.load.image("guy1", "sprites/guy1.png");
this.load.image("girl1", "sprites/girl1.png");
}
create() {
// Create the map
const map = this.make.tilemap({ key: "map" });
const tileset1 = map.addTilesetImage("outside1", "outside1");
const tileset2 = map.addTilesetImage("outside2", "outside2");
const tileset3 = map.addTilesetImage("outside3", "outside3");
const groundLayer = map.createLayer("ground", [tileset1, tileset2], 0, 0);
const passibleLayer = map.createLayer("passible", [tileset3], 0, 0);
const collisionLayer = map.createLayer("collision", [tileset3], 0, 0);
groundLayer.setDepth(0);
passibleLayer.setDepth(2); // Passible layer appears above the player
collisionLayer.setDepth(1);
collisionLayer.setCollisionByExclusion([-1]);
//console.log("Collision Layer: ", collisionLayer);
if (!collisionLayer) {
console.error("Collision layer is undefined. Check your map JSON or Phaser initialization.");
return;
}
const worldWidth = this.scale.gameSize.width;
const worldHeight = this.scale.gameSize.height;
// Add virtual joystick
this.joyStick = this.plugins.get("rexvirtualjoystickplugin").add(this, {
x: 100,
y: worldHeight - 150,
radius: 50,
base: this.add.circle(0, 0, 50, 0x888888, 0.8).setDepth(100), // Set depth for base
thumb: this.add.circle(0, 0, 20, 0xcccccc, 0.8).setDepth(100), // Set depth for thumb
});
// Adjust the camera to follow the player
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.physics.world.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// Add "Talk" button
this.talkButton = this.add
.text(200, worldHeight - 150, "Talk", {
font: "20px Arial",
fill: "#ffffff",
backgroundColor: "#444444",
padding: { x: 15, y: 5 },
})
.setInteractive()
.setVisible(false)
.on("pointerdown", () => this.onTalkButtonPressed())
.setDepth(1000);
const sayButton = document.getElementById("say-button");
const leaveButton = document.getElementById("leave-button");
// Add "Say" button
this.sayButton = this.add
.text(worldWidth / 2, worldHeight - 200, "Say", {
font: "20px Arial",
fill: "#ffffff",
backgroundColor: "#444444",
padding: { x: 10, y: 5 },
})
.setInteractive()
.setOrigin(0.5)
.setDepth(10) // Ensure it's above the dialogue overlay
.setVisible(false)
.on("pointerdown", () => this.onSayButtonPressed());
// Add "Leave" button
this.leaveButton = this.add
.text(worldWidth - 150, worldHeight - 150, "Leave", {
font: "20px Arial",
fill: "#ffffff",
backgroundColor: "#444444",
padding: { x: 10, y: 5 },
})
.setInteractive()
.setDepth(10) // Ensure it's above the dialogue overlay
.setVisible(false)
.on("pointerdown", () => this.endConversation());
// Say button triggers sending a message
sayButton.addEventListener("click", () => this.onSayButtonPressed());
// Leave button exits the conversation
leaveButton.addEventListener("click", () => this.endConversation());
// Set world and camera bounds
this.cameras.main.setBounds(0, 0, worldWidth, worldHeight);
this.physics.world.setBounds(0, 0, worldWidth, worldHeight);
// Add player and NPCs
this.player = this.physics.add
.sprite(worldWidth / 2, worldHeight / 2, "player")
.setScale(1.5);
this.physics.add.collider(this.player, collisionLayer);
this.physics.add.collider(this.player, collisionLayer);
const getRandomPosition = (dimension, padding = 50) =>
Phaser.Math.Between(padding, dimension - padding);
/*
this.npc = this.physics.add
.sprite(
getRandomPosition(worldWidth),
getRandomPosition(worldHeight),
"npc",
)
.setScale(1.5);
this.npc2 = this.physics.add
.sprite(
getRandomPosition(worldWidth),
getRandomPosition(worldHeight),
"npc2",
)
.setScale(1.5);
*/
this.npc = this.physics.add
.sprite(
64,
162,
"npc",
)
.setScale(1.5);
this.npc2 = this.physics.add
.sprite(
256,
640,
"npc2",
)
.setScale(1.5);
// Make NPCs immovable and enable collision
this.npc.setCollideWorldBounds(true).setImmovable(true);
this.npc2.setCollideWorldBounds(true).setImmovable(true);
this.player.setCollideWorldBounds(true);
this.physics.add.collider(this.npc, collisionLayer);
this.physics.add.collider(this.npc2, collisionLayer);
// Prevent NPCs from passing through each other
this.physics.add.collider(this.npc, this.npc2);
// Use handleCollision method for player-NPC collisions
this.physics.add.collider(
this.player,
this.npc,
this.handleCollision,
null,
this,
);
this.physics.add.collider(
this.player,
this.npc2,
this.handleCollision,
null,
this,
);
// DOM elements for dialogue overlay
this.dialogueOverlay = document.getElementById("dialogue-overlay");
this.npcDialogue = document.getElementById("npc-dialogue");
this.playerDialogue = document.getElementById("player-dialogue");
this.playerInput = document.getElementById("player-input");
// Center the input box at the bottom
this.playerInput.style.position = "absolute";
this.playerInput.style.bottom = "50px";
this.playerInput.style.left = "50%";
this.playerInput.style.transform = "translateX(-50%)";
this.playerInput.style.display = "none";
document
.getElementById("player-input")
.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
this.onSayButtonPressed();
}
});
// Make overlay full screen
this.dialogueOverlay.style.width = "100vw";
this.dialogueOverlay.style.height = "100vh";
this.dialogueOverlay.style.position = "absolute";
this.dialogueOverlay.style.top = "0";
this.dialogueOverlay.style.left = "0";
this.dialogueOverlay.style.display = "none";
// NPC Movement Timer
this.npcMoveTimer = this.time.addEvent({
delay: 1500,
callback: this.moveNPCs,
callbackScope: this,
loop: true,
});
}
update() {
const force = this.joyStick.force;
const angle = this.joyStick.angle;
if (force > 0) {
const maxSpeed = 75;
const radian = Phaser.Math.DegToRad(angle);
const velocityX = Math.cos(radian) * force * maxSpeed;
const velocityY = Math.sin(radian) * force * maxSpeed;
this.player.setVelocityX(
Phaser.Math.Clamp(velocityX, -maxSpeed, maxSpeed),
);
this.player.setVelocityY(
Phaser.Math.Clamp(velocityY, -maxSpeed, maxSpeed),
);
} else {
this.player.setVelocity(0);
}
this.checkProximityToNPCs();
}
handleCollision(player, npc) {
// Stop the player and NPC's velocity on collision
player.body.setVelocity(0);
npc.body.setVelocity(0);
}
moveNPCs() {
const randomSpeed = () => Phaser.Math.Between(-50, 50);
this.npc.setVelocity(randomSpeed(), randomSpeed());
this.npc2.setVelocity(randomSpeed(), randomSpeed());
}
checkProximityToNPCs() {
const distanceToNPC1 = Phaser.Math.Distance.Between(
this.player.x,
this.player.y,
this.npc.x,
this.npc.y,
);
const distanceToNPC2 = Phaser.Math.Distance.Between(
this.player.x,
this.player.y,
this.npc2.x,
this.npc2.y,
);
if (distanceToNPC1 < 70) {
this.talkButton.setVisible(true);
this.showTalkButtonWithNPCImage("girl1");
} else if (distanceToNPC2 < 70) {
this.talkButton.setVisible(true);
this.showTalkButtonWithNPCImage("guy1");
} else {
this.talkButton.setVisible(false);
this.hideNPCImage();
}
}
showTalkButtonWithNPCImage(npcImageKey) {
const buttonX = this.talkButton.x;
const buttonY = this.talkButton.y;
if (!this.npcImage) {
// Create the NPC image and place it next to the button
this.npcImage = this.add
.image(buttonX - 70, buttonY, npcImageKey) // Position the image relative to the button
.setScale(1)
.setOrigin(1)
.setDepth(1000);
} else {
// Update the NPC image texture and position
this.npcImage.setTexture(npcImageKey);
this.npcImage.setPosition(buttonX + 65, buttonY); // Dynamically update position
this.npcImage.setVisible(true);
}
}
hideNPCImage() {
if (this.npcImage) {
this.npcImage.setVisible(false);
}
}
onTalkButtonPressed() {
const closestNPC =
Phaser.Math.Distance.Between(
this.player.x,
this.player.y,
this.npc.x,
this.npc.y,
) <
Phaser.Math.Distance.Between(
this.player.x,
this.player.y,
this.npc2.x,
this.npc2.y,
)
? this.npc
: this.npc2;
const npcType = closestNPC === this.npc ? "charlotte" : "gary";
this.currentNPCType = npcType; // Set the current NPC type
const npcImageSrc =
closestNPC === this.npc ? "sprites/girl1.png" : "sprites/guy1.png";
const npcDialogueText =
closestNPC === this.npc ? "Hello there!" : "What's up?";
this.startConversation(npcImageSrc, npcDialogueText);
}
async onSayButtonPressed() {
const playerInput = this.playerInput.value.trim();
if (playerInput === "") return;
this.playerDialogue.innerText = playerInput;
this.playerInput.value = "";
this.npcDialogue.innerText = "...";
await sendChatMessage(playerInput, this.currentNPCType);
}
endConversation() {
//console.log("endConversation called");
this.inConvo = false;
// Hide dialogue elements
this.dialogueOverlay.style.display = "none";
this.npcDialogue.innerText = "";
this.playerDialogue.innerText = "";
this.playerInput.value = "";
this.playerInput.style.display = "none";
// Enable joystick and hide buttons
this.joyStick.base.setVisible(true);
this.joyStick.thumb.setVisible(true);
this.talkButton.setVisible(true);
this.npcMoveTimer.paused = false;
this.resetCameraToTop();
}
// Helper method to reset the camera position
resetCameraToTop() {
// console.log("camera reset");
// Reset Phaser camera scroll
this.cameras.main.scrollX = 0;
this.cameras.main.scrollY = 0;
// Reset browser viewport scroll to the top
window.scrollTo(0, 0);
}
startConversation(npcImageSrc, npcDialogueText) {
this.inConvo = true;
// Stop NPC movement
this.npc.setVelocity(0, 0);
this.npc2.setVelocity(0, 0);
this.npcMoveTimer.paused = true;
// Hide joystick and Talk button
this.joyStick.base.setVisible(false);
this.joyStick.thumb.setVisible(false);
this.talkButton.setVisible(false);
// Show Say and Leave buttons
this.sayButton.setVisible(false);
this.leaveButton.setVisible(false);
// Display dialogue
this.dialogueOverlay.style.display = "flex";
document.getElementById("npc-image").src = npcImageSrc;
this.npcDialogue.innerText = npcDialogueText;
this.playerInput.style.display = "block";
this.playerInput.focus();
}
async onSayButtonPressed() {
const playerInput = this.playerInput.value.trim();
if (playerInput === "") return;
this.playerDialogue.innerText = playerInput;
this.playerInput.value = "";
this.npcDialogue.innerText = "...";
await sendChatMessage(playerInput, this.currentNPCType);
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 450,
physics: {
default: "arcade",
arcade: { debug: false },
},
scene: GameScene,
backgroundColor: "#333333",
scale: {
mode: Phaser.Scale.RESIZE,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
};
let secrets = null; // Cache the secrets after fetching
async function fetchSecrets() {
if (!secrets) {
const response = await fetch("/secrets");
if (!response.ok) {
throw new Error("Failed to fetch secrets");
}
secrets = await response.json(); // Parse response as JSON
}
return secrets;
}
let sessionId = localStorage.getItem("sessionId");
if (!sessionId) {
sessionId = crypto.randomUUID(); // Generate a unique session ID
localStorage.setItem("sessionId", sessionId);
}
async function sendChatMessage(playerInput, npcType) {
const questionCounterElement = document.getElementById('question-counter');
const questionCounter = sessionStorage.getItem('questionCounter') || 0;
if (parseInt(questionCounter) >= 20) {
showDecisionScreen();
return;
}
try {
const response = await fetch("https://api-call.stuartvinton.workers.dev/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input: playerInput, npc: npcType, sessionId: sessionId }),
});
if (!response.ok) {
throw new Error("Failed to fetch response from the server");
}
const data = await response.json();
const npcContent = data.message || "No response available";
// Update NPC dialogue
document.getElementById("npc-dialogue").innerText = npcContent;
// Increment and store the counter
const updatedCounter = parseInt(questionCounter) + 1;
sessionStorage.setItem('questionCounter', updatedCounter);
console.log(updatedCounter);
questionCounterElement.innerText = `Questions Asked: ${updatedCounter}`;
if(parseInt(questionCounter) >= 2){
document.querySelector('#exclamation-icon').style.display = 'block';
}
else{
document.querySelector('#exclamation-icon').style.display = 'none';
}
if (updatedCounter >= 20) {
playerInput.style.display = 'none';
showDecisionScreen();
}
} catch (error) {
console.error("Error sending chat message:", error);
document.getElementById("npc-dialogue").innerText =
"There was an error communicating with the NPC.";
}
}
function showDecisionScreen() {
const decisionOverlay = document.createElement('div');
decisionOverlay.id = 'decision-overlay';
decisionOverlay.style.position = 'absolute';
decisionOverlay.style.top = 0;
decisionOverlay.style.left = 0;
decisionOverlay.style.width = '100%';
decisionOverlay.style.height = '100%';
decisionOverlay.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
decisionOverlay.style.display = 'flex';
decisionOverlay.style.flexDirection = 'column';
decisionOverlay.style.justifyContent = 'center';
decisionOverlay.style.alignItems = 'center';
decisionOverlay.style.zIndex = 9999;
// Add NPC choices
decisionOverlay.innerHTML = `
<h1 style="color: white; font-family: Arial, sans-serif; margin-bottom: 20px;">Who ate Grandad's cake?</h1>
<div style="display: flex; justify-content: center; gap: 50px;">
<img src="sprites/girl1.png" id="npc-charlotte" style="cursor: pointer; width: 150px; height: auto; border-radius: 10px;" />
<img src="sprites/guy1.png" id="npc-gary" style="cursor: pointer; width: 150px; height: auto; border-radius: 10px;" />
</div>
<p id="result-message" style="color: white; font-family: Arial, sans-serif; margin-top: 20px; text-align: center;"></p>
`;
document.body.appendChild(decisionOverlay);
// Disable further clicks after guessing
let hasGuessed = false;
// Handle guesses
const handleGuess = async (npc) => {
if (hasGuessed) return; // Prevent multiple guesses
hasGuessed = true;
// Disable both NPC icons
document.getElementById('npc-charlotte').style.pointerEvents = 'none';
document.getElementById('npc-gary').style.pointerEvents = 'none';
// Show a loading message
const resultMessage = document.getElementById('result-message');
resultMessage.textContent = 'Checking your answer...';
try {
// Make the API call
const response = await fetch('https://api-call.stuartvinton.workers.dev/guess', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ npc, sessionId }),
});
if (!response.ok) {
throw new Error('Failed to submit your guess.');
}
const data = await response.json();
// Show the result from the API
resultMessage.textContent = data.message;
} catch (error) {
console.error('Error making guess:', error);
resultMessage.textContent = 'An error occurred. Please try again later.';
}
};
// Add event listeners to NPC icons
document.getElementById('npc-charlotte').addEventListener('click', () => handleGuess('charlotte'));
document.getElementById('npc-gary').addEventListener('click', () => handleGuess('gary'));
const restartButton = document.createElement('button');
restartButton.textContent = 'Restart';
restartButton.style.marginTop = '20px';
restartButton.style.padding = '10px 20px';
restartButton.style.fontSize = '16px';
restartButton.style.cursor = 'pointer';
restartButton.addEventListener('click', () => {
window.location.reload(); // Reset the game
sessionStorage.setItem('questionCounter', '0');
window.location.href = 'index.html';
});
decisionOverlay.appendChild(restartButton);
}
async function makeGuess(npc) {
try {
const response = await fetch('https://api-call.stuartvinton.workers.dev/guess', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ npc, sessionId }),
});
if (!response.ok) {
throw new Error("Failed to send guess.");
}
const data = await response.json();
alert(data.message); // Show feedback from the server
window.location.reload(); // Reset the game
} catch (error) {
console.error('Error making guess:', error);
}
}
/*
window.onload = function() {
sessionStorage.setItem('questionCounter', '0');
const questionCounterElement = document.getElementById('question-counter');
questionCounterElement.innerText = 'Questions Asked: 0';
};
*/
const game = new Phaser.Game(config);