-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnswengine.js
More file actions
250 lines (229 loc) · 6.25 KB
/
nswengine.js
File metadata and controls
250 lines (229 loc) · 6.25 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
import Debug from "./debug.js";
import { isColor } from "./utilities.js";
export default class NSWEngine {
#dataFilePath = "./data.json";
#imagesPath = "";
#images = [];
#soundsPath = "";
#sounds = [];
#musicTracksPath = "";
#musicTracks = [];
#defaultFont = "Arial";
#defaultBackgroundColor = "cornflowerblue";
#defaultTextColor = "white";
#defaultDescription = "not defined";
#defaultExitLabel = "not defined";
constructor() {
this.addButtonOnClickEvents();
}
get dataFilePath() {
return this.#dataFilePath;
}
get imagesPath() {
return this.#imagesPath;
}
get images() {
return this.#images;
}
get soundsPath() {
return this.#soundsPath;
}
get sounds() {
return this.#sounds;
}
get musicTracksPath() {
return this.#musicTracksPath;
}
get musicTracks() {
return this.#musicTracks;
}
get defaultFont() {
return this.#defaultFont;
}
get defaultBackgroundColor() {
return this.#defaultBackgroundColor;
}
get defaultTextColor() {
return this.#defaultTextColor;
}
get defaultDescription() {
return this.#defaultDescription;
}
get defaultExitLabel() {
return this.#defaultExitLabel;
}
get directions() {
return {
n: "north",
s: "south",
w: "west",
e: "east",
};
}
get allDirections() {
return [
this.directions.n,
this.directions.s,
this.directions.w,
this.directions.e,
];
}
get currentRoom() {
return this.data.rooms.find((room) => room.id === this.currentRoomId);
}
get currentTextColor() {
const roomTextColor = this.currentRoom.textColor;
if (roomTextColor === undefined) {
return this.defaultTextColor;
}
if (!isColor(roomTextColor)) {
Debug.log(
`room ${this.currentRoomId}: \'${roomTextColor}\' is not a valid textColor`,
);
return this.defaultTextColor;
}
return roomTextColor;
}
get currentBackgroundColor() {
const roomBackgroundColor = this.currentRoom.backgroundColor;
if (roomBackgroundColor === undefined) {
return this.defaultBackgroundColor;
}
if (!isColor(roomBackgroundColor)) {
Debug.log(
`room ${this.currentRoomId}: \'${roomBackgroundColor}\' is not a valid backgroundColor`,
);
return this.defaultBackgroundColor;
}
return roomBackgroundColor;
}
get currentDescription() {
const roomDescription = this.currentRoom.description;
if (roomDescription === undefined) {
Debug.log(`room ${this.currentRoomId}: no room description found`);
return this.defaultDescription;
}
return roomDescription;
}
get currentFont() {
const roomFont = this.currentRoom.font;
if (roomFont === undefined) {
return this.defaultFont;
}
return roomFont;
}
getExitButton(direction) {
return document.querySelector(`#${direction}-exit-button`);
}
getExitLabel(direction) {
const label = this.currentRoom[direction].label;
if (label === "") {
return label;
}
return label ? label : this.defaultExitLabel;
}
start(game) {
this.game = game;
this.data = game.content.data;
this.currentRoomId = 0;
this.displayCurrentRoom();
}
addButtonOnClickEvents() {
this.allDirections.forEach((direction) => {
const exitButton = this.getExitButton(direction);
exitButton.onclick = () => {
this.go(direction);
};
});
}
update(game) {
this.handleKeyboardInput(game);
}
handleKeyboardInput(game) {
const input = game.input;
if (input.isKeyPressed(input.keys.UP) || input.isKeyPressed(input.keys.W)) {
this.go(this.directions.n);
}
if (
input.isKeyPressed(input.keys.DOWN) ||
input.isKeyPressed(input.keys.S)
) {
this.go(this.directions.s);
}
if (
input.isKeyPressed(input.keys.LEFT) ||
input.isKeyPressed(input.keys.A)
) {
this.go(this.directions.w);
}
if (
input.isKeyPressed(input.keys.RIGHT) ||
input.isKeyPressed(input.keys.D)
) {
this.go(this.directions.e);
}
}
go(direction) {
if (!this.currentRoom[direction]) {
return;
}
if (this.currentRoom[direction].linkTo === undefined) {
Debug.log(`no \'linkTo\' provided for \'${direction}\' exit`);
return;
}
this.currentRoomId = this.currentRoom[direction].linkTo;
this.displayCurrentRoom();
Debug.log(`entering room ${this.currentRoomId}`);
}
displayCurrentRoom() {
if (!this.currentRoom) {
throw new Error(
`room with id \'${this.currentRoomId}\' was not defined in data.json`,
);
}
this.setColors();
this.setFont();
this.displayRoomDescription();
this.allDirections.forEach((direction) => {
this.displayExit(direction);
});
}
setColors() {
const gameContainer = document.querySelector("#game-container");
gameContainer.style.backgroundColor = this.currentBackgroundColor;
gameContainer.style.color = this.currentTextColor;
}
setFont() {
const gameContainer = document.querySelector("#game-container");
gameContainer.style.fontFamily = this.currentFont;
}
displayRoomDescription() {
const roomDescriptionDiv = document.querySelector("#room-description");
const description = this.currentDescription;
if (Array.isArray(description)) {
this.displayDescriptionArray(roomDescriptionDiv, description);
} else if (typeof description === "string") {
this.displayDescriptionString(roomDescriptionDiv, description);
}
}
displayDescriptionString(roomDescriptionDiv, description) {
roomDescriptionDiv.style.textAlign = "center";
roomDescriptionDiv.style.whiteSpace = "normal";
roomDescriptionDiv.textContent = description;
}
displayDescriptionArray(roomDescriptionDiv, description) {
roomDescriptionDiv.style.textAlign = "start";
const htmlContent = description.join("<br>");
roomDescriptionDiv.style.whiteSpace = "preserve nowrap";
roomDescriptionDiv.innerHTML = htmlContent;
}
displayExit(direction) {
const exitButton = this.getExitButton(direction);
if (!this.currentRoom[direction]) {
exitButton.style.display = "none";
return;
}
exitButton.style.display = "block";
exitButton.textContent = this.getExitLabel(direction);
}
}