jsge
Version:
Javascript Game Engine
151 lines (119 loc) • 6.35 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="expires" content="Wen, 05 Jun 2024 07:01:00 GMT">
<title>JSDoc: Source: base/System.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="stage-title">Source: base/System.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import { ERROR_CODES } from "../constants.js";
import { Exception } from "./Exception.js";
import { GameStage } from "./GameStage.js";
import { ISystem } from "./ISystem.js";
import { SystemSettings } from "../configs.js";
import { LoadingStage } from "../design/LoadingStage.js";
const loadingPageName = "loadingPage";
/**
* A main app class, <br>
* Holder class for GameStage,<br>
* can register new GameStages,<br>
* init and preload data for them,<br>
*/
export class System {
/**
* @type {Map<string, Object>}
*/
#registeredStages;
/**
* @type {ISystem}
*/
#iSystem;
/**
* @param {SystemSettings} iSystemSettings - holds iSystem settings
* @param {HTMLElement | null} [canvasContainer = null] - If it is not passed, iSystem will create div element and attach it to body
*/
constructor(iSystemSettings, canvasContainer) {
if (!iSystemSettings) {
Exception(ERROR_CODES.CREATE_INSTANCE_ERROR, "iSystemSettings should be passed to class instance");
}
this.#registeredStages = new Map();
if (!canvasContainer) {
canvasContainer = document.createElement("div");
document.body.appendChild(canvasContainer);
}
this.#iSystem = new ISystem(iSystemSettings, this.#registeredStages, canvasContainer);
this.#addPreloadStage();
}
/**
* @returns {ISystem}
*/
get iSystem() {
return this.#iSystem;
}
/**
* A main factory method for create GameStage instances, <br>
* register them in a System and call GameStage.register() stage
* @param {string} screenPageName
* @param {Object} extendedGameStage - extended GameStage class(not an instance!)
*/
registerStage(screenPageName, extendedGameStage) {
if (screenPageName && typeof screenPageName === "string" && screenPageName.trim().length > 0) {
const stageInstance = new extendedGameStage();
stageInstance._register(screenPageName, this.iSystem);
this.#registeredStages.set(screenPageName, stageInstance);
} else {
Exception(ERROR_CODES.CREATE_INSTANCE_ERROR, "valid class name should be provided");
}
}
/**
* Preloads assets for all registered pages
* @return {Promise<void>}
*/
preloadAllData() {
return this.#iSystem.iLoader.preload();
}
#addPreloadStage() {
this.registerStage(loadingPageName, LoadingStage);
this.#iSystem.iLoader.addEventListener("loadstart", this.#loadStart);
this.#iSystem.iLoader.addEventListener("progress", this.#loadProgress);
this.#iSystem.iLoader.addEventListener("load", this.#loadComplete);
}
#loadStart = (event) => {
this.#iSystem.startGameStage(loadingPageName, { total: event.total });
};
#loadProgress = (event) => {
const uploaded = event.loaded,
left = event.total,
loadingPage = this.#registeredStages.get(loadingPageName);
loadingPage._progress(uploaded, left);
};
#loadComplete = () => {
this.#iSystem.stopGameStage(loadingPageName);
};
}</code></pre>
</article>
</section>
</div>
<nav>
<div class="switcher"><ul><li class="active"><a href="/">1.5.9</a></li></div>
<h2><a href="/">Home</a></h2><h3>Classes</h3><ul><li><a href="DrawCircleObject.html">DrawCircleObject</a></li><li><a href="DrawConusObject.html">DrawConusObject</a></li><li><a href="DrawImageObject.html">DrawImageObject</a></li><li><a href="DrawLineObject.html">DrawLineObject</a></li><li><a href="DrawObjectFactory.html">DrawObjectFactory</a></li><li><a href="DrawPolygonObject.html">DrawPolygonObject</a></li><li><a href="DrawRectObject.html">DrawRectObject</a></li><li><a href="DrawShapeObject.html">DrawShapeObject</a></li><li><a href="DrawTextObject.html">DrawTextObject</a></li><li><a href="DrawTiledLayer.html">DrawTiledLayer</a></li><li><a href="GameStage.html">GameStage</a></li><li><a href="GameStageData.html">GameStageData</a></li><li><a href="IExtension.html">IExtension</a></li><li><a href="INetwork.html">INetwork</a></li><li><a href="IRender.html">IRender</a></li><li><a href="ISystem.html">ISystem</a></li><li><a href="ISystemAudio.html">ISystemAudio</a></li><li><a href="RenderLoop.html">RenderLoop</a></li><li><a href="RenderLoopDebug.html">RenderLoopDebug</a></li><li><a href="System.html">System</a></li><li><a href="SystemSettings.html">SystemSettings</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-application_scheme.html">Application Scheme</a></li><li><a href="tutorial-assets_manager.html">Assets Manager</a></li><li><a href="tutorial-common_issues.html">Common Issues</a></li><li><a href="tutorial-how_to_add_and_use_audio.html">How to add and use audio</a></li><li><a href="tutorial-how_to_do_animations.html">How to Create Animations</a></li><li><a href="tutorial-how_to_load_and_use_tilemaps.html">How to Load and Use Tilemaps</a></li><li><a href="tutorial-quick_start.html">Quick Start</a></li><li><a href="tutorial-spine_animations.html">How to Add Spine Animations</a></li><li><a href="tutorial-stages_lifecycle.html">Stages Lifecycle</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Wed Jun 18 2025 06:53:54 GMT+0000 (Coordinated Universal Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>