import GameObject from './GameObject.js'; import Scene from './Scene.js'; import Camera from './Camera.js'; import RenderingSystem from '../systems/RenderingSystem.js'; import type { CollisionManifold } from '../physics/Physics.js'; import { ObjectSet } from './ObjectSet.js'; /** * Options for initialising the Engine. */ export interface EngineOpts { /** The ID of the HTML element to inject the canvas into. */ containerId?: string; /** Width of the game window (e.g., '800px' or '100%'). */ width: string; /** Height of the game window (e.g., '600px' or '100%'). */ height: string; /** Title of the game, set in the document head. */ title: string; /** Background colour of the canvas. */ backgroundColour: string; } /** * Lifecycle and update callbacks for the engine. */ export interface EngineCallbacks { /** Called once after the engine is initialised. */ onLoad?: () => void; /** Called every frame for game logic and rendering. */ update: () => void; /** Optional callback for physics or fixed-step logic. */ fixedUpdate?: () => void; } /** * The core singleton class that manages the game loop, rendering, and scene state. */ export default class Engine { /** Internal helper to access the global shared state. */ private static get _state(); /** * Resets the engine state to its initial values. * Useful for testing to prevent state bleed between test cases. */ static resetState(): void; /** * Global set of game objects if no scene is active. */ static get objects(): ObjectSet; /** * Global list of systems that process game objects. */ private static get _systems(); /** * The global rendering system. */ static get renderingSystem(): RenderingSystem | undefined; static set renderingSystem(val: RenderingSystem | undefined); /** * Recent collisions for debug visualization. */ static get debugCollisions(): { manifold: CollisionManifold; timestamp: number; }[]; static set debugCollisions(val: { manifold: CollisionManifold; timestamp: number; }[]); /** * Whether to show velocity/acceleration vectors in debug mode. */ static get showPhysicsVectors(): boolean; static set showPhysicsVectors(val: boolean); /** * Whether to show collision normals/contact points in debug mode. */ static get showCollisionLines(): boolean; static set showCollisionLines(val: boolean); /** * Whether the game loop is currently paused. */ static get paused(): boolean; static set paused(val: boolean); /** * Returns the total active time of the game (excluding pause time). */ static get totalTime(): number; /** * Toggle for visual debug mode (hitboxes and stats). */ static get debug(): boolean; static set debug(val: boolean); /** * Internal event bus for engine events. */ private static get events(); /** * Listens for an event on the global engine bus. * @param type The event type (e.g., 'paused', 'PLAYER_DIED'). * @param callback The function to run when the event occurs. */ static on(type: string, callback: (event: CustomEvent) => void): void; /** * Stops listening for an event on the global engine bus. * @param type The event type. * @param callback The function to remove. */ static off(type: string, callback: (event: CustomEvent) => void): void; /** * Emits a custom event on the global engine bus. * @param type The event type. * @param detail Optional data to pass with the event. */ static emit(type: string, detail?: unknown): void; /** * The currently selected object in debug mode. */ static get selectedObject(): GameObject | null; static set selectedObject(val: GameObject | null); /** * Whether the global object list needs to be re-sorted by z-index. */ static get zOrderDirty(): boolean; static set zOrderDirty(val: boolean); /** * The cached list of globally registered game objects, sorted by z-index. */ static get sortedObjects(): GameObject[]; static set sortedObjects(val: GameObject[]); /** * The global camera instance. */ static get camera(): Camera; /** * Gets the currently active scene. */ static get currentScene(): Scene | null; /** * Sets the active scene, resetting the camera and calling the scene's onLoad. */ static set currentScene(scene: Scene | null); private _canvas; private _ctx; private _window; private _title; private _isWindowInternal; private _destroyed; private _resizeHandler; /** Current background colour. */ backgroundColour: string; /** Registered engine callbacks. */ callbacks: EngineCallbacks; /** Pixel width of the canvas. */ width: number; /** Pixel height of the canvas. */ height: number; /** * Current mouse x position in world space. */ get mouseX(): number; /** * Current mouse y position in world space. */ get mouseY(): number; /** Current frames per second (rolling average). */ fps: number; /** Current frame time in milliseconds. */ frameTime: number; private _oldTimestamp; private _secondsPassed; private _accumulator; private _fixedDelta; private _fpsValues; /** * initialises a new instance of the Engine. * @param callbacks Lifecycle and update callbacks. * @param opts Configuration options for the engine. */ constructor(callbacks: EngineCallbacks, opts?: Partial); /** * Completely stops the engine and cleans up all resources. */ terminate(): void; private _onLoad; private _update; private _fixedUpdate; private _setBackground; private _draw; /** * Cleans up expired debug collisions. * @param now Current timestamp. */ static cleanDebugCollisions(now?: number): void; private _drawDebug; /** * Schedules a function to run after a delay. * @param callback The function to run. * @param delay The delay in milliseconds. * @returns A timer ID. */ setTimeout(callback: () => void, delay: number): number; /** * Starts a countdown timer that runs a function every second and an ending function. * @param milliseconds The total duration of the countdown. * @param fn The function to run every second. * @param onEnded The function to run when the countdown is finished. */ countdown(milliseconds: number, fn: () => void, onEnded: () => void): void; /** * Sets the CSS cursor style for the game canvas. * @param value The CSS cursor value (e.g., 'pointer', 'crosshair'). */ set cursor(value: string); /** * Registers a game object with the active scene or global engine loop. * @param object The object to register. */ static registerObject(object: GameObject): void; /** * Removes a game object from the active scene or global engine loop. * @param object The object to destroy. */ static destroyObject(object: GameObject): void; /** * Destroy all objects in the active scene or global engine. */ static destroyAll(): void; }