Pockety is a minimalistic game engine, and as such it keeps its API quite minimal as well.
The engine lifecycle is the following:
As the page is loaded, the StartGame function inside the game.js file is called. In this function, we expect to implement the entirety of our game loop.
const StartGame = async () => {
// First, a new instance of the engine must be initialised
// this method accepts 2 arguments: width and height.
// if they are set to "auto", they will expand to fit the page
let pockety = new Pockety("auto", 300);
}
The game can then be started by calling the Run(scene) function.
At this point the engine will perform the following operations:
// first the engine will call the internal _start function
_start = (startingScene) => {
this.StopAllAudio();
this.startTimeMS = Date.now();
// the LoadScene() function, not only loads the entities in the activeScene
// but it also calls the Start() function in each Entity in the scene
this.LoadScene(startingScene);
this._gameLoop();
};
// then the _gameLoop starts. This function is called every Frame
_gameLoop = () => {
// if the engine has Quit, we don't run the game
if (!this.isRunning) return;
// we calculate the delta time (time between Frame renders)
this.deltaTime = (Date.now() - this.startTimeMS) / 1000;
// if there is a touch or click, we run the OnTouch() function
if (this.touching) this.OnTouch();
// we call the Update() function in each Entity in the scene
this.activeScene.Update(this.deltaTime);
// then we run collision detection, this notifies each colliding Entity
this._collisionDetection();
// finally, we draw everything on the canvas
this._render(this.deltaTime);
// here we update the last pressed keys
this.lastKeys = this.keysPressed.slice(0);
// we then reset the start time for delta time calculations
this.startTimeMS = Date.now();
// and finally, we request the next frame to the canvas
requestAnimationFrame(this._gameLoop);
};
// the Quit function simply stops the engine from running the game
Quit = () => {
this.isRunning = false;
this.StopAllAudio();
};