jetlag2d
    Preparing search index...

    Class CameraService

    The Camera is used to determine /how much/ of a world to render, and /which part/. The Camera can have a minimum X and Y coordinate, and a maximum X and Y coordinate. It also has a zoom factor, and a current center point.

    The zoom factor and center point give a rectangular region. The min and max coordinates give another rectangular region. If the first region is not fully within the second, we shift it so that it is within, and then we only show those things that are within it.

    Note that the camera center can be changed dynamically, in response to changes in the world to which the camera is attached.

    Index
    • get scale(): number

      The pixel/meter ratio of the camera. It is computed from the zoom and the size of the screen, so it works with resizing.

      Returns number

    • get zoom(): number

      How far the camera is zoomed. 1 is the starting view, 2 is twice as close, and 0.5 is twice as far.

      Returns number

    • set zoom(z: number): void

      Set how far the camera is zoomed (1 is the starting view), and redraw

      Parameters

      • z: number

      Returns void

    • Convert world coordinates (meters) to screen coordinates (absolute pixels)

      Parameters

      • world: { x: number; y: number }
        • x: number

          The X coordinate, in meters

        • y: number

          The Y coordinate, in meters

      Returns { x: number; y: number }

      • x: number

        The X coordinate, in pixels

      • y: number

        The Y coordinate, in pixels

    • Convert screen coordinates (absolute pixels), to world coordinates (meters)

      Parameters

      • screen: { x: number; y: number }
        • x: number

          The X coordinate, in pixels

        • y: number

          The Y coordinate, in pixels

      Returns { x: number; y: number }

      • x: number

        The X coordinate, in meters

      • y: number

        The Y coordinate, in meters

    • Update a camera's bounds by providing a new maximum (X, Y) coordinate

      Parameters

      • cfg: { maxX?: number; maxY?: number; minX?: number; minY?: number }
        • OptionalmaxX?: number

          The new maximum X value (in meters)

        • OptionalmaxY?: number

          The new maximum Y value (in meters)

        • OptionalminX?: number

          The new minimum X value (in meters)

        • OptionalminY?: number

          The new minimum Y value (in meters)

      Returns void

    • Make the camera follow an Actor. Optionally add an offset to the actor's center, and center the camera on that point instead. Optionally give a lag, so that the camera eases toward that point instead of staying exactly on it.

      Parameters

      • actor: Actor<unknown> | undefined

        The actor to follow

      • offset: { dx: number; dy: number } = ...
        • dx: number

          Amount of x distance to add to actor center

        • dy: number

          Amount of y distance to add to actor center

      • lag: number = 0

        How many seconds the camera takes to close most of the distance to the point it is following. The default, 0, keeps the camera exactly on that point.

      Returns void

    • Set the center point on which the camera should focus. If the requested center would push the camera's viewport past the camera's bounds, the center is shifted inward just enough to keep the viewport within bounds, rather than showing empty space past the edge of the world.

      NB: This is called (indirectly) by the render loop in order to make sure we don't go out of bounds.

      Parameters

      • center: { x: number; y: number }
        • x: number

          The X coordinate of the center point (in meters)

        • y: number

          The Y coordinate of the center point (in meters)

      Returns void

    • Shake the camera, so that a hit or an explosion can be felt. The shake starts at the given size and dies down to nothing. It is applied on top of wherever the camera is, so it works whether or not the camera is following an Actor. A second shake replaces the first, unless it would be weaker.

      Parameters

      • seconds: number

        How long the shake lasts

      • size: number

        How far, in meters, the camera is thrown at the start

      Returns void