Skip to main content

Class: World

world.World

Represents the 2D world.

Implements

Constructors

constructor

โ€ข new World(cellSize, cameraViewport)

Creates a World instance.

Parameters

NameTypeDescription
cellSizevec2The width and height of each world cell in pixels
cameraViewportvec2The width and height of the camera's viewport in pixels

Defined in

src/world.ts:35

โ€ข new World(cellSize)

Creates a World instance with a dynamic viewport that resizes to the Renderer's dimensions.

Parameters

NameTypeDescription
cellSizevec2The width and height of each world cell in pixels

Defined in

src/world.ts:42

Properties

camera

โ€ข Private camera: Camera

Defined in

src/world.ts:20


cellSize

โ€ข cellSize: vec2

Defined in

src/world.ts:17


entities

โ€ข Private entities: Entity[] = []

Defined in

src/world.ts:21


entityListeners

โ€ข Private entityListeners: EntityListener[] = []

Callbacks which are fired whenever an entity is added or removed.

Defined in

src/world.ts:27


fluids

โ€ข Private fluids: Fluid[] = []

Defined in

src/world.ts:22


useBatchRenderer

โ€ข useBatchRenderer: boolean = false

Defined in

src/world.ts:18

Methods

addEntities

โ–ธ addEntities(...entities): void

Adds the given entities to the world.

By default entity listeners are fired for each entity added however, if the last argument passed to this function is a boolean then it will be used to decide wether or not to fire entity listeners.

Parameters

NameTypeDescription
...entities(boolean | Entity)[]The entities to add

Returns

void

Defined in

src/world.ts:257


addEntity

โ–ธ addEntity(entity, fireListeners?): void

Adds an entity to the world.

Parameters

NameTypeDefault valueDescription
entityEntityundefinedThe entity to add
fireListenersbooleantrueWether or not to execute the world's entity listeners

Returns

void

Defined in

src/world.ts:244


addEntityListener

โ–ธ addEntityListener(listener): void

Adds a callback which will be called whenever an entity is added or removed from the world.

Parameters

NameTypeDescription
listenerEntityListenerThe listener to add

Returns

void

Defined in

src/world.ts:333


addFluid

โ–ธ addFluid(fluid): void

Adds a fluid to the world.

Parameters

NameTypeDescription
fluidFluidThe fluid to add

Returns

void

Defined in

src/world.ts:354


callEntityListeners

โ–ธ Private callEntityListeners(event, entity, index): void

Parameters

NameType
event"add" | "remove"
entityEntity
indexnumber

Returns

void

Defined in

src/world.ts:322


fixedUpdate

โ–ธ fixedUpdate(ts): void

Performs the world's fixed update operations.

Parameters

NameTypeDescription
tsTimeStepThe TimeStep for this fixed update

Returns

void

Defined in

src/world.ts:96


getCamera

โ–ธ getCamera(): Camera

Gets the camera that is currently being used for rendering.

Returns

Camera

The camera that is currently being used for rendering

Defined in

src/world.ts:384


getEntities

โ–ธ getEntities(): Entity[]

Gets all entities currently in the world.

Returns

Entity[]

All entites in the world

Defined in

src/world.ts:225


getEntitiesByName

โ–ธ getEntitiesByName(name): Entity[]

Gets all entities with the given name.

Parameters

NameTypeDescription
namestringThe name of the entity

Returns

Entity[]

Defined in

src/world.ts:234


getPixelFromWorld

โ–ธ getPixelFromWorld(world): vec2

Gets the pixel coordinate of a world location.

This is calculated using the viewport of the world camera.

Parameters

NameTypeDescription
worldvec2A world position

Returns

vec2

Defined in

src/world.ts:205


getPixelToWorldSpace

โ–ธ getPixelToWorldSpace(): vec2

Calculates the pixel space to world space scale.

Returns

vec2

The number that multiplying a pixel space coordinate by provides the equivalent world space coordinate.

Defined in

src/world.ts:165


getWorldFromPixel

โ–ธ getWorldFromPixel(pixel): vec2

Gets the world location of a pixel on the screen.

This is calculated using the viewport of the world camera.

Parameters

NameTypeDescription
pixelvec2A pixel position on the screen

Returns

vec2

Defined in

src/world.ts:180


getWorldToPixelSpace

โ–ธ getWorldToPixelSpace(): vec2

Calculates the world space to pixel space scale.

Returns

vec2

The number that multiplying a world space coordinate by provides the equivalent pixel space coordinate.

Defined in

src/world.ts:155


getWorldtoClipSpace

โ–ธ getWorldtoClipSpace(): vec2

Calculates the world space to clip space scale.

Returns

vec2

The number that multiplying a world space coordinate by provides the equivalent clip space coordinate.

Defined in

src/world.ts:143


removeAllEntities

โ–ธ removeAllEntities(fireListeners?): void

Removes all entities from the world.

Parameters

NameTypeDefault valueDescription
fireListenersbooleantrueWether or not to fire entity listeners (default is true)

Returns

void

Defined in

src/world.ts:314


removeEntities

โ–ธ removeEntities(...entities): void

Removes the given entities from the world.

By default entity listeners are fired for each entity added however, if the last argument passed to this function is a boolean then it will be used to decide wether or not to fire entity listeners.

Parameters

NameTypeDescription
...entities(boolean | Entity)[]The entities to remove

Returns

void

Defined in

src/world.ts:295


removeEntity

โ–ธ removeEntity(entity, fireListeners?): boolean

Removes an entity from the world.

Parameters

NameTypeDefault valueDescription
entityEntityundefinedThe entity to remove
fireListenersbooleantrueWether or not to execute the world's entity listeners

Returns

boolean

Wether or not the entity was removed

Defined in

src/world.ts:278


removeEntityListener

โ–ธ removeEntityListener(listener): void

Removes an EntityListener from the world.

Parameters

NameTypeDescription
listenerEntityListenerThe listener to remove

Returns

void

Defined in

src/world.ts:342


removeFluid

โ–ธ removeFluid(fluid): void

Removes a fluid from the world.

Parameters

NameTypeDescription
fluidFluidThe fluid to remove

Returns

void

Defined in

src/world.ts:363


renderEntities

โ–ธ renderEntities(): void

Renders the entities in the world using the world's current camera.

Returns

void

Defined in

src/world.ts:105


renderFluids

โ–ธ renderFluids(): void

Renders the fluids in the world using the world's current camera.

Returns

void

Defined in

src/world.ts:126


update

โ–ธ update(ts): void

Updates the world's camera, terrain and entities.

Also calls the render function.

Parameters

NameTypeDescription
tsTimeStepThe TimeStep for this update

Returns

void

Implementation of

System.update

Defined in

src/world.ts:75


useCamera

โ–ธ useCamera(camera): void

Sets the camera to use for rendering.

Parameters

NameTypeDescription
cameraCameraThe camera to use for rendering

Returns

void

Defined in

src/world.ts:375