GameLoop
The GameLoop
class is a singleton that handles the game loop. It manages update and render subscribers, as well as calculating the delta time and frame rate.
Prefer to get the unique instance of this service from the ServiceContainer
like:
const gameLoop = ServiceContainer.gameLoop;
Getters
instance
- Type:
GameLoop
Returns the singleton instance of the GameLoop
class.
FPS
- Type:
number
Returns the current frames per second (FPS) as a number.
Methods
setFreezeLimit
setFreezeLimit(limit: number): GameLoop
Sets the freeze limit in seconds.
Parameters
limit
- Type:
number
- The freeze limit in seconds
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
ServiceContainer.GameLoop.setFreezeLimit(0.1);
subscribeToUpdate
subscribeToUpdate(subscriber: UpdateFunction): GameLoop
Subscribes a function to the update loop.
Parameters
subscriber
- Type:
UpdateFunction
- The function to subscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
ServiceContainer.GameLoop.subscribeToUpdate((deltaTime) => {
console.log(`deltaTime: ${deltaTime}`);
});
unsubscribeFromUpdate
unsubscribeFromUpdate(subscriber: UpdateFunction): GameLoop
Unsubscribes a function from the update loop.
Parameters
subscriber
- Type:
UpdateFunction
- The function to unsubscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
const updateFunction = (deltaTime) => {
console.log(`deltaTime: ${deltaTime}`);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToUpdate(updateFunction);
//...
gameLoop.unsubscribeFromUpdate(updateFunction);
subscribeToPreRender
subscribeToPreRender(subscriber: DrawFunction): GameLoop
Subscribes a function to the pre-render loop.
Parameters
subscriber
- Type:
DrawFunction
- The function to subscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
ServiceContainer.GameLoop.subscribeToPreRender((ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
});
unsubscribeFromPreRender
unsubscribeFromPreRender(subscriber: DrawFunction): GameLoop
Unsubscribes a function to the pre-render loop.
Parameters
subscriber
- Type:
DrawFunction
- The function to unsubscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
const preRenderFunction = (ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToPreRender(preRenderFunction);
//...
gameLoop.unsubscribeFromPreRender(preRenderFunction);
subscribeToPostRender
subscribeToPostRender(subscriber: DrawFunction): GameLoop
Subscribes a function to the post-render loop.
Parameters
subscriber
- Type:
DrawFunction
- The function to subscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
ServiceContainer.GameLoop.subscribeToPostRender((ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
});
unsubscribeFromPostRender
unsubscribeFromPostRender(subscriber: DrawFunction): GameLoop
Unsubscribes a function from the post-render loop.
Parameters
subscriber
- Type:
DrawFunction
- The function to unsubscribe
- Type:
Return
GameLoop
The instance of the GameLoop class.
Example
const postRenderFunction = (ctx) => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
};
const gameLoop = ServiceContainer.GameLoop;
gameLoop.subscribeToPostRender(postRenderFunction);
//...
gameLoop.unsubscribeFromPostRender(postRenderFunction);