Class Game

Represents a game engine that manages scenes, assets, and game state.

Constructors

Properties

animationFrame?: number

Animation frame ID for the game loop.

audioVolume: number = 1

Game global audio volume.

avgFPS: number = 0

Game average frames per second.

backgroundColor: Color = DefaultColors.Black

Background color of the game.

canvas: HTMLCanvasElement

Canvas element for rendering the game.

config: GameConfig

The game configuration.

ctx: CanvasRenderingContext2D

Canvas rendering context.

currentScene: null | Scene = null

Current scene being played.

debug: boolean = false

Debug mode flag.

delta: number = ...

Game delta time.

draw: Draw = ...

Draw object for rendering 2D shapes and images.

frame: number = 0

Game frame counter.

frameRate: number = 60

Game frame rate.

frameTimeBuffer: number = 0

Game frame time buffer.

frameTimeLast: number = 0

Game frame time last.

height: number = window.innerHeight

Game height.

images: Record<string, HTMLImageElement> = {}

Image assets record containing image elements.

input: Input = ...

Input object for handling user input.

objectClasses: Record<string, Constructable<Entity>> = {}

Object classes record containing entity classes.

paused: boolean = false

Game pause flag.

pixelPerfect: boolean = false

Flag indicating whether the game should render pixel-perfect.

postProcess?: PostProcess

The post-processing effect to apply to the game.

preload: Record<string, string>

The preload record.

preloadPercent: number = -1

Game preloader percent.

primaryColor: Color = DefaultColors.White

Primary color. Used for text, and other UI elements like preloader.

sceneClasses: Record<string, Constructable<Scene>> = {}

Scene classes record containing scene classes.

scenes: Record<string, Scene> = {}

Scene record containing all scenes.

settings: Record<string, any> = {}

Optional settings for the game.

sounds: Record<string, Howl> = {}

Sound assets record containing Howl objects.

time: number = 0

Game time.

timeReal: number = 0

Game time in real seconds.

width: number = window.innerWidth

Game width.

Methods

  • Retrieves an image by its name from the assets.

    Parameters

    • name: string

      The name of the image to retrieve.

    Returns HTMLImageElement

    The HTMLImageElement corresponding to the specified name.

    Error if the image is not found in the assets.

  • Retrieves the value of a setting based on the provided key.

    Parameters

    • key: string

      The key of the setting to retrieve.

    Returns any

    The value of the setting.

  • Retrieves a TmxMap object from the session storage based on the provided name. If the object is not found, an empty object is returned.

    Parameters

    • name: string

      The name of the TmxMap object to retrieve.

    Returns any

    The retrieved TmxMap object or an empty object if not found.

  • Plays the specified scene.

    Parameters

    • sceneName: string

      The name of the scene to play.

    • restart: boolean = false

      Whether to restart the scene if it is already playing. Default is false.

    Returns void

    Error if the specified scene is not found.

  • Plays the sound with the specified name.

    Parameters

    • name: string

      The name of the sound to play.

    Returns void

  • Sets a setting value for the game.

    Parameters

    • key: string

      The key of the setting.

    • value: any

      The value to set for the setting.

    Returns void

  • Starts the game engine and initializes the scenes.

    Parameters

    • OptionalsceneName: string

      Optional name of the scene to start with.

    Returns Promise<void>

  • Toggles the pause state of the game.

    Parameters

    • paused: boolean = !this.paused

      Optional. Specifies whether the game should be paused or not. Default is true.

    Returns void

  • Updates the game state and renders the scene.

    Parameters

    • frameTime: number = 0

      The time elapsed since the last frame in milliseconds.

    Returns void