-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.js
218 lines (190 loc) · 6.61 KB
/
application.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
System.register([], function (_export, _context) {
"use strict";
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function createApplication(_ref) {
var loadJsListFile = _ref.loadJsListFile,
fetchWasm = _ref.fetchWasm;
// NOTE: before here we shall not import any module!
var promise = Promise.resolve();
promise = promise.then(function () {
return topLevelImport('wait-for-ammo-instantiation');
}).then(function (_ref2) {
var waitForAmmoInstantiation = _ref2["default"];
var isWasm = waitForAmmoInstantiation.isWasm,
wasmBinaryURL = waitForAmmoInstantiation.wasmBinaryURL;
if (!isWasm) {
return waitForAmmoInstantiation();
} else {
return Promise.resolve(fetchWasm(wasmBinaryURL)).then(function (wasmBinary) {
return waitForAmmoInstantiation(wasmBinary);
});
}
});
return promise.then(function () {
return _defineProperty({
start: start
}, 'import', topLevelImport);
});
function start(_ref4) {
var findCanvas = _ref4.findCanvas;
var settings;
var cc;
return Promise.resolve().then(function () {
return topLevelImport('cc');
}).then(function (engine) {
cc = engine;
return loadSettingsJson(cc);
}).then(function () {
settings = window._CCSettings;
return initializeGame(cc, settings, findCanvas).then(function () {
if (settings.scriptPackages) {
return loadModulePacks(settings.scriptPackages);
}
}).then(function () {
return loadJsList(settings.jsList);
}).then(function () {
return loadAssetBundle(settings.hasResourcesBundle, settings.hasStartSceneBundle);
}).then(function () {
return cc.game.run(function () {
return onGameStarted(cc, settings);
});
});
});
}
function topLevelImport(url) {
return _context["import"]("".concat(url));
}
function loadAssetBundle(hasResourcesBundle, hasStartSceneBundle) {
var promise = Promise.resolve();
var _cc$AssetManager$Buil = cc.AssetManager.BuiltinBundleName,
MAIN = _cc$AssetManager$Buil.MAIN,
RESOURCES = _cc$AssetManager$Buil.RESOURCES,
START_SCENE = _cc$AssetManager$Buil.START_SCENE;
var bundleRoot = hasResourcesBundle ? [RESOURCES, MAIN] : [MAIN];
if (hasStartSceneBundle) {
bundleRoot.push(START_SCENE);
}
return bundleRoot.reduce(function (pre, name) {
return pre.then(function () {
return loadBundle(name);
});
}, Promise.resolve());
}
function loadBundle(name) {
return new Promise(function (resolve, reject) {
cc.assetManager.loadBundle(name, function (err, bundle) {
if (err) {
return reject(err);
}
resolve(bundle);
});
});
}
function loadModulePacks(packs) {
return Promise.all(packs.map(function (pack) {
return topLevelImport(pack);
}));
}
function loadJsList(jsList) {
var promise = Promise.resolve();
jsList.forEach(function (jsListFile) {
promise = promise.then(function () {
return loadJsListFile("src/".concat(jsListFile));
});
});
return promise;
}
function loadSettingsJson(cc) {
var server = '';
var settings = 'src/settings.json';
return new Promise(function (resolve, reject) {
if (typeof fsUtils !== 'undefined' && !settings.startsWith('http')) {
var result = fsUtils.readJsonSync(settings);
if (result instanceof Error) {
reject(result);
} else {
window._CCSettings = result;
window._CCSettings.server = server;
resolve();
}
} else {
var requestSettings = function requestSettings() {
var xhr = new XMLHttpRequest();
xhr.open('GET', settings);
xhr.responseType = 'text';
xhr.onload = function () {
window._CCSettings = JSON.parse(xhr.response);
window._CCSettings.server = server;
resolve();
};
xhr.onerror = function () {
if (retryCount-- > 0) {
setTimeout(requestSettings, retryInterval);
} else {
reject(new Error('request settings failed!'));
}
};
xhr.send(null);
};
var retryCount = 3;
var retryInterval = 2000;
requestSettings();
}
});
}
}
function initializeGame(cc, settings, findCanvas) {
if (settings.macros) {
for (var key in settings.macros) {
cc.macro[key] = settings.macros[key];
}
}
var gameOptions = getGameOptions(cc, settings, findCanvas);
return Promise.resolve(cc.game.init(gameOptions));
}
function onGameStarted(cc, settings) {
window._CCSettings = undefined;
cc.view.enableRetina(true);
cc.view.resizeWithBrowserSize(true);
if (cc.sys.isMobile) {
if (settings.orientation === 'landscape') {
cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
} else if (settings.orientation === 'portrait') {
cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
}
cc.view.enableAutoFullScreen(false);
}
var launchScene = settings.launchScene; // load scene
cc.director.loadScene(launchScene, null, function () {
cc.view.setDesignResolutionSize(2600, 5500, 2);
console.log("Success to load scene: ".concat(launchScene));
});
}
function getGameOptions(cc, settings, findCanvas) {
// asset library options
var assetOptions = {
bundleVers: settings.bundleVers,
remoteBundles: settings.remoteBundles,
server: settings.server,
subpackages: settings.subpackages
};
var options = {
debugMode: settings.debug ? cc.DebugMode.INFO : cc.DebugMode.ERROR,
showFPS: !false && settings.debug,
frameRate: 60,
groupList: settings.groupList,
collisionMatrix: settings.collisionMatrix,
renderPipeline: settings.renderPipeline,
adapter: findCanvas('GameCanvas'),
assetOptions: assetOptions,
customJointTextureLayouts: settings.customJointTextureLayouts || [],
physics: settings.physics
};
return options;
}
_export("createApplication", createApplication);
return {
setters: [],
execute: function () {}
};
});