forked from JoshuaKGoldberg/Old-Deleted-FullScreenMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
92 lines (77 loc) · 2.66 KB
/
load.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
/* Load.js */
// Maps and sounds are loaded via AJAX
/* Maps */
function startLoadingMaps() {
// Don't attempt to ajax if on a local system, it'll just crash and you'll look like a fool
if(window.location.protocol == "file:") return;
// Over-eager loading ftw!
passivelyLoadMap([1,2], new XMLHttpRequest());
}
// Finds a map, parses it, then recurses on the next map
function passivelyLoadMap(map, ajax) {
// Don't try to load worlds 9 and up
if(!map || map[0] > 8 || map[1] <= 0) return;
// Maps/WorldXY.js
var url = "Maps/World" + map[0] + "" + map[1] + ".js"
ajax.open("GET", url, true);
mlog("Maps", "Requesting:", url);
ajax.send();
ajax.onreadystatechange = function() {
if(ajax.readyState != 4) return;
// Map file found, load it up!
if(ajax.status == 200) {
// This is potentially insecure, so I'd like to use an editor-style JSON arrangement instead..
mapfuncs[map[0]][map[1]] = Function(ajax.responseText);
if(window.parentwindow && parentwindow.onMapLoad) {
parentwindow.onMapLoad(map[0],map[1]);
setTimeout(function() { parentwindow.onMapLoad(map[0],map[1]); }, 2100); // just in case
}
mlog("Maps", " Loaded: Maps/World" + map[0] + "" + map[1] + ".js");
}
// Otherwise, unless it just was a 404, return
else if(ajax.status != 404) return;
setTimeout(function() { passivelyLoadMap(setNextLevelArr(map), ajax); }, 7);
};
}
function setNextLevelArr(arr) {
if(arr[1]++ == 4) {
++arr[0];
arr[1] = 1;
}
return arr;
}
/* Sounds */
function startLoadingSounds() {
var libsounds = library.sounds;
setTimeout(function() { loadSounds(libsounds, library.sounds.names, "Sounds/"); }, 7);
setTimeout(function() { loadSounds(libsounds, library.sounds.themes, "Sounds/Themes/"); }, 14);
}
// Loads sounds (in order) from the reference into the container
function loadSounds(container, reference, prefix) {
var sound, name_raw,
details = {
preload: 'auto',
prefix: '',
used: 0
},
len, i;
for(i = 0, len = reference.length; i < len; ++i) {
name_raw = reference[i];
// Create the sound and store it in the container
sound = createElement("Audio", details);
container[name_raw] = sound;
mlog("Sounds", sound)
// Create the MP3 and OGG sources for the audio
sound.appendChild(createElement("Source", {
type: "audio/mp3",
src: prefix + "mp3/" + name_raw + ".mp3"
}));
sound.appendChild(createElement("Source", {
type: "audio/ogg",
src: prefix + "ogg/" + name_raw + ".ogg"
}));
// This preloads the sound.
sound.volume = 0;
sound.play();
}
}