-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNpc.js
More file actions
77 lines (72 loc) · 2.56 KB
/
Npc.js
File metadata and controls
77 lines (72 loc) · 2.56 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
class Npc extends InteractiveLevelObject {
constructor(x, y, tileSize, type, tilemapHandler, extraAttributes = {}) {
super(x, y, tileSize, type, 0, extraAttributes);
this.upReleased = true;
this.key = this.makeid(5);
this.arrowUpFrameIndex = 0;
this.upButtonReleased = false;
this.collidedWithPlayer = false;
if(!this.avatars && this.dialogue.length > 0) {
this.avatars = [];
this.dialogue.forEach(_ => {
this.avatars.push(null);
});
}
}
resetObject() {
this.collidedWithPlayer = false;
}
collisionEvent() {
if(this.playAutomatically) {
if(!this.collidedWithPlayer) {
this.collidedWithPlayer = true;
player.collidingWithNpcId = this.key;
this.startDialogue();
}
}
else {
player.collidingWithNpcId = this.key;
this.arrowUpFrameIndex++;
const frameModulo = this.arrowUpFrameIndex % 60;
if (frameModulo < 30) {
DialogueHandler.showDialogueUpArrow(this.x, this.y - this.tileSize);
}
if (!Controller.jump) {
this.upButtonReleased = true;
}
else {
if (this.upButtonReleased && !DialogueHandler.active) {
this.startDialogue();
}
this.upButtonReleased = false;
}
}
}
startDialogue() {
const parsedDialogue = [];
this.dialogue.forEach((singleDialogue, index) => {
const singleDialogueObject = DialogueHandler.createDialogObject(singleDialogue, this?.avatars?.[index]);
if (singleDialogueObject.textLength > 0) {
parsedDialogue.push(singleDialogueObject);
}
});
if (parsedDialogue.length > 0) {
DialogueHandler.dialogue = parsedDialogue;
DialogueHandler.active = true;
DialogueHandler.calculateDialogueWindowPosition();
DialogueHandler.soundPlaying = true;
SoundHandler.dialogueSound.stopAndPlay();
player.fixedSpeed = false;
player.xspeed = 0;
player.yspeed = 0;
}
}
draw(spriteCanvas) {
if (player.collidingWithNpcId === this.key && !Collision.objectsColliding(player, this)) {
player.collidingWithNpcId = null;
this.arrowUpFrameIndex = 0;
this.upButtonReleased = false;
}
super.draw(spriteCanvas);
}
}