-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (125 loc) · 3.92 KB
/
index.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
143
144
145
146
147
148
149
150
const FADE_DURATION = 100;
const startRoom = localStorage.getItem("savedRoom");
const storyArea = document.getElementById("story-area");
/**
*
* @param {Object} data
* @throws {Error} - The data is not valid
*/
function errorOnInvalidData(data) {
for (const id in data) {
for (const button of data[id]["buttons"]) {
if (!(button.id in data))
throw new Error(`Data is not valid - "${button.id}" is not a room (referenced in room "${id}")`);
}
}
}
// TODO: make async
function showContent(text, delay, speaker) {
const content = document.createElement("div");
if (speaker.toLowerCase() === "narrator") {
content.innerHTML = text;
} else {
content.innerHTML = speaker + "> " + text;
}
content.className = "story-element";
content.style.display = "none";
scrollFade(content, delay)
storyArea.appendChild(content);
}
function scrollFade(element, delay) {
$(element).delay(delay * 1000).fadeIn(FADE_DURATION).queue(function (nxt) {
$('body')[0].scrollIntoView(false);
nxt();
});
};
function spacerCreate(parentEle, delay) {
const spacer = document.createElement("div");
spacer.id = "spacer";
spacer.style.display = "none";
scrollFade(spacer, delay);
parentEle.appendChild(spacer);
};
// returns a div full of buttons
function showChoices(buttons, roomId, data, delay) {
const choiceList = document.createElement("div");
const roomName = `choice-${roomId}`;
choiceList.id = roomName;
choiceList.className = "choice-list"
if (buttons.length > 0)
spacerCreate(choiceList, delay);
for (const button of buttons) {
const buttonEle = document.createElement("button");
const linkName = `choice-${roomId}-to-${button.id}`;
buttonEle.textContent = button.text;
buttonEle.onclick = function () { makeChoice(button.id, data, this) };
buttonEle.id = linkName;
buttonEle.style.display = "none";
buttonEle.className = "choice"
scrollFade(buttonEle, delay);
choiceList.appendChild(buttonEle);
}
if (buttons.length > 0)
spacerCreate(choiceList, delay);
storyArea.appendChild(choiceList);
}
function makeChoice(roomId, data, buttonObj) {
// hide all other choices
for (const ele of buttonObj.parentElement.children) {
ele.disabled = "disabled"; // we can't click anything in the past
if (ele == buttonObj) {
ele.className = "chosen";
} else {
ele.className = "not-chosen";
}
}
if (roomId == "load") {
if (!startRoom || startRoom === "startup") {
showContent("Error! Saved game could not be found.", 0, "Narrator");
showContent("Starting game...", 0.5, "Narrator");
showContent("", 3, "Narrator");
playThroughRoom("start", data);
} else {
playThroughRoom(startRoom, data);
};
} else {
playThroughRoom(roomId, data);
}
};
function playThroughRoom(roomId, data) {
localStorage.setItem("savedRoom", roomId);
let runningDelay = 0;
for (const { text, delay, speaker } of data[roomId].content) {
runningDelay += delay;
showContent(text, runningDelay, speaker);
}
runningDelay += 1;
showChoices(data[roomId].buttons, roomId, data, runningDelay);
}
function loadingAnim() {
i = 0;
setInterval(function () {
if (i == 3) {
return
}
else {
$("#loading").append(".");
i++;
}
}, 500);
};
loadingAnim();
function startGame(script) {
playThroughRoom("startup", script);
}
function startupAnim() {
}
fetch("./script.json")
.then(mockResponses => {
return mockResponses.json();
})
.then(data => {
errorOnInvalidData(data);
console.log("starting game...")
startGame(data);
}).catch(err => console.error(err));