4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 document.title = dragonblocks.version.string;
27 let mainmenu = document.body.insertBefore(document.createElement("div"), document.body.firstChild);
28 mainmenu.style.visibility = "hidden";
30 let center = mainmenu.appendChild(document.createElement("center"));
32 let logo = center.appendChild(document.createElement("img"));
33 logo.src = "textures/logo-mainmenu.png";
35 let splash = mainmenu.appendChild(document.createElement("div"));
36 splash.style.position = "absolute";
37 splash.style.transform = "rotate(-15deg)";
38 splash.style.color = "yellow";
39 splash.style.fontSize = "30px";
41 let splashes = $.getJSON("splashes.json").responseJSON;
43 let status = center.appendChild(document.createElement("h1"));
44 status.style.fontSize = "50px";
45 status.style.display = "none";
47 let content = center.appendChild(document.createElement("div"));
48 content.id = "dragonblocks.mainmenu.content";
49 content.style.position = "relative";
50 content.style.top = "50px";
55 let clearChildren = parent => {
56 while (elem = parent.firstChild)
63 let loadWorldGUI, worldlistDisplay, noWorldsNotice;
65 if (dragonblocks.loggedin) {
68 clearChildren(worldlistDisplay);
70 loadWorldGUI = new dragonblocks.gui.Box();
72 let headline = loadWorldGUI.create("h1");
73 headline.innerHTML = "Select World";
74 headline.align = "center";
76 noWorldsNotice = loadWorldGUI.create("center").appendChild(document.createElement("b"));
78 worldlistDisplay = loadWorldGUI.create("ul");
81 noWorldsNotice.innerHTML = dragonblocks.worlds.length == 0 ? "No Worlds" : "";
83 for (let worldname in dragonblocks.worlds) {
84 let world = dragonblocks.worlds[worldname];
87 let worldDisplay = worldlistDisplay.appendChild(document.createElement("li"));
88 worldDisplay.style.fontSize = "20px";
89 worldDisplay.textContent = world.name;
90 worldDisplay.style.postion = "relative";
92 let button = worldDisplay.appendChild(document.createElement("button"));
93 button.textContent = "Play";
94 button.style.position = "absolute";
95 button.style.right = "5px";
96 button.style.fontSize = "12px";
97 button.addEventListener("click", event => {
98 event.srcElement.blur();
101 dragonblocks.worldIsLoaded = true;
102 dragonblocks.worldname = world.name;
103 dragonblocks.world = $.getJSON("worlds/" + worldname + "/world.json").responseJSON;
105 dragonblocks.mods = dragonblocks.world.mods;
107 dragonblocks.start();
115 text: "Load Saved World",
119 disabled: ! dragonblocks.loggedin,
123 // Create World Button
126 let createWorldGUI = new dragonblocks.gui.Box();
129 let worldProperties = {};
131 let headline = createWorldGUI.create("h1");
132 headline.innerHTML = "New World";
133 headline.align = "center";
136 createWorldGUI.create("h2").innerHTML = " World Name";
138 let worldnameInput = createWorldGUI.create("input");
139 worldnameInput.type = "text";
140 worldnameInput.style.position = "relative";
141 worldnameInput.style.left = "40px";
143 let worldnameAlert = createWorldGUI.create("b");
144 worldnameAlert.style.position = "relative";
145 worldnameAlert.style.left = "50px";
147 worldnameInput.addEventListener("input", _ => {
148 let worldname = worldnameInput.value;
150 if(! dragonblocks.loggedin) {
151 worldnameAlert.textContent = "Warning: You are not logged in and cannot save worlds.";
152 worldnameAlert.style.color = "#FF7D00";
153 createButton.disabled = false;
154 } else if (worldname == "") {
155 worldnameAlert.textContent = "";
156 createButton.disabled = true;
157 } else if (! dragonblocks.checkWorldnameSpelling(worldname)) {
158 worldnameAlert.textContent = "The world name contains forbidden characters";
159 worldnameAlert.style.color = "#FF001F";
160 createButton.disabled = true;
161 } else if (dragonblocks.worlds[worldname]) {
162 if (dragonblocks.worlds[worldname].owned) {
163 worldnameAlert.textContent = "Warning: This will overwrite an existing world";
164 worldnameAlert.style.color = "#FF7D00";
165 createButton.disabled = false;
167 worldnameAlert.textContent = "This Worldname is taken";
168 worldnameAlert.style.color = "#FF001F";
169 createButton.disabled = true;
172 worldnameAlert.textContent = "";
173 createButton.disabled = false;
176 worldProperties.worldname = worldname;
180 worldProperties.mods = {};
182 createWorldGUI.create("h2").innerHTML = " Mods";
186 let updateModlist = _ => {
188 clearChildren(modlistDisplay);
190 modlistDisplay = createWorldGUI.create("ul");
192 let oldSelectedMods = worldProperties.mods;
193 worldProperties.mods = {};
195 for (let modname in dragonblocks.mods) {
196 let modinfo = dragonblocks.mods[modname];
198 let modDisplay = modlistDisplay.appendChild(document.createElement("li"));
199 modDisplay.style.fontSize = "20px";
200 modDisplay.innerHTML = mod;
201 modDisplay.style.postion = "relative";
202 modDisplay.title = modinfo.description;
204 let checkbox = modDisplay.appendChild(document.createElement("input"));
205 checkbox.type = "checkbox";
206 checkbox.style.position = "absolute";
207 checkbox.style.right = "5px";
209 checkbox.addEventListener("input", _ => {
210 worldProperties.mods[mod] = checkbox.checked;
213 worldProperties.mods[mod] = checkbox.checked = oldSelectedMods[mod];
218 worldProperties.gamemode = "survival";
220 createWorldGUI.create("h2").innerHTML = " Gamemode";
222 for (let gamemode of ["survival", "creative"]){
223 let radiobox = createWorldGUI.create("input");
224 radiobox.name = "dragonblocks.mainmenu.createWorldGUI.gamemode";
225 radiobox.type = "radio";
226 radiobox.checked = gamemode == worldProperties.gamemode;
227 radiobox.style.position = "relative";
228 radiobox.style.left = "40px";
230 radiobox.addEventListener("input", _ => {
231 if (radiobox.checked)
232 worldProperties.gamemode = gamemode;
235 let label = createWorldGUI.create("label");
236 label.innerHTML = dblib.humanFormat(gamemode);
237 label.style.position = "relative";
238 label.style.left = "40px";
242 createWorldGUI.create("h2").innerHTML = " Mapgen";
244 let selectMapgen = createWorldGUI.create("select");
245 selectMapgen.style.position = "relative";
246 selectMapgen.style.left = "40px";
248 selectMapgen.addEventListener("input", _ => {
249 worldProperties.mapgen = selectMapgen.value;
252 for (let mapgen in dragonblocks.mapgen.list)
253 selectMapgen.appendChild(document.createElement("option")).innerHTML = mapgen;
255 worldProperties.mapgen = selectMapgen.value;
257 createWorldGUI.create("br");
258 createWorldGUI.create("br");
261 createButton = createWorldGUI.create("button");
262 createButton.style.position = "absolute";
263 createButton.style.left = "1%";
264 createButton.style.bottom = "5px";
265 createButton.style.width = "98%";
266 createButton.style.fontSize = "20px";
267 createButton.innerHTML = "Create World";
269 createButton.addEventListener("click", event => {
270 event.srcElement.blur();
271 createWorldGUI.close();
273 dragonblocks.worldIsLoaded = false;
274 dragonblocks.worldname = worldProperties.worldname;
275 dragonblocks.world = dragonblocks.getEmptyWorld();
277 dragonblocks.entities["dragonblocks:player"].meta.creative = (worldProperties.gamemode == "creative");
279 dragonblocks.mapgen.selected = worldProperties.mapgen;
281 dragonblocks.start(worldProperties.mods);
284 createWorldGUI.create("br");
285 createWorldGUI.create("br");
288 worldnameInput.value = "";
289 worldnameAlert.textContent = "";
290 createButton.disabled = dragonblocks.loggedin;
296 text: "Create New World",
298 createWorldGUI.open();
306 let creditsGUI = new dragonblocks.gui.Box();
308 let pages = $.getJSON("credits.json").responseJSON;
311 for (let dir of ["left", "right"]) {
312 let arrow = creditsGUI.create("div");
313 arrow.style.position = "absolute";
314 arrow.style.width = "80px";
315 arrow.style.height = "80px";
316 arrow.style.position = "absolute";
317 arrow.style[dir] = "3px";
318 arrow.style.background = dragonblocks.getTexture("arrow.png");
319 arrow.style.backgroundSize = "cover";
320 arrow.style.cursor = "pointer";
323 arrow.style.transform = "rotate(180deg)";
325 arrow.addEventListener("click", _ => {
334 dblib.centerVertical(arrow);
337 let creditsContent = creditsGUI.create("center");
339 creditsGUI.addEventListener("open", _ => {
341 page = pages.length - 1;
342 else if (page >= pages.length)
345 creditsContent.innerHTML = pages[page];
346 dragonblocks.resolveTextures(creditsContent);
348 // fix to center the dots of the li elements in chromium as well
350 let lis = creditsContent.getElementsByTagName("li");
353 li.style.width = "max-content";
370 if (dragonblocks.isChromeApp)
378 for (let {text, action, disabled} of buttons) {
379 let button = content.appendChild(document.createElement("button"));
380 button.style.fontSize = "40px";
381 button.style.width = "100%";
382 button.innerHTML = text;
383 button.disabled = disabled;
384 button.addEventListener("click", action);
386 content.appendChild(document.createElement("br"));
387 content.appendChild(document.createElement("br"));
390 for (let [side, text] of [["left", dragonblocks.version.string], ["right", dragonblocks.version.copyright]]) {
391 let notice = content.appendChild(document.createElement("span"));
392 notice.style.position = "fixed";
393 notice.style.bottom = "5px";
394 notice.style[side] = "5px";
395 notice.innerHTML = text;
398 dragonblocks.enterMainMenu = _ => {
399 dragonblocks.loadModList();
400 dragonblocks.loadWorldList();
402 content.style.display = "inherit";
403 status.style.display = "none";
405 for (let func of onReload)
409 dragonblocks.registerOnStart(_ => {
410 content.style.display = "none";
411 status.style.display = "inherit";
413 status.innerHTML = "Loading...";
416 dragonblocks.registerOnStarted(_ => {
417 mainmenu.style.visibility = "hidden";
420 dragonblocks.registerOnQuit(_ => {
421 mainmenu.style.visibility = "visible";
422 status.innerHTML = "Saving...";
425 let updateSplash = _ => {
426 splash.style.left = (logo.x + logo.width - splash.clientWidth / 2) + "px";
427 splash.style.top = (logo.y + logo.height / 3 * 2 - splash.clientHeight / 2) + "px";
430 let initMainMenu = _ => {
431 document.body.style.backgroundColor = "skyblue";
432 document.getElementById("elidragon").remove();
433 content.style.width = logo.offsetWidth + "px";
434 mainmenu.style.visibility = "visible";
436 splash.innerHTML = splashes[Math.floor(Math.random() * splashes.length)];
441 splash.style.fontSize = Math.sin(counter++ / 100 * Math.PI) * 5 + 30 + "px";
445 dragonblocks.enterMainMenu();
451 logo.addEventListener("load", initMainMenu);