Space

Root class for holding all chunks and actors.

Constructor

new Space(app, chunkSize, unitSize)

Create a new space.
Parameters:
NameTypeDefaultDescription
appAppParent application
chunkSizenumber16Number of units per chunk
unitSizenumber16Number of pixels per unit

Members

camera

Get the camera of the space.

camera

Set the camera of the space.

events

Get the space's event handler.

Methods

addActor(actor)

Add an actor to the space.
Parameters:
NameTypeDescription
actorActorThe actor to add

addGlobalID(actor)

Add an actor's global identifier to the space (do not call manually).
Parameters:
NameTypeDescription
actorActorActor to register identifier for

checkCollision(actor, pos) → {Object}

Check collision with all other actors.
Parameters:
NameTypeDescription
actorActorThe actor to check collision for
posVec2The position to attempt to move to
Returns:
Object containing collision result data
Type: 
Object

chunkAdded(chunk)

Handle a chunk just being added.
Parameters:
NameTypeDescription
chunkChunkChunk that was just added

draw(ctx)

Draw the space and its actors.
Parameters:
NameTypeDescription
ctxCanvasRenderingContext2DThe canvas context to draw on

drawBackground(ctx)

Draw the background color of the space.
Parameters:
NameTypeDescription
ctxCanvasRenderingContext2DThe canvas context to draw on

fillUnit(x, y, fill, color, walkOn, persistent, regenCache)

Fill a single unit at a specified position.
Parameters:
NameTypeDefaultDescription
xnumberPosition along x-axis
ynumberPosition along y-axis
fillbooleantrueWhether to fill unit or clear it
colorColor | string | SpriteColor, sprite, or sprite identifier for unit
walkOnbooleanfalseWhether to allow walking on unit
persistentbooleantrueWhether to make parent chunk persistent
regenCachebooleantrueWhether to regenerate cached units image

fillUnits(x1, y1, x2, y2, fill, color, walkOn)

Fill an area at a specified position.
Parameters:
NameTypeDefaultDescription
x1numberFirst position along x-axis
y1numberFirst position along y-axis
x2numberSecond position along x-axis
y2numberSecond position along y-axis
fillbooleantrueWhether to fill unit or clear it
colorColor | string | SpriteColor, sprite, or sprite identifier for unit
walkOnbooleanfalseWhether to allow walking on unit

fillUnitsLine(x1, y1, x2, y2, fill, color, walkOn)

Fill a line from one point to another.
Parameters:
NameTypeDefaultDescription
x1numberFirst position along x-axis
y1numberFirst position along y-axis
x2numberSecond position along x-axis
y2numberSecond position along y-axis
fillbooleantrueWhether to fill unit or clear it
colorColor | string | SpriteColor, sprite, or sprite identifier for unit
walkOnbooleanfalseWhether to allow walking on unit

fillUnitsWalls(x1, y1, x2, y2, fill, color, walkOn)

Fill walls around an area at a specified position.
Parameters:
NameTypeDefaultDescription
x1numberFirst position along x-axis
y1numberFirst position along y-axis
x2numberSecond position along x-axis
y2numberSecond position along y-axis
fillbooleantrueWhether to fill unit or clear it
colorColor | string | SpriteColor, sprite, or sprite identifier for unit
walkOnbooleanfalseWhether to allow walking on unit

find(id) → {Actor|null}

Get an actor with specified global identifier.
Parameters:
NameTypeDescription
idstringIdentifier to search for
Returns:
Actor with specified identifier, or null if it does not exist.
Type: 
Actor | null

generatePath(start, end) → {Path|null}

Generate a new path in the space.
Parameters:
NameTypeDescription
startVec2Starting position
endVec2Final position
Returns:
Generated path, or null if unsuccessful.
Type: 
Path | null

getMousePos() → {Vec2}

Get the mouse position relative to the camera and its zoom.
Returns:
Mouse position in space
Type: 
Vec2

getNearbyActors(ignore, pos, radius) → {Array.<Actor>}

Get all actors near a specified position and within a radius.
Parameters:
NameTypeDescription
ignoreActorActor to ignore
posVec2Point to find actors around
radiusnumberRadius to find actors within
Returns:
Array of actors near point within at radius
Type: 
Array.<Actor>

getUnit(x, y) → {ChunkUnit|null}

Get a unit at specified indices.
Parameters:
NameTypeDescription
xnumberIndex along x-axis
ynumberIndex along y-axis
Returns:
Chunk unit at specified indices, or null if there is none.
Type: 
ChunkUnit | null

getUnitAt(x, y) → {ChunkUnit|null}

Get a unit at a specified position.
Parameters:
NameTypeDescription
xnumberPosition along x-axis
ynumberPosition along y-axis
Returns:
Chunk unit at specified position, or null if there is none.
Type: 
ChunkUnit | null

setUnitPattern(x, y, pattern, color, unfilled, filled)

Parameters:
NameTypeDefaultDescription
xnumber0Position along x-axis
ynumber0Position along y-axis
patternstringPattern to use for generation
colorColor | string | SpriteColor, sprite, or sprite identifier for unit
unfilledstring0Character to use for unfilled units
filledstring1Character to use for filled units

update(elapsed)

Update the space and its actors.
Parameters:
NameTypeDescription
elapsednumberTime since last update