ChunkMgr

Manager of chunks for a space.

Constructor

new ChunkMgr(space, chunkSize, unitSize)

Create a new chunk manager.
Parameters:
NameTypeDescription
spaceSpaceParent space
chunkSizenumberNumber of units per chunk
unitSizenumberNumber of pixels per unit

Methods

addActor(actor)

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

draw(ctx)

Draw all chunks.
Parameters:
NameTypeDescription
ctxCanvasRenderingContext2DCanvas context to draw on

exists(x, y) → {boolean}

Check if a chunk exists.
Parameters:
NameTypeDescription
xnumberIndex along x-axis
ynumberIndex along y-axis
Returns:
Whether chunk exists
Type: 
boolean

forAll(func)

Run a function for each chunk.
Parameters:
NameTypeDescription
funcfunctionFunction to run

genChunk(x, y) → {Chunk}

Generate a chunk at specified indices.
Parameters:
NameTypeDescription
xnumberIndex along x-axis
ynumberIndex along y-axis
Returns:
Chunk that was generated or already existed
Type: 
Chunk

getChunk(x, y) → {Chunk|null}

Get the chunk at specified indices.
Parameters:
NameTypeDescription
xnumberIndex along x-axis
ynumberIndex along y-axis
Returns:
Chunk at indices, or null if it does not exist
Type: 
Chunk | null

getChunkAt(pos) → {Chunk|null}

Get a chunk at a specified position.
Parameters:
NameTypeDescription
posVec2Position to get chunk at
Returns:
Chunk at position, or null if it does not exist
Type: 
Chunk | null

getFromTo(from, to) → {Array.<Chunk>}

Get all chunks within two corner points.
Parameters:
NameTypeDescription
fromVec2Minimum corner
toVec2Maximum corner
Returns:
Chunks within corners
Type: 
Array.<Chunk>

getInBounds(bounds) → {Array.<Chunk>}

Get all chunks within specified bounds.
Parameters:
NameTypeDescription
boundsBoundsBounding box
Returns:
Chunks within bounds
Type: 
Array.<Chunk>

getIndex(pos) → {Object}

Get the chunk indices at a specific position.
Parameters:
NameTypeDescription
posVec2Position to check at
Returns:
Chunk indices
Type: 
Object

getNearby(pos, rx, ry) → {Array.<Chunk>}

Get all chunks within an area. Will not include null chunks.
Parameters:
NameTypeDescription
posVec2Center of area
rxnumberWidth of area
rynumberHeight of area
Returns:
Chunks within area
Type: 
Array.<Chunk>

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>

update(elapsed)

Update all chunks.
Parameters:
NameTypeDescription
elapsednumberTime since last update cycle in seconds

valChunk(x, y) → {Chunk}

Get a chunk at specified indices, and create it if it does not yet exist.
Parameters:
NameTypeDescription
xnumberIndex along x-axis
ynumberIndex along y-axis
Returns:
Chunk that was generated or already existed
Type: 
Chunk

valChunkAt(pos) → {Chunk}

Get a chunk at a specified position, and create it if it does not yet exist.
Parameters:
NameTypeDescription
posVec2Position to get chunk at
Returns:
Chunk that was generated or already existed
Type: 
Chunk