-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
153 lines (136 loc) · 4.44 KB
/
engine.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
async function game(gameJsonUrl, jsonParser = JSON.parse) {
game["play_scene"]=function (scene) {
var scene = game["data"].scenes[scene];
game["buttons"].classList.add("hidden");
game["video"].classList.remove("hidden");
if(scene.url){
game["video"].src = scene.url;
game["video"].load();
game["video"].addEventListener("ended", ()=>{
game["display_buttons"](scene);
});
game["video"].oncanplay = ()=>{game["video"].play()};
} else if (scene.bg) {
game["video"].poster = scene.bg;
game["video"].src="";
game["display_buttons"](scene);
} else {
game["video"].classList.add("hidden");
game["display_buttons"](scene);
}
}
game["display_buttons"] = function (scene) {
if(game.data.ongui){
eval(game.data.ongui);
}
game["buttons"].innerHTML="";
game["buttons"].classList.remove("hidden");
if(scene.title) {
game["buttons"].innerHTML+=`<h1>${scene.title}</h1>`;
}
if(scene.subtitle) {
game["buttons"].innerHTML+=`<p>${scene.subtitle}</p>`;
}
if(!scene.buttons) {
return;
}
scene.buttons.forEach(button => {
var btn = document.createElement("button");
if (button.icon) {
var img = document.createElement("img");
img.src=button.icon;
img.draggable = false;
btn.append(img);
}
btn.innerHTML += button.label;
btn.classList.add("button");
btn.addEventListener("click", ()=>{
if(button.onclick){
eval(button.onclick);
}
if(button.scene){
if (typeof button.scene === "string") {
game["play_scene"](button.scene);
} else if (Array.isArray(button.scene) && button.scene.length !== 0) {
game["play_scene"](button.scene[Math.floor(Math.random()*button.scene.length)]);
}
}
});
game["buttons"].append(btn);
});
}
game["remove_dupes"]=function () {
if(document.getElementById("clickengine_video")){
document.getElementById("clickengine_video").remove();
}
if(document.getElementById("clickengine_style")){
document.getElementById("clickengine_style").remove();
}
if(document.getElementById("clickengine_buttons")){
document.getElementById("clickengine_buttons").remove();
}
}
game["startgame"]=function () {
if(game.data.onstart){
eval(game.data.onstart);
}
if (game.data.mainscene) {
game["play_scene"](game.data.mainscene);
} else {
game["play_scene"](Object.keys(game.data.scenes)[0]);
}
}
var c = await fetch(gameJsonUrl);
var d = await c.blob();
var textContent = await d.text();
game.data = jsonParser(textContent);
game["remove_dupes"]();
game["video"] = document.createElement("video");
game["video"].controls = false;
game["video"].draggable = false;
game["video"].addEventListener("click", (e)=>{e.preventDefault()});
game["video"].addEventListener("contextmenu", (e)=>{e.preventDefault()});
game["video"].id = "clickengine_video";
game["video"].style = `position: fixed; z-index: 1; top: 50%; left: 50%; width: 100%; transform: translate(-50%, -50%)`;
document.body.append(game["video"]);
game["style"] = document.createElement("style");
game["style"].id = "clickengine_style";
game["style"].innerHTML = `.hidden {display: none !important; }
.button {
text-align: center;
background: transparent;
font-size: 2rem;
color: ${game.data.theme === "light" ? "black" : "white"};
text-shadow: 2px 2px 2px rgba(0,0,0,0.5);
border: 0;
cursor: pointer;
transition: 0.35s;
margin: 1.5rem;
}
.button:hover {
transform: scale(1.2);
}
.button>img {
width: 30vh;
display: block;
}
html, body {
background: ${game.data.theme === "light" ? "white" : "black"};
}
#clickengine_buttons>h1{
font-size: 4rem;
margin-top: 0;
margin-bottom: 1rem;
}
#clickengine_buttons>p{
font-size: 1.5rem;
margin-bottom: 0.2rem;
}
`;
document.body.append(game["style"]);
game["buttons"] = document.createElement("div");
game["buttons"].id = "clickengine_buttons";
game["buttons"].style = `position: fixed; z-index: 2; top: 50%; left: 50%; width: 100%; transform: translate(-50%, -50%); text-align: center; color: ${game.data.theme === "light" ? "black" : "white"}; text-shadow: 2px 2px 2px rgba(0,0,0,0.5);`;
document.body.append(game["buttons"]);
game["startgame"]();
}