MCPcopy Create free account
hub / github.com/melonjs/melonJS / safeLoadLevel

Function safeLoadLevel

packages/melonjs/src/level/level.js:19–61  ·  view source on GitHub ↗

* @ignore

(levelId, options, restart)

Source from the content-addressed store, hash-verified

17 * @ignore
18 */
19function safeLoadLevel(levelId, options, restart) {
20 // clean the destination container
21 options.container.reset();
22
23 // reset the renderer
24 game.reset();
25
26 // clean the current (previous) level
27 if (levels[level.getCurrentLevelId()]) {
28 levels[level.getCurrentLevelId()].destroy();
29 }
30
31 // update current level index
32 currentLevelIdx = levelIdx.indexOf(levelId);
33
34 // add the specified level to the game world. TMX maps keep their
35 // dedicated loader (GUID reset + viewport bounds + object flattening);
36 // other formats (glTF/GLB scenes, …) use the generic duck-typed
37 // `addTo(container, options)` interface.
38 const targetLevel = levels[levelId];
39 if (targetLevel.format === "tmx") {
40 loadTMXLevel(
41 levelId,
42 options.container,
43 options.flatten,
44 options.setViewportBounds,
45 );
46 } else {
47 options.container.anchorPoint.set(0, 0);
48 targetLevel.addTo(options.container, options);
49 }
50
51 // publish the corresponding message
52 emit(LEVEL_LOADED, levelId);
53
54 // fire the callback
55 options.onLoaded(levelId);
56
57 if (restart) {
58 // resume the game loop if it was previously running
59 state.restart();
60 }
61}
62
63/**
64 * Load a TMX level

Callers 1

loadFunction · 0.85

Calls 7

emitFunction · 0.90
loadTMXLevelFunction · 0.85
restartMethod · 0.80
destroyMethod · 0.65
resetMethod · 0.45
setMethod · 0.45
addToMethod · 0.45

Tested by

no test coverage detected