forked from JoshuaKGoldberg/Old-Deleted-FullScreenMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
204 lines (176 loc) · 5.65 KB
/
ui.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
var loadstart,
// Security fixes
isLocal,
// References
elemGame,
game,
body,
elemSelect;
function start() {
// Don't double start
if(window.loadstart) return;
window.loadstart = true;
// Know whether this is being run locally
setLocalStatus();
// Quick UI references
setReferences();
// Map selection
setMapSelector();
// Level editor
setLevelEditor();
// Options
setOptions();
// Make lots of friends
setCheats();
}
function setLocalStatus() {
window.isLocal = window.location.origin == "file://";
}
function setReferences() {
// Set the game references (elemGame is not the same as the content window)
window.elemGame = document.getElementById("game");
window.game = window.elemGame.contentWindow;
// Local games may not allow contentWindow shenanigans
if(!isLocal)
window.game.parentwindow = window;
window.body = document.body;
window.elemSelect = document.getElementById("in_mapselect");
}
function setMapSelector(timed) {
// If this isn't ready and hasn't tried before, try it again
if(!window.elemSelect && !timed)
setTimeout(function() {
setMapSelector(true);
}, 350);
// Get HTML each of the 32 levels' blocks in order
var innerHTML = "",
i, j;
for(i = 1; i <= 8; ++i)
for(j = 1; j <= 4; ++j)
innerHTML += createAdderMap(i, j);
// Add that HTML to #in_mapselect, along with a big one for random maps
elemSelect.innerHTML += innerHTML + createAdderBigMap("Map Generator!", "setGameMapRandom");
// If this isn't local, actually responding to the game loading maps is doable
// See load.js
if(!isLocal) {
// This will allow for onMapLoad
game.parentwindow = window;
// If the game already has a map, set the class to be loaded
var elem;
for(i = 1; i <= 8; ++i)
for(j = 1; j <= 4; ++j) {
if(game["World" + i + String(j)] && (elem = document.getElementById("maprect" + i + "," + j)))
elem.className = "maprect";
}
}
}
function createAdderMap(i, j) {
var adder = "";
adder += "<div class='maprectout'>";
adder += "<div id='maprect" + i + "," + j;
adder += "' class='maprect" + (isLocal ? "" : " off") + "' onclick='setGameMap(" + i + "," + j + ")'>";
adder += i + "-" + j;
adder += "</div></div>";
return adder;
}
function createAdderBigMap(name, onclick, giant) {
var adder = "";
adder += "<div class='maprectout'>";
adder += "<div class='maprect big " + (giant ? "giant" : "" ) + "' onclick='" + onclick + "()'>";
adder += name;
adder += "</div></div>";
return adder;
}
function setGameMap(one, two) {
// If it hasn't been loaded yet, don't do anything
if(document.getElementById("maprect" + one + "," + two).className != "maprect")
return;
// Otherwise go to the map
game.postMessage({
type: "setMap",
map: [one, two]
}, "*");
game.focus();
}
// See load.js
function onMapLoad(one, two) {
var elem = document.getElementById("maprect" + one + "," + two);
if(elem)
elem.className = "maprect";
}
function setGameMapRandom() {
game.postMessage({
type: "setMap",
map: ["Random", "Overworld"]
}, "*");
game.focus();
}
function setLevelEditor() {
var out = document.getElementById("in_editor"),
blurb = "Why use Nintendo's?<br />";
button = createAdderBigMap("Make your<br />own levels!", "startEditor", true);
out.innerHTML += blurb + button + "<br />You can save these as text files when you're done.";
}
function startEditor() {
game.postMessage({
type: "startEditor"
}, "*");
game.focus();
}
// Fills the options menu with a bunch of divs, each of which have an onclick of toggleGame('XYZ')
function setOptions() {
var out = document.getElementById("in_options"),
options = [
"Mute",
"Luigi",
"FastFWD"
],
innerHTML = "",
option, i;
for(i in options) {
option = options[i];
innerHTML += "<div class='maprectout' onclick='toggleGame(\"" + option + "\")'><div class='maprect big larger'>Toggle " + option + "</div></div>";
innerHTML += "<br />";
}
out.innerHTML += innerHTML + "<br />More coming soon!";
}
// toggleGame('XYZ') sends a message to the game to toggle XYZ
function toggleGame(me) {
game.postMessage({
type: "toggleOption",
option: me
}, "*");
}
function setCheats() {
var i;
console.log("Hi, thanks for playing Full Screen Mario! I see you're using the console.");
console.log("There's not really any way to stop you from messing around so if you'd like to know the common cheats, enter \"displayCheats()\" here.");
console.log("If you'd like, go ahead and look around the source code. There are a few surprises you might have fun with... ;)");
console.log("http://www.github.com/DiogenesTheCynic/FullScreenMario");
window.cheats = {
Change_Map: "game.setMap([#,#] or #,#);",
Change_Map_Location: "game.shiftToLocation(#);",
Fast_Forward: "game.fastforward(amount; 1 by default);",
Life: "game.gainLife(# amount or Infinity)",
Low_Gravity: "game.player.gravity = game.gravity /= 2;",
Lulz: "game.lulz();",
Random_Map: "game.setMapRandom();",
Shroom: "game.playerShroom(game.player)",
Star_Power: "game.playerStar(game.player)",
Unlimited_Time: "game.data.time.amount = Infinity;",
}
cheatsize = 0;
for(var i in cheats)
cheatsize = Math.max(cheatsize, i.length);
}
function displayCheats() {
console.log("These are stored in the global 'cheats' object, by the way.");
for(i in cheats)
printCheat(i, cheats[i]);
return "Have fun!";
}
function printCheat(name, text) {
for (i = cheatsize - name.length; i > 0; --i)
name += ".";
console.log(name.replace("_", " ") + "...... " + text);
}