* @ignore
(levelId, options, restart)
| 17 | * @ignore |
| 18 | */ |
| 19 | function 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 |