-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
984e141
commit d9e6974
Showing
9 changed files
with
457 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
"packageManager": "[email protected]", | ||
"private": true, | ||
"sketch": { | ||
"hidden": true, | ||
"title": "Procedural Generation - BSP Dungeon", | ||
"tags": [ | ||
"procedural-generation" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Vector2 } from './vector.js' | ||
|
||
export const CHUNK_SIZE = Math.pow(2, 6) | ||
|
||
const getChunkCoords = (x, y) => { | ||
const chunkX = Math.floor(x / CHUNK_SIZE) | ||
const chunkY = Math.floor(y / CHUNK_SIZE) | ||
|
||
return [chunkX, chunkY] | ||
} | ||
|
||
const getChunkKey = (x, y) => { | ||
return `${x},${y}` | ||
} | ||
|
||
const getChunkIndex = (x, y) => { | ||
const localX = ((x % CHUNK_SIZE) + CHUNK_SIZE) % CHUNK_SIZE | ||
const localY = ((y % CHUNK_SIZE) + CHUNK_SIZE) % CHUNK_SIZE | ||
|
||
return localX + localY * CHUNK_SIZE | ||
} | ||
|
||
export const getChunkPositionFromIndex = (index, out) => { | ||
const x = index % CHUNK_SIZE | ||
const y = Math.floor(index / CHUNK_SIZE) | ||
|
||
out.set(x, y) | ||
} | ||
|
||
export class Grid { | ||
/** | ||
* @type {Record<string, { chunkPosition: Vector2, data: Array<number> }>} | ||
*/ | ||
chunks = {} | ||
|
||
/** | ||
* @param {number} x | ||
* @param {number} y | ||
* @param {number} value | ||
*/ | ||
set(x, y, value) { | ||
const [chunkX, chunkY] = getChunkCoords(x, y) | ||
const chunkKey = getChunkKey(chunkX, chunkY) | ||
|
||
let chunk = this.chunks[chunkKey] | ||
if (!chunk) { | ||
this.chunks[chunkKey] = chunk = { chunkPosition: [chunkX, chunkY], data: new Array(CHUNK_SIZE * CHUNK_SIZE) } | ||
} | ||
|
||
const chunkIndex = getChunkIndex(x, y) | ||
|
||
chunk.data[chunkIndex] = value | ||
} | ||
|
||
/** | ||
* @param {number} x | ||
* @param {number} y | ||
* @returns {number} | ||
*/ | ||
get(x, y) { | ||
const [chunkX, chunkY] = getChunkCoords(x, y) | ||
const chunk = this.chunks[getChunkKey(chunkX, chunkY)] | ||
|
||
if (!chunk) { | ||
return undefined | ||
} | ||
|
||
return chunk.data[getChunkIndex(x, y)] | ||
} | ||
|
||
/** | ||
* @returns {[Vector2, Vector2]} | ||
*/ | ||
getChunkBounds() { | ||
const min = new Vector2() | ||
const max = new Vector2() | ||
|
||
for (const chunk of Object.values(this.chunks)) { | ||
min[0] = Math.min(min[0], chunk.chunkPosition[0]) | ||
min[1] = Math.min(min[1], chunk.chunkPosition[1]) | ||
|
||
max[0] = Math.max(max[0], chunk.chunkPosition[0]) | ||
max[1] = Math.max(max[1], chunk.chunkPosition[1]) | ||
} | ||
|
||
min.multiplyScalar(CHUNK_SIZE) | ||
max.multiplyScalar(CHUNK_SIZE) | ||
|
||
return [min, max] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.