Class: World
world.World
Represents the 2D world.
Implements
Constructors
constructor
โข new World(cellSize
, cameraViewport
)
Creates a World instance.
Parameters
Name | Type | Description |
---|---|---|
cellSize | vec2 | The width and height of each world cell in pixels |
cameraViewport | vec2 | The 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
Name | Type | Description |
---|---|---|
cellSize | vec2 | The 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
Name | Type | Description |
---|---|---|
...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
Name | Type | Default value | Description |
---|---|---|---|
entity | Entity | undefined | The entity to add |
fireListeners | boolean | true | Wether 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
Name | Type | Description |
---|---|---|
listener | EntityListener | The listener to add |
Returns
void
Defined in
src/world.ts:333
addFluid
โธ addFluid(fluid
): void
Adds a fluid to the world.
Parameters
Name | Type | Description |
---|---|---|
fluid | Fluid | The fluid to add |
Returns
void
Defined in
src/world.ts:354
callEntityListeners
โธ Private
callEntityListeners(event
, entity
, index
): void
Parameters
Name | Type |
---|---|
event | "add" | "remove" |
entity | Entity |
index | number |
Returns
void
Defined in
src/world.ts:322
fixedUpdate
โธ fixedUpdate(ts
): void
Performs the world's fixed update operations.
Parameters
Name | Type | Description |
---|---|---|
ts | TimeStep | The 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
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
Name | Type | Description |
---|---|---|
name | string | The 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
Name | Type | Description |
---|---|---|
world | vec2 | A 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
Name | Type | Description |
---|---|---|
pixel | vec2 | A 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
Name | Type | Default value | Description |
---|---|---|---|
fireListeners | boolean | true | Wether 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
Name | Type | Description |
---|---|---|
...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
Name | Type | Default value | Description |
---|---|---|---|
entity | Entity | undefined | The entity to remove |
fireListeners | boolean | true | Wether 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
Name | Type | Description |
---|---|---|
listener | EntityListener | The listener to remove |
Returns
void
Defined in
src/world.ts:342
removeFluid
โธ removeFluid(fluid
): void
Removes a fluid from the world.
Parameters
Name | Type | Description |
---|---|---|
fluid | Fluid | The 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
Name | Type | Description |
---|---|---|
ts | TimeStep | The TimeStep for this update |
Returns
void
Implementation of
Defined in
src/world.ts:75
useCamera
โธ useCamera(camera
): void
Sets the camera to use for rendering.
Parameters
Name | Type | Description |
---|---|---|
camera | Camera | The camera to use for rendering |
Returns
void
Defined in
src/world.ts:375