UNPKG

duckengine

Version:
256 lines (255 loc) 8.62 kB
import Game from '../game'; import Scene from '../scene'; import { Duck } from '../../index'; /** * @class Camera * @classdesc Creates a DuckEngine Camera * @description The Camera Class. Creates and uses a viewport that acts like a camera * @since 1.0.0-beta */ export default class Camera { /** * @memberof Camera * @description Game instance * @type Game * @since 1.0.0-beta */ game: Game; /** * @memberof Camera * @description Scene instance * @type Scene * @since 2.0.0 */ scene: Scene; protected lookAt: number[]; /** * @memberof Camera * @description The Camera's current FOV * @type number * @since 2.1.0 */ fieldOfView: number; /** * @memberof Camera * @description The Camera's current zoom * @type number * @since 2.1.0 */ distance: number; /** * @memberof Camera * @description Camera's viewport, has info about the viewport * @since 1.0.0-beta */ viewport: { left: number; right: number; top: number; bottom: number; w: number; h: number; scale: number[]; }; protected aspectRatio: number | undefined; /** * @memberof Camera * @description Determines if the camera is a main camera in the attached scene. Must be added by Scene.mainCamera * @type boolean * @since 1.0.0-beta */ readonly isMain: boolean; protected bounds: { position: { x: number; y: number; }; w: number; h: number; } | undefined; /** * @memberof Camera * @description A gameobject that the Camera is currently following * @type Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type> | undefined * @since */ following: Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type> | undefined; protected lerpX: number; protected lerpY: number; /** * @constructor Camera * @description Creates a Camera instance. * @param {Game} game Game instance * @param {Scene} scene Scene instance * @since 1.0.0-beta */ constructor(game: Game, scene: Scene); /** * @memberof Camera * @description Begins camera path * * DO NOT CALL MANUALLY, CALLED IN GAME -> SCENE LOOP AUTOMATICALLY * * @since 1.0.0-beta */ begin(): void; /** * @memberof Camera * @description Ends camera path * * DO NOT CALL MANUALLY, CALLED IN GAME -> SCENE LOOP AUTOMATICALLY * * @since 1.0.0-beta */ end(): void; protected applyScale(): void; protected applyTranslation(): void; protected updateViewport(): void; /** * @memberof Camera * @description Sets the zoom * @param {number} z Zoom value * @since 1.0.0-beta */ setZoom(z: number): void; /** * @memberof Camera * @description Sets the zoom smoothly * @param {number} intervalMS How often zoom is modified by smoothValue * @param {number} smoothValue The number that is added to zoom on an interval * @param {number} z Target Zoom value * @since 1.0.0-beta */ setZoomSmooth(intervalMS: number, smoothValue: number, z: number): void; /** * @memberof Camera * @description Moves the camera to a position * @param {number} x X position * @param {number} y Y position * @since 1.0.0-beta */ moveTo(x: number, y: number): void; /** * @memberof Camera * @description Follows a GameObject * @param {Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>} gameObject Game object to follow * @param {number} [lerpX=1] Lerp on the x axis, optional -> defaults: 1 * @param {number} [lerpY=1] Lerp on the y axis, optional -> defaults: 1 * @since 1.0.0-beta */ startFollow(gameObject: Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>, lerpX?: number, lerpY?: number): void; /** * @memberof Camera * @description Stops following a gameobject * @since 1.0.0-beta */ stopFollow(): void; /** * @memberof Camera * @description Transforms a coordinate pair from screen coordinates (relative to the canvas) into world coordinates * @param {number} x X position * @param {number} y Y position * @param {Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>} obj GameObject * @since 1.0.0-beta */ screenToWorld(x: number, y: number, obj: Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>): Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>; /** * @memberof Camera * @description Transforms a coordinate pair from world coordinates into screen coordinates (relative to the canvas) - * useful for interacting with a Button while having a camera * @param {number} x X position * @param {number} y Y position * @param {Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>} obj GameObject * @since 1.0.0-beta */ worldToScreen(x: number, y: number, obj: Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>): Duck.TypeClasses.GameObjects.GameObject<Duck.Types.Texture.Type>; /** * @memberof Camera * @description Sets the FOV * @param {number} f FOV value * @since 1.0.0-beta */ setFOV(f: number): void; /** * @memberof Camera * @description Sets the FOV smoothly * @param {number} intervalMS How often FOV is modified by smoothValue * @param {number} smoothValue The number that is added to FOV on an interval * @param {number} f Target FOV value * @since 1.0.0-beta */ setFOVSmooth(intervalMS: number, smoothValue: number, f: number): void; /** * @memberof Camera * @description Resets the FOV to the default value * @since 1.0.0-beta */ resetFOV(): void; /** * @memberof Camera * @description Resets the Zoom to the default value * @since 1.0.0-beta */ resetZoom(): void; /** * @memberof Camera * @description Bounds of the Camera, an area the camera can be in * @param {{ position: { x:number; y:number }; w:number; h:number; }} bounds An object with the bounds * @since 1.0.0-beta */ setBounds(bounds: { position: { x: number; y: number; }; w: number; h: number; }): void; /** * @memberof Camera * @description Shakes the camera * @param {number} intervalMS How often (in milliseconds) the passed value is added/subtracted from the Camera viewport position * @param {number} timeMS For how long in milliseconds the shake lasts * @param {number} v Value to be added/subtracted from the Camera viewport position * @since 1.0.0 */ shake(intervalMS: number, timeMS: number, v: number): void; /** * @memberof Camera * @description Culls/Renders objects that are passed and does not render other object that are not passed * @param {Duck.Types.Renderable[]} renderableObjects Objects that should be culled/rendered * @param {Duck.Types.Camera.CullingOptions} [options] Options to modify how objects are culled, optional -> defaults: * { preserveVisibility: true, modifyPhysicsEnable: true } * * Notes: * - Calls CanvasRenderer.pipeline.pool() ignoring the pool interval * * @since 2.0.0 */ cull(renderableObjects: Duck.Types.Renderable[], options?: Duck.Types.Camera.CullingOptions): void; /** * @memberof Camera * @description A form of Frustum Culling that gets all objects visible to the player by the viewport's width and height and culls those objects * and does not render objects outside/not-visible to the player/camera * @param {Duck.Types.Camera.CullingOptions} [options] Options to modify how objects are culled, optional -> defaults: * { preserveVisibility: true, modifyPhysicsEnable: true } * * Notes: * - Calls CanvasRenderer.pipeline.pool() ignoring the pool interval * * @since 2.0.0 */ autoCull(options?: Duck.Types.Camera.CullingOptions): void; /** * @memberof Camera * @description Returns the default zoom, also uses DPR scaling if enabled * @since 1.0.0 */ get defaultZoom(): number; /** * @memberof Camera * @description Returns the default FOV * @since 1.0.0 */ get defaultFOV(): number; }