UNPKG

duckengine

Version:
259 lines (258 loc) 7.51 kB
import { Duck } from '../index'; import Scene from './scene'; import EventEmitter from './events/eventEmitter'; import PluginManager from './misc/pluginManager'; import CacheManager from './storage/cacheManager'; import CanvasRenderer from './renderer/canvas/canvasRenderer'; import DisplayManager from './display/displayManager'; /** * @class Game * @classdesc Creates a DuckEngine Game * @description The Game Class. Stores many important methods and properties. * @since 1.0.0-beta */ export default class Game { /** * @memberof Game * @description Game Configuration * @type Duck.Types.Game.Config * @since 1.0.0-beta */ readonly config: Duck.Types.Game.Config; /** * @memberof Game * @description The Canvas that is used to render to * @type HTMLCanvasElement * @since 1.0.0-beta */ canvas: HTMLCanvasElement; /** * @memberof Game * @description The Renderer used to draw and clear frames * @type CanvasRenderer * @since 2.1.0 */ renderer: CanvasRenderer; /** * @memberof Game * @description The Game Stack, holds all Scenes, and the defaultScene key * @type Duck.Types.Game.Stack * @since 1.0.0-beta */ stack: Duck.Types.Game.Stack; /** * @memberof Game * @description A reference to the animationFrame * @type number | undefined * @since 1.0.0 */ animationFrame: number | undefined; /** * @memberof Game * @description A CacheManager instance * @type CacheManager * @since 2.0.0 */ cacheManager: CacheManager; /** * @memberof Game * @description An array of the last 100 deltaTimes, deltaTime = time since last frame * @type number[] * @since 2.0.0 */ deltaTimeArray: number[]; /** * @memberof Game * @description The time since the last frame * @type number * @since 1.0.0 */ deltaTime: number; /** * @memberof Game * @description The time since the last frame averaged and smoothed out using Game.deltaTimeArray, applied to velocity of gameobjects * @type number * @since 2.0.0 */ smoothDeltaTime: number; protected oldTime: number; protected now: number; /** * @memberof Game * @description The current fps (Frames per second) that the Game loop is running at * @type number * @since 2.0.0 */ fps: number; /** * @memberof Game * @description The state of being in fullscreen, determines if the game is in fullscreen or not, changing this value does nothing * use game.fullscreen and game.unfullscreen to effect this value * @type boolean * @since 1.0.0 */ isInFullscreen: boolean; /** * @memberof Game * @description The width of the canvas that was set by the config, never changes, may not be current * @type number * @since 2.1.0 */ readonly oldWidth: number; /** * @memberof Game * @description The width of the canvas that was set by the config, never changes, may not be current * @type number * @since 2.1.0 */ readonly oldHeight: number; /** * @memberof Game * @description The scene manager, object that holds methods to add and remove scenes from the Game.stack * @type{ add: (scenes: Scene[]) => void; remove: (scene: Scene) => void }; * @since 1.0.0-beta */ scenes: { add: (scenes: Scene[]) => void; remove: (scene: Scene) => void; }; /** * @memberof Game * @description The state of the game, if it is currently rendering * @type boolean * @since 2.0.0 */ isRendering: boolean; /** * @memberof Game * @description The state of the game, if it is currently loading * @type boolean * @since 2.0.0 */ isLoaded: boolean; /** * @memberof Game * @description The source to the splash screen image that is shown during loading * @type string * @since 2.0.0 */ splashScreen: string; /** * @memberof Game * @description An EventEmitter, used by many classes other than the Game class (also used by Game class) * @type EventEmitter * @since 2.0.0 */ eventEmitter: EventEmitter; /** * @memberof Game * @description A PluginManager, stores and manages plugins * @type PluginManager * @since 2.0.0 */ pluginManager: PluginManager; /** * @memberof Game * @description A DisplayManager, manages the scaling of the canvas * @type DisplayManager * @since 2.0.0 */ displayManager: DisplayManager; /** * @memberof Game * @description The browser being used * @type string * @since 2.0.0 */ browser: string; /** * @constructor Game * @description Creates a Game instance * @param {Duck.Types.Game.Config} config Game Configuration * @since 1.0.0-beta */ constructor(config: Duck.Types.Game.Config); /** * @memberof Game * @description Starts the game loop * @emits EVENTS.GAME.LOAD_BEGIN * @emits EVENTS.GAME.DRAW_SPLASH * @emits EVENTS.GAME.LOAD_SCENE * @emits EVENTS.GAME.LOAD_FINISH * @since 1.0.0-beta */ start(): Promise<void>; /** * @memberof Game * @description Stops the game loop * @emits EVENTS.GAME.STOP * @since 1.0.0 */ stop(): void; /** * @memberof Game * @description Core loop * @since 1.0.0-beta */ protected loop(): void; /** * @memberof Game * @description Draws the splash screen to the canvas by setting the background image * @since 2.0.0 */ protected drawSplashScreen(): Promise<void>; /** * @memberof Game * @description Hides the splash screen to the canvas * @since 2.0.0 */ protected hideSplashScreen(): Promise<void>; protected sleep(ms: number): Promise<unknown>; /** * @memberof Game * @description Sets the scale of the canvas * @param {Duck.Types.Misc.Scale} scale Scale to set the canvas to * @emits EVENTS.GAME.SET_SCALE * @since 1.0.0-beta */ setScale(scale: Duck.Types.Misc.Scale): void; /** * @memberof Game * @description Sets the style background color of the canvas * @param {string} background Background color * @emits EVENTS.GAME.SET_BACKGROUND * @since 1.0.0-beta */ setBackground(background: string): void; /** * @memberof Game * @description Switches the current scene by the key * @param {string} key Key of the scene to switch from * @param {string} key2 Key of the scene to switch to * @emits EVENTS.GAME.SWITCH_SCENE * @since 1.0.0-beta */ switchScene(key: string, key2: string): void; /** * @memberof Game * @description Sets a scene to visible. Keeps the current scene visible * @param {string} key Key of the scene to show * @emits EVENTS.GAME.SHOW_SCENE * @since 1.0.0-beta */ showScene(key: string): void; /** * @memberof Game * @description Locks the pointer on the canvas * @emits EVENTS.GAME.LOCK_POINTER * @since 1.0.0 */ lockPointer(): void; /** * @memberof Game * @description Unlocks the pointer from the canvas * @emits EVENTS.GAME.UNLOCK_POINTER * @since 1.0.0 */ unlockPointer(): void; }