-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
363 lines (299 loc) · 10.4 KB
/
sketch.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Raw JSON data
*/
let countriesData;
let alliancesData;
/**
* Timing
*/
let currentTime;
let previousTime;
/**
* Simulation data
*/
let simulation;
/**
* Mouse and zoom
*/
let zoom = 1;
let offset;
/**
* Menu and GUI system
*/
let clockButton;
let menuButtons = [];
let showCountryMenu = false;
let showNodeMenu = false;
let showWarMenu = false;
function preload() {
countriesData = loadJSON("data/countries.json");
alliancesData = loadJSON("data/alliances.json");
}
function setup() {
createCanvas(windowWidth, windowHeight);
simulation = new Simulation(1, 1, 2021);
/* Create clock button */
clockButton = createButton("");
setDefaultButtonLooks(clockButton);
clockButton.size(320, 50);
clockButton.position(width * 0.42, height * 0.02);
/* Create speed buttons */
let speedButtons = [];
for (let i = 0; i < 4; i++) {
let button = createButton("");
setDefaultButtonLooks(button);
button.position(width * 0.963 + (i * width * 0.035) - (width * 0.1), height * 0.02);
button.mouseClicked(() => {
simulation.changeSpeed(i);
button.style("background-color", "rgb(150, 150, 150)");
});
speedButtons.push(button);
}
speedButtons[0].html("||");
speedButtons[1].html("1");
speedButtons[2].html("2");
speedButtons[3].html("3");
/* Create menu buttons */
let menuButtonAmount = 3;
for (let i = 0; i <= menuButtonAmount; i++) {
let button = createButton("");
if (i != 0) {
button.hidden = true;
button.hide();
}
setDefaultButtonLooks(button);
button.position(width * 0.01, height * (0.02 + (i * 0.07)));
menuButtons.push(button);
}
let menuButton = menuButtons[0];
menuButton.html("≡");
menuButton.mouseClicked(() => {
for (let i = 0; i < menuButtons.length; i++) {
let button = menuButtons[i];
if (i != 0) {
if (button.hidden) {
button.hidden = false;
button.show();
} else {
button.hidden = true;
showCountryMenu = false;
showNodeMenu = false;
showWarMenu = false;
button.hide();
}
}
}
menuButton.style("background-color", "rgb(150, 150, 150)");
});
let countryMenuButton = menuButtons[1];
countryMenuButton.html("🏳️");
countryMenuButton.mouseClicked(() => {
showCountryMenu = !showCountryMenu;
showNodeMenu = false;
showWarMenu = false;
countryMenuButton.style("background-color", "rgb(150, 150, 150)");
});
let nodeMenuButton = menuButtons[2];
nodeMenuButton.html("🔘");
nodeMenuButton.mouseClicked(() => {
showNodeMenu = !showNodeMenu;
showCountryMenu = false;
showWarMenu = false;
nodeMenuButton.style("background-color", "rgb(150, 150, 150)");
});
let warMenuButton = menuButtons[3];
warMenuButton.html("⚔️");
warMenuButton.mouseClicked(() => {
showWarMenu = !showWarMenu;
showCountryMenu = false;
showNodeMenu = false;
warMenuButton.style("background-color", "rgb(150, 150, 150)");
});
/* Create countries from data */
countriesData.countries.forEach(country => simulation.countries.set(country.id, new Country(country)));
/* Create alliances from data */
alliancesData.alliances.forEach(alliance => simulation.alliances.push(new Alliance(alliance.name, alliance.members)));
/* Create nodes for all countries */
let totalNodeAmount = 10000;
let nodeAmount = [];
let countryAreas = [];
simulation.countries.forEach(country => {
let triangleAreas = Country.getAreas(country.countryTriangles());
let totalArea = 0;
triangleAreas.forEach(ratio => {
totalArea += ratio;
})
countryAreas.push(totalArea);
});
let areaRatios = Country.getAreaRatios(countryAreas);
areaRatios.forEach(ratio => nodeAmount.push(Math.ceil(totalNodeAmount * ratio)));
let counter = 0;
simulation.countries.forEach(country => {
country.nodes = country.generateNodes(countriesData.countries[counter], nodeAmount[counter]);
country.nodeAmount = nodeAmount[counter];
counter += 1;
})
/* Mouse transformations setup */
offset = createVector(0, 0);
window.addEventListener("wheel", event => {
const minZoom = 1;
const zoomCalc = 1 - (event.deltaY / 1000);
const mouse = createVector(mouseX, mouseY);
zoom *= zoomCalc;
if (zoom < minZoom) {
zoom = minZoom;
return;
}
offset.sub(mouse).mult(zoomCalc).add(mouse);
});
}
function draw() {
background(0, 0, 50);
stroke(255);
strokeWeight(2 / zoom);
/* Run simulation */
currentTime = millis();
if (Math.floor(currentTime / simulation.speed) != Math.floor(previousTime / simulation.speed)) {
simulation.update();
}
previousTime = currentTime;
/* Mouse transformations */
translate(offset.x, offset.y);
scale(zoom);
if (mouseIsPressed) {
offset.x -= pmouseX - mouseX;
offset.y -= pmouseY - mouseY;
}
/* Draw countries */
simulation.countries.forEach(country => country.draw());
/* Draw and update GUI */
clockButton.html(simulation.time.toString());
drawGui();
}
function mouseReleased() {
simulation.countries.forEach(country => {
country.selected = country.mouseInsideCountry();
if (country.selected) {
simulation.selectedCountry = country;
for (let node of country.nodes) {
node.selected = node.mouseInsideNode();
if (node.selected) {
simulation.selectedNode = node;
}
}
}
});
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function setDefaultButtonLooks(button) {
button.size(50, 50);
button.style("font-size", "2em");
button.style("border", "2px solid white");
button.style("background-color", "black");
button.style("color", "white");
button.mouseOver(() => {
button.style("background-color", "rgb(100, 100, 100)");
});
button.mouseOut(() => {
button.style("background-color", "black");
})
}
function drawGui() {
/* Reset Matrix for GUI */
resetMatrix();
noStroke();
fill(255);
textSize(20);
textAlign(LEFT);
text("Online Geopolitical Simulator - Jagger Harris 2023", 20, height * 0.97);
textAlign(LEFT, CENTER);
textSize(30);
/* Menus */
drawCountryMenu();
drawNodeMenu();
drawWarMenu();
}
function drawCountryMenu() {
if (showCountryMenu) {
stroke(255);
strokeWeight(2);
fill(0);
rect(width * 0.044, height * 0.092, 325, 420);
noStroke();
fill(255);
textSize(22);
textAlign(LEFT);
text("Country Menu", width * 0.048, height * 0.12);
if (simulation.selectedCountry) {
text(simulation.selectedCountry.name, width * 0.048, height * 0.16);
text("Population: " + simulation.selectedCountry.population.toLocaleString(), width * 0.048, height * 0.19);
text("Active Military: " + simulation.selectedCountry.activeMilitary.toLocaleString(), width * 0.048, height * 0.22);
text("Reserve Military: " + simulation.selectedCountry.reserveMilitary.toLocaleString(), width * 0.048, height * 0.25);
text("Fertility Rate: " + simulation.selectedCountry.fertilityRate.toFixed(2), width * 0.048, height * 0.28);
text("Adult Mortality Rate (M): " + simulation.selectedCountry.mortalityMaleAdults.toFixed(2), width * 0.048, height * 0.31);
text("Adult Mortality Rate (F): " + simulation.selectedCountry.mortalityFemaleAdults.toFixed(2), width * 0.048, height * 0.34);
text("Lifespan: " + simulation.selectedCountry.lifespan.toFixed(2), width * 0.048, height * 0.37);
text("Democracy Index: " + simulation.selectedCountry.democracyIndex.toFixed(2), width * 0.048, height * 0.4);
text("GDP: $" + Number(simulation.selectedCountry.gdp.toFixed(2)).toLocaleString(), width * 0.048, height * 0.43);
text("Nuclear Weapons: " + simulation.selectedCountry.nuclearWeapons, width * 0.048, height * 0.46);
} else {
text("No Selected Country", width * 0.048, height * 0.16);
}
}
}
function drawNodeMenu() {
if (showNodeMenu) {
stroke(255);
strokeWeight(2);
fill(0);
rect(width * 0.044, height * 0.162, 325, 420);
noStroke();
fill(255);
textSize(22);
textAlign(LEFT);
text("Node Menu", width * 0.048, height * 0.19);
if (simulation.selectedCountry) {
if (simulation.selectedNode) {
text("Country: " + simulation.selectedNode.country.name, width * 0.048, height * 0.23);
text("Population: " + simulation.selectedNode.population.toLocaleString(), width * 0.048, height * 0.26);
text("Active Military: " + simulation.selectedNode.activeMilitary.toLocaleString(), width * 0.048, height * 0.29);
text("Reserve Military: " + simulation.selectedNode.reserveMilitary.toLocaleString(), width * 0.048, height * 0.32);
text("Fertility Rate: " + simulation.selectedNode.fertilityRate, width * 0.048, height * 0.35);
text("Adult Mortality Rate (M): " + simulation.selectedNode.mortalityMaleAdults, width * 0.048, height * 0.38);
text("Adult Mortality Rate (F): " + simulation.selectedNode.mortalityFemaleAdults, width * 0.048, height * 0.41);
text("Lifespan: " + simulation.selectedNode.lifespan, width * 0.048, height * 0.44);
text("Democracy Index: " + simulation.selectedNode.democracyIndex, width * 0.048, height * 0.47);
text("GDP: $" + simulation.selectedNode.gdp.toLocaleString(), width * 0.048, height * 0.5);
text("Nuclear Weapons: " + simulation.selectedNode.nuclearWeapons, width * 0.048, height * 0.53);
if (simulation.selectedNode.capturer != simulation.selectedNode.country) {
text("Capturer: " + simulation.selectedNode.capturer.name, width * 0.048, height * 0.56);
}
} else {
text("No Selected Node", width * 0.048, height * 0.23);
}
} else {
text("No Selected Country", width * 0.048, height * 0.23);
}
}
}
function drawWarMenu() {
if (showWarMenu) {
stroke(255);
strokeWeight(2);
fill(0);
rect(width * 0.044, height * 0.232, 325, 420);
noStroke();
fill(255);
textSize(22);
textAlign(LEFT);
text("War Menu", width * 0.048, height * 0.26);
for (let i = 0; i < simulation.activeWars.length; i++) {
let war = simulation.activeWars[i];
text(war.attackersLeader.name + " vs. " + war.defendersLeader.name, width * 0.048, height * (0.3 + (i * 0.1)));
text("Attackers: " + war.calculatePercentage(true) + "%", width * 0.048, height * (0.33 + (i * 0.1)));
text("Defenders: " + war.calculatePercentage(false) + "%", width * 0.048, height * (0.36 + (i * 0.1)));
}
}
}