/
main.js
330 lines (261 loc) · 10.1 KB
/
main.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
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
// constants
var DAYS_OF_WEEK = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var STARTING_DAY = 15; // Sunday, Jan. 15
var UPDATE_INTERVAL = 5; // update very five minutes
var CALENDAR = []; // special events
var counter = 0;
var deltaTime = 0;
var Disease; // main object
// time stuff; different events happen depending on stuff
var day, hour, minute;
var numStudents;
var numInfected;
var numSick;
// DOM stuff
var tooltip;
var clock;
var svg, g;
pruneCourses();
// initial stuff
document.getElementById("input").onkeypress = function(event) {
if (event.keyCode === 13) { // success
console.log("return pressed");
document.getElementById("input").remove();
document.getElementById("title").innerText = "You have chosen the name 'Norovirus'\nStarting game...";
var opacity = 1;
var interval = window.setInterval(function() {
opacity -= 0.02;
document.getElementById("container").style.opacity = opacity;
if (opacity <= 0) {
initialize();
document.getElementById("container").remove();
clearInterval(interval);
}
}, 50);
}
}
function updateLoop() {
if (counter % Math.max(1, Math.floor(UPDATE_INTERVAL - deltaTime/60)) === 0) {
var t1 = performance.now();
update();
var t2 = performance.now();
deltaTime = t2-t1;
// update clock
// clock.innerText = ("00" + hour).slice(-2) + ":" + ("00" + minute).slice(-2);
clock.innerHTML = '<span style="font-size: 24px;">' + numInfected + " / " + numStudents + " infected</span><br><span>Day " + day + "<br>" + ("00" + ((hour+11) % 12 + 1)).slice(-2) + ":00" + (hour >= 12 ? "pm" : "am</span>");
minute += UPDATE_INTERVAL;
while (minute >= 60) {
minute -= 60;
hour++;
while (hour >= 24) {
hour -= 24;
day++;
}
}
}
counter++;
window.requestAnimationFrame(updateLoop);
}
function update() {
var time = hour*60+minute;
var timestamp = day*24*60 + time;
// for every student, check if they have stuff to do
for (var i = 0; i < BUILDINGS.length; i++) {
var list = BUILDINGS[i].students;
for (var j = 0; j < list.length; j++) {
var student = list[j];
// check if they are finished with their current activity
if (student.finished <= timestamp) {
// remove from current building
BUILDINGS[i].students.splice(j, 1);
j--; // don't skip over the next student, everyone else has index reduced by 1
var duration = student.chooseNextActivity(day, hour, minute, BUILDINGS[i]);
student.exposeToInfection(duration, BUILDINGS[i]);
}
}
}
// student count and polygon filling
var studentCount = 0;
for (var i = 0; i < BUILDINGS.length; i++) {
studentCount += BUILDINGS[i].students.length
g.children[i].setAttribute("fill", "#" + ("00" + Math.min(255,Math.floor(12*Math.sqrt(BUILDINGS[i].students.length))).toString(16)).slice(-2) + "0000");
}
// update tooltip
var b = BUILDINGS[parseInt(tooltip.value)].students.length
tooltip.children[3].innerText = b + " student" + (b !== 1 ? "s" : "");
if (numInfected <= 0) {
console.log("EVERYONE CURED! YOU FAIL!");
}
// console.log(DAYS_OF_WEEK[day % 7] + ", January " + (15+day) + ", " + hour + ":" + (minute < 10 ? "0" : "") + minute);
}
function initialize() {
numStudents = 0, numInfected = 0, numSick = 0;
day = 1, hour = 0, minute = 0;
// upgrade window
createUpgradesScreen();
// create vector map
createVectorMap();
// create students
for (var i = 0; i < BUILDINGS.length; i++) {
// list of students currently in the building
BUILDINGS[i].students = [];
// everyone starts out in their residence hall
if (BUILDINGS[i].type === "residence") {
var studentList = [];
for (var j = 0; j < BUILDINGS[i].population; j++) {
studentList.push(new Student(BUILDINGS[i]));
numStudents++;
}
BUILDINGS[i].students = studentList;
}
// add eateries to buildings
for (var j = 0; j < FOOD_PLACES.length; j++) {
if (FOOD_PLACES[j].building === BUILDINGS[i].name) {
if (BUILDINGS[i].food === undefined) {
BUILDINGS[i].food = [];
}
BUILDINGS[i].food.push(FOOD_PLACES[j]);
// fix days stuff for food
var count = 0;
var pow2 = 1;
for (var k = 0; k < 7; k++) {
if (FOOD_PLACES[j].days[k]) {
count += pow2;
}
pow2 *= 2;
}
FOOD_PLACES[j].days = count;
FOOD_PLACES[j].building = BUILDINGS[i]; // direct reference, not name
}
}
if (BUILDINGS[i].type === "gym") {
ATHLETICS_PLACES.push(BUILDINGS[i]);
}
}
// TODO: off-campus students?
// GUI
var gui = document.createElement("div");
gui.setAttribute("id", "gui");
var upgrades = document.createElement("div");
upgrades.setAttribute("id", "upgrades");
upgrades.innerText = "Upgrades";
upgrades.addEventListener("click", function(e) {
document.getElementById("upgradeScreen").style.display = "block";
}, false);
gui.appendChild(upgrades);
document.body.appendChild(gui);
// initialize tooltip
tooltip = document.createElement("div");
tooltip.setAttribute("id", "tooltip");
tooltip.value = "0";
tooltip.innerHTML = '<h4></h4><div class="category"></div><br><div></div>';
tooltip.style.display = "none";
document.body.appendChild(tooltip);
// initialize clock
clock = document.createElement("div");
clock.setAttribute("id", "clock");
gui.appendChild(clock);
// infect patient zero!
randomArrayElement(BUILDINGS[0].students).infected = 0.1;
numInfected++;
console.log("initialized");
window.requestAnimationFrame(updateLoop);
}
function pruneCourses() {
var oldLength = COURSES.length
// remove courses that are not suitable
var new_courses = [];
for (var i = 0; i < oldLength; i++) {
var course = COURSES[i];
if (course.enrolled < 20) { // minimum enrollment
continue;
} else if (!course.startH) { // some courses don't have defined locations
continue;
}
// change building string to building ID
var foundID = false;
for (var j = 0; j < BUILDINGS.length; j++) {
if (course.building === BUILDINGS[j].id) {
foundID = true;
course.building = j;
break;
}
}
if (!foundID) {
continue;
}
new_courses.push(course);
}
COURSES = new_courses;
console.log('pruned number of courses from ' + oldLength + ' to ' + COURSES.length);
}
function createVectorMap() {
var svgns = "http://www.w3.org/2000/svg";
svg = document.createElementNS(svgns, "svg");
svg.setAttribute("id", "map");
svg.setAttribute("baseProfile", "tiny");
svg.setAttribute("height", "938.997");
// svg.setAttribute("width", "1131.5007");
svg.setAttribute("width", "1050");
svg.setAttribute("version", "1.2");
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
g = document.createElementNS(svgns, "g");
for (var i = 0; i < BUILDINGS.length; i++) {
var polygon = document.createElementNS(svgns, "polygon");
polygon.setAttribute("fill", "black");
polygon.setAttribute("stroke", "black");
polygon.setAttribute("stroke-width", "1");
polygon.setAttribute("name", BUILDINGS[i].name);
polygon.setAttribute("buildingID", i);
var polyString = "";
for (var j = 0; j < BUILDINGS[i].points.length; j+=2) {
// each coordinate pair
polyString += BUILDINGS[i].points[j];
polyString += "," + BUILDINGS[i].points[j+1];
if (j+2 < BUILDINGS[i].points.length) {
polyString += " ";
}
}
polygon.setAttribute("points", polyString)
// event listeners
polygon.addEventListener("mouseenter", function(e) {
// this.setAttribute("fill", "red");
var building = BUILDINGS[parseInt(this.getAttribute("buildingID"))];
tooltip.value = parseInt(this.getAttribute("buildingID"));
tooltip.children[0].innerText = building.name;
if (building.type === "misc") {
tooltip.children[1].innerText = "";
} else {
tooltip.children[1].innerText = building.type;
}
tooltip.children[3].innerText = building.students.length + " student" + (building.students.length !== 1 ? "s" : "");
tooltip.style.display = "block";
}, false);
polygon.addEventListener("mouseleave", function(e) {
// USE TRANSPARENT, NOT NONE
// this.setAttribute("fill", "black");
tooltip.style.display = "none";
}, false);
g.appendChild(polygon);
}
svg.innerHTML += SVG_DATA;
svg.appendChild(g);
document.body.appendChild(svg);
// create events for moving tooltip
document.addEventListener("mousemove", function(e) {
var tooltip = document.getElementById("tooltip");
if (tooltip) {
var x = e.pageX + 10;
var y = e.pageY - 3;
// shift it to the other side of the mouse if near the right/bottom of the screen
if (window.innerWidth + window.pageXOffset < x + 210) {
x -= 225;
}
if (window.innerHeight + window.pageYOffset < y + 230) {
y -= 220;
}
tooltip.style.left = x + "px";
tooltip.style.top = y + "px";
}
}, false);
}