Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
[Engine]: Documenting the CoreGameManager (JS Doc) #65
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 19, 2022
1 parent 8f3b720 commit 3b488af
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/Core/CoreGameManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { GlobalStateManagerInstance } from "../State/GlobalStateManager.js";
import { CanvasStateInstance } from "../State/CanvasState.js";

/**
* @class CoreGameManager
* @description A core class to manage the game cycle.
* @private
*/
export default class CoreGameManager {
constructor() {
if (CoreGameManager.instance instanceof CoreGameManager) return CoreGameManager.instance;
Expand All @@ -26,21 +31,42 @@ export default class CoreGameManager {
setInterval(this.updateFPS.bind(this), 1000);
}

/**
* Add a new scene to the game
* @param {Object} scene - The scene to add
* @private
*/
addScene(scene) {
const newScene = new scene();
this.scenes.push(newScene);
}

/**
* Initialize a scene and set it as the current scene
*
* @param {number} index - The index of the scene to initialize
* @private
*/
startScene(index) {
this.currentScene = this.scenes[index];
this.currentScene.start();
}

/**
* Reset the calc of the FPS
*
* @private
*/
updateFPS() {
this.fps = this._fps;
this._fps = 0;
}

/**
* Calculate the time since the game start, the delta time and the FPS
*
* @private
*/
calcTime() {
if (this.tabActive) return;

Expand All @@ -60,7 +86,12 @@ export default class CoreGameManager {
};
}

// Core function
/**
* Update the game
*
* @param {number} gameTime - The time since the game start
* @private
*/
step(gameTime) {
window.requestAnimationFrame(this.step.bind(this));
if (GlobalStateManagerInstance.isPaused) return;
Expand Down Expand Up @@ -88,12 +119,20 @@ export default class CoreGameManager {
}
}

// Run User Code
/**
* Run User Code
*
* @private
*/
update() {
this.currentScene.update(this.deltaTime, this.fps);
}

// Render
/**
* Render the game in the canvas
*
* @private
*/
render() {
const ctx = CanvasStateInstance.context;
if (!ctx) return;
Expand Down

0 comments on commit 3b488af

Please sign in to comment.