Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
EmulatorOS
GitHub Repository: EmulatorOS/attaeht.github.io
Path: blob/main/ehtsite/assets/games/subway/js/boot.js
2252 views
1
/* eslint-disable func-names */
2
/* eslint-disable no-var */
3
/** Load script file */
4
function loadScript(path, onComplete)
5
{
6
var script = document.createElement('script');
7
8
if (onComplete) script.onload = onComplete;
9
script.src = path;
10
document.head.appendChild(script);
11
}
12
13
/** Start up Poki SDK */
14
function initPokiSDK(onComplete)
15
{
16
var PokiSDK = window.PokiSDK;
17
18
// No PokiSDK available, move on.
19
if (!PokiSDK) return onComplete();
20
21
// Promise callback, should be dispatched either way
22
function onInitComplete(adBlockerOn)
23
{
24
PokiSDK.adBlockerOn = adBlockerOn;
25
PokiSDK.gameLoadingStart();
26
if (onComplete) onComplete();
27
}
28
29
// Init the SDK and notify about loading start
30
return PokiSDK.init()
31
.then(function ()
32
{
33
onInitComplete(false);
34
})
35
.catch(function ()
36
{
37
onInitComplete(true);
38
});
39
}
40
41
/** Init app */
42
function initApp()
43
{
44
// Set SDK debug mode
45
if (window.PokiSDK && window.GAME_CONFIG && window.GAME_CONFIG.pokiSdkDebug !== undefined)
46
{
47
window.PokiSDK.setDebug(window.GAME_CONFIG.pokiSdkDebug);
48
}
49
50
loadScript('./js/dependencies.bundle.js');
51
loadScript(window.MAIN ? window.MAIN : './js/index.js');
52
}
53
54
/** Prevent arrows and space from scrolling browser */
55
function preventDefaultKeyboardEvents()
56
{
57
const keys = [
58
' ',
59
'ArrowUp',
60
'ArrowDown',
61
'ArrowLeft',
62
'ArrowRight',
63
];
64
65
function onKey(e)
66
{
67
if (keys.indexOf(e.key) < 0) return;
68
e.preventDefault();
69
}
70
71
window.addEventListener('keydown', onKey);
72
window.addEventListener('keyup', onKey);
73
}
74
75
function registerServiceWorker(onComplete)
76
{
77
if (navigator.serviceWorker && !window.NOSW)
78
{
79
console.log('Service worker available');
80
navigator.serviceWorker.register('./sw.js', { scope: './' })
81
.then(function ()
82
{
83
console.log('Service worker registered');
84
if (onComplete) onComplete();
85
})
86
.catch(function (error)
87
{
88
// eslint-disable-next-line prefer-template
89
console.log('Service worker registration failed - ' + error);
90
if (onComplete) onComplete();
91
});
92
}
93
else if (onComplete)
94
{
95
onComplete();
96
}
97
}
98
99
/** Prevent default keyboard events */
100
preventDefaultKeyboardEvents();
101
102
/** Register service worker if available */
103
registerServiceWorker(function ()
104
{
105
initPokiSDK(function ()
106
{
107
initApp();
108
});
109
});
110
111
112