Constructor
new ChunkMgr(space, chunkSize, unitSize)
Create a new chunk manager.
Parameters:
Name | Type | Description |
---|---|---|
space | Space | Parent space |
chunkSize | number | Number of units per chunk |
unitSize | number | Number of pixels per unit |
Methods
addActor(actor)
Add an actor to the space.
Parameters:
Name | Type | Description |
---|---|---|
actor | Actor | Actor to add |
draw(ctx)
Draw all chunks.
Parameters:
Name | Type | Description |
---|---|---|
ctx | CanvasRenderingContext2D | Canvas context to draw on |
exists(x, y) → {boolean}
Check if a chunk exists.
Parameters:
Name | Type | Description |
---|---|---|
x | number | Index along x-axis |
y | number | Index along y-axis |
Returns:
Whether chunk exists
- Type:
- boolean
forAll(func)
Run a function for each chunk.
Parameters:
Name | Type | Description |
---|---|---|
func | function | Function to run |
genChunk(x, y) → {Chunk}
Generate a chunk at specified indices.
Parameters:
Name | Type | Description |
---|---|---|
x | number | Index along x-axis |
y | number | Index 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:
Name | Type | Description |
---|---|---|
x | number | Index along x-axis |
y | number | Index 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:
Name | Type | Description |
---|---|---|
pos | Vec2 | Position 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.
Returns:
Chunks within corners
- Type:
- Array.<Chunk>
getInBounds(bounds) → {Array.<Chunk>}
Get all chunks within specified bounds.
Parameters:
Name | Type | Description |
---|---|---|
bounds | Bounds | Bounding box |
Returns:
Chunks within bounds
- Type:
- Array.<Chunk>
getIndex(pos) → {Object}
Get the chunk indices at a specific position.
Parameters:
Name | Type | Description |
---|---|---|
pos | Vec2 | Position 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:
Name | Type | Description |
---|---|---|
pos | Vec2 | Center of area |
rx | number | Width of area |
ry | number | Height 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:
Name | Type | Description |
---|---|---|
ignore | Actor | Actor to ignore |
pos | Vec2 | Point to find actors around |
radius | number | Radius to find actors within |
Returns:
Array of actors near point within at radius
- Type:
- Array.<Actor>
update(elapsed)
Update all chunks.
Parameters:
Name | Type | Description |
---|---|---|
elapsed | number | Time 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:
Name | Type | Description |
---|---|---|
x | number | Index along x-axis |
y | number | Index 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:
Name | Type | Description |
---|---|---|
pos | Vec2 | Position to get chunk at |
Returns:
Chunk that was generated or already existed
- Type:
- Chunk