-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogue.js
142 lines (136 loc) · 4.93 KB
/
dialogue.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
class Character {
constructor(source) {
this.image = new Image();
this.image.src = source;
this.image.onload = this.setisloaded;
this.loaded = false;
this.button = document.createElement("button");
this.haseventlistener = false;
}
draw(xoffset, yoffset, scale, canvasobj) {
canvasobj.image(this.image, xoffset, yoffset, this.image.width * scale, this.image.height * scale);
this.button.setAttribute("class", "CanvasArrow CanvasInputElement Invisible");
this.button.style.setProperty("width", (this.image.width * scale)+"px");
this.button.style.setProperty("height", (this.image.height * scale)+"px");
this.button.style.setProperty("left", xoffset+"px");
this.button.style.setProperty("top", yoffset+"px");
}
append(canvasobj) {
if((typeof this.button === "undefined")) {
console.error("Arrow: Object not initialized.");
return;
}
if((typeof canvasobj === "undefined")) {
console.error("Arrow: Argument to function not provided.");
return;
}
canvasobj.canvas.parentElement.appendChild(this.button);
}
deleteButton() {
this.button.remove();
}
setisloaded() {
this.loaded = true;
}
resetEventListeners() {
var newElem = this.button.cloneNode(true);
this.button.parentNode.replaceChild(newElem, this.button);
this.button = newElem;
}
};
let chr = new Character("res/Character.png");
let chrf = new Character("res/CharacterFisher.png");
let chrs = new Character("res/CharacterSitting.png");
let cook = new Character("res/Cook.png");
let traindriver = new Character("res/TrainDriver.png");
let info = new Character("res/StationInfo.png");
let schl = new Character("res/Scholar.png");
let util = new Character("res/UtilityMan.png");
let chse = new Character("res/Cheesemaker.png");
class Dialogue {
constructor() {
this.canvas_info;
this.counter = 0;
this.can_proceed = true;
this.choice_result = -1;
}
begin(canvasobj) {
this.canvas_info = canvasobj;
AllowedToPause = false;
}
makeBox() {
this.canvas_info.setlinethickness(5);
this.canvas_info.boxborder(20, (this.canvas_info.canvas.height * 0.8), this.canvas_info.canvas.width - 40, (this.canvas_info.canvas.height * 0.2));
}
makeText(text) {
this.canvas_info.textml(text, 30, (this.canvas_info.canvas.height * 0.8) + 30);
}
makeBubble(id, text, textcolor = "black") {
if(!(this.can_proceed && this.counter === id)) {
setTimeout(this.makeBubble.bind(this), 100, id, text, textcolor);
return;
}
else { this.can_proceed = false; }
let dlgInputElems = document.getElementsByClassName("DialogueArrow"); //remove all at beginning to avoid duplicates
while(dlgInputElems[0]) {
dlgInputElems[0].parentNode.removeChild(dlgInputElems[0]);
}
this.canvas_info.setnewcolor("white");
this.makeBox();
this.canvas_info.setnewcolor(textcolor);
this.makeText(text);
let NextArrow = new Arrow(this.canvas_info.canvas.width - 140, (this.canvas_info.canvas.height * 0.8) + 10, 100, 100, ArrowDirections.Right, this.canvas_info);
NextArrow.button.setAttribute("class", NextArrow.button.getAttribute("class")+" DialogueArrow");
NextArrow.button.addEventListener("click", () => {
this.counter++;
this.can_proceed = true;
NextArrow.deleteButton();
}, this, { once: true });
}
makeChoice(id) {
if(!(this.can_proceed && this.counter === id)) {
setTimeout(this.makeChoice.bind(this), 100, id);
return;
}
else {
this.can_proceed = false;
}
this.choice_result = -1;
let dlgInputElems = document.getElementsByClassName("DialogueArrow"); //remove all at beginning to avoid duplicates
while(dlgInputElems[0]) {
dlgInputElems[0].parentNode.removeChild(dlgInputElems[0]);
}
this.canvas_info.setnewcolor("white");
this.makeBox();
let YesButton = new Arrow(40, (this.canvas_info.canvas.height * 0.8), 100, 100, ArrowDirections.Yes, this.canvas_info);
let NoButton = new Arrow(this.canvas_info.canvas.width - 140, (this.canvas_info.canvas.height * 0.8), 100, 100, ArrowDirections.No, this.canvas_info);
YesButton.button.setAttribute("class", YesButton.button.getAttribute("class")+" DialogueArrow");
NoButton.button.setAttribute("class", NoButton.button.getAttribute("class")+" DialogueArrow");
YesButton.button.addEventListener("click", () => {
ap.playSFX(1);
this.choice_result = 1;
}, this, { once: true });
NoButton.button.addEventListener("click", () => {
ap.playSFX(2);
this.choice_result = 0;
}, this, { once: true });
let thisInterval = window.setInterval((dialogue) => {
if(this.choice_result !== -1) {
clearInterval(thisInterval);
YesButton.deleteButton();
NoButton.deleteButton();
dialogue.counter++;
dialogue.can_proceed = true;
}
}, 100, this, { once: true });
}
end() {
let dlgInputElems = document.getElementsByClassName("DialogueArrow"); //remove all at beginning to avoid duplicates
while(dlgInputElems[0]) {
dlgInputElems[0].parentNode.removeChild(dlgInputElems[0]);
}
this.canvas_info;
this.counter = 0;
this.can_proceed = false;
}
};