Options
All
  • Public
  • Public/Protected
  • All
Menu

Follows a subject to provide a viewport into a game world.

Example

Assumes this is in a Scene.

const camera = new Camera(player, { width: 640, height: 480 } as Rect, {
width: map.mapWidth * map.tileWidth,
height: map.mapHeight * map.tileHeight
} as Rect)

this.camera = this.add<Camera>(camera)

Hierarchy

Index

Constructors

constructor

Properties

alpha

alpha: number

The opacity of the entity (from 0 to 1)

children

children: Partial<Entity>[]

An array of child entities for recursive update/render.

dead

dead: boolean

Whether or not the entity is dead.

easing

easing: number

Amount to move towards subject per update.

Range from 0 to 1 where 0 is not moving, and 1 is always on subject. Default is .3

pos

pos: Vec

Position of the entity.

scale

scale: Vec

Scale of the entity.

visible

visible: boolean

Whether or not the entity is visible.

Accessors

hasChildren

  • get hasChildren(): boolean

Methods

add

  • add<T>(child: T): T
  • Add and return a child object.

    Type parameters

    • T

    Parameters

    • child: T

      Object to add to the container.

    Returns T

flash

  • flash(duration?: number, color?: string): void
  • Add a brief overlay to 'flash' the viewport.

    Parameters

    • Default value duration: number = 0.2

      Time in seconds for flash to fade out.

    • Default value color: string = "#fff"

      Color of the overlay used.

    Returns void

focus

  • focus(ease?: number, track?: boolean): void
  • Focus the camera on the subject.

    Parameters

    • Default value ease: number = 1

      Range from 0 to 1 where 0 is not moving, and 1 is on subject.

    • Default value track: boolean = true

      Whether or not to keep the subject in tracking limits.

    Returns void

map

  • map(fn: (child: Partial<Entity>, index?: number) => void): any
  • Map a function across all children.

    Parameters

    • fn: (child: Partial<Entity>, index?: number) => void

      Function to call for each child.

        • (child: Partial<Entity>, index?: number): void
        • Parameters

          • child: Partial<Entity>
          • Optional index: number

          Returns void

    Returns any

remove

  • remove<T>(child: T): T
  • Remove and return a child object.

    Type parameters

    • T

    Parameters

    • child: T

      Object to remove from the container.

    Returns T

setDebug

  • setDebug(debug?: boolean): void
  • Set debug status on camera.

    When true, a rectangle displays on-screen showing camera tracking bounds.

    Only call after everything is added to the camera.

    Parameters

    • Default value debug: boolean = true

      Whether to turn debugging on or off.

    Returns void

setSubject

  • Update the subject being tracked by the camera.

    Also causes the camera to refocus to the new subject.

    Parameters

    Returns void

setTracking

  • setTracking(xo: number, yo: number): void
  • Update tracking offsets used to keep the subject in view.

    Parameters

    • xo: number

      Offset of tracking space from sides on X axis.

    • yo: number

      Offset of tracking space from sides on Y axis.

    Returns void

shake

  • shake(power?: number, duration?: number): void
  • Shake the camera randomly for a short time.

    Parameters

    • Default value power: number = 5

      Number of pixels to shake the camera.

    • Default value duration: number = 0.5

      Time in seconds to shake.

    Returns void

update

  • update(dt: number, t: number): void
  • Update the camera.

    Parameters

    • dt: number

      Delta time since last update.

    • t: number

      Timestamp in seconds.

    Returns void