Skip to content

Commit 04f5114

Browse files
committed
menu sys
1 parent fe7bdd5 commit 04f5114

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

game/iframe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</script>
1414
<script src="js/pako.js" defer></script>
1515
<script src="js/html2canvas.js" defer></script>
16-
<!-- compression library for izombie lvl saving -->
16+
<script src="js/Menus.js" defer></script>
1717
<script src="js/Custom.js" defer></script>
1818
<script src="js/Cheatcodes.js" defer></script>
1919
<meta property="og:title" content="Play Plants vs Zombies Modded Online" />

game/js/Menus.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const dSurface = document.getElementById("dSurface");
2+
3+
async function PreloadMenu(menuId) {
4+
// fetch menu/html/{menuId}.html and menu/js/{menuId}.js, return nothing and do nothing with it
5+
await Promise.all([
6+
fetch(`menu/html/${menuId}.html`).then(response => response.text()),
7+
fetch(`menu/js/${menuId}.js`).then(response => response.text())
8+
]);
9+
}
10+
11+
async function LoadMenu(menuId, background) {
12+
// fetch menu/html/{menuId}.html and menu/js/{menuId}.js. run the js code after injecting the html into the #dSurface element.
13+
const [htmlResponse, jsResponse] = await Promise.all([
14+
fetch(`menu/html/${menuId}.html`),
15+
fetch(`menu/js/${menuId}.js`)
16+
]);
17+
18+
const html = await htmlResponse.text();
19+
const js = await jsResponse.text();
20+
21+
// make menuContainer for menu
22+
const menuContainer = document.createElement("div");
23+
menuContainer.id = `dMenu_${menuId}`;
24+
menuContainer.style.display = "block";
25+
menuContainer.style.position = "absolute";
26+
menuContainer.style.width = "100%";
27+
menuContainer.style.height = "100%";
28+
menuContainer.style.top = "0";
29+
menuContainer.style.left = "0";
30+
if (background) {
31+
menuContainer.style.background = `url(${background}) no-repeat`;
32+
}
33+
menuContainer.innerHTML = html;
34+
dSurface.appendChild(menuContainer);
35+
36+
// run the js code (provide menuContainer to the menu script)
37+
const runMenuScript = new Function("menuContainer", "menuId", `"use strict";\n${js}\n`);
38+
runMenuScript(menuContainer, menuId, background);
39+
40+
return menuContainer;
41+
}
42+
43+
async function UnloadMenu(menuId) {
44+
// remove the menu menuContainer from #dSurface
45+
const menuContainer = document.getElementById(`dMenu_${menuId}`);
46+
if (menuContainer) {
47+
dSurface.removeChild(menuContainer);
48+
}
49+
}

game/menu/html/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>works!</h1>

game/menu/js/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("no way test.js loaded!", menuId, menuContainer);

0 commit comments

Comments
 (0)