a lil improvement
This commit is contained in:
parent
d2f693ccf5
commit
15eec53d50
37
src/index.js
37
src/index.js
@ -138,6 +138,11 @@ class Chunk {
|
||||
class ChunkWorld {
|
||||
constructor () {
|
||||
this.chunks = []
|
||||
this.dirty = true
|
||||
}
|
||||
|
||||
mkdirty () {
|
||||
this.dirty = true
|
||||
}
|
||||
|
||||
static generateTerrain (c) {
|
||||
@ -181,13 +186,15 @@ class ChunkWorld {
|
||||
}
|
||||
|
||||
update (pos, scale) {
|
||||
if (!this.dirty) return
|
||||
let positions = ChunkWorld.getPositionsInViewport(pos, scale)
|
||||
let generated = false
|
||||
for (let p in positions) {
|
||||
let cpos = positions[p]
|
||||
let found = false
|
||||
for (let i in this.chunks) {
|
||||
let chunk = this.chunks[i]
|
||||
if (chunk.x == cpos.x && chunk.y == cpos.y) {
|
||||
if (chunk.x === cpos.x && chunk.y === cpos.y) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -195,25 +202,33 @@ class ChunkWorld {
|
||||
|
||||
if (!found) {
|
||||
let chunk = new Chunk(cpos.x, cpos.y)
|
||||
generated = true
|
||||
ChunkWorld.generateTerrain(chunk)
|
||||
this.chunks.push(chunk)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// We generated nothing this time around, mark it as not dirty
|
||||
if (!generated) {
|
||||
this.dirty = false
|
||||
}
|
||||
}
|
||||
|
||||
render (r, o, s) {
|
||||
for (let i in this.chunks) {
|
||||
let chunk = this.chunks[i]
|
||||
let scaledSize = TotalSize / s
|
||||
|
||||
// Convert to chunk-space coordinates
|
||||
let localX = Math.floor(cursor.x / scaledSize) * -1
|
||||
let localY = Math.floor(cursor.y / scaledSize) * -1
|
||||
let aw = Math.ceil(canvas.width / scaledSize) + 1
|
||||
let ah = Math.ceil(canvas.height / scaledSize)
|
||||
let localW = Math.ceil(canvas.width / scaledSize) + 1
|
||||
let localH = Math.ceil(canvas.height / scaledSize)
|
||||
|
||||
if (chunk.x <= localX + aw && chunk.x + 1 >= localX &&
|
||||
chunk.y <= localY + ah && chunk.y + 1 >= localY) {
|
||||
for (let i in this.chunks) {
|
||||
let chunk = this.chunks[i]
|
||||
|
||||
if (chunk.x <= localX + localW && chunk.x + 1 >= localX &&
|
||||
chunk.y <= localY + localH && chunk.y + 1 >= localY) {
|
||||
Chunk.render(r, chunk, o, s)
|
||||
}
|
||||
}
|
||||
@ -222,7 +237,7 @@ class ChunkWorld {
|
||||
|
||||
let world = new ChunkWorld()
|
||||
let input = new Input(canvas)
|
||||
let noise = new BetterNoise(1, /*amplitude = */15, /*persistence = */0.4, /*octaves = */5, /*period = */120, /*lacunarity = */2)
|
||||
let noise = new BetterNoise(1, /* amplitude = */15, /* persistence = */0.4, /* octaves = */5, /* period = */120, /* lacunarity = */2)
|
||||
let zoom = 2
|
||||
|
||||
let tiles = [
|
||||
@ -232,7 +247,7 @@ let tiles = [
|
||||
new Tile('stone', '#515151')
|
||||
]
|
||||
|
||||
let cursor = {x: 0, y: 0}
|
||||
let cursor = { x: 0, y: 0 }
|
||||
|
||||
function render () {
|
||||
ctx.fillStyle = '#00aaff'
|
||||
@ -243,14 +258,18 @@ function render () {
|
||||
function update (dt) {
|
||||
if (input.isDown('w')) {
|
||||
cursor.y += 6
|
||||
world.mkdirty()
|
||||
} else if (input.isDown('s')) {
|
||||
cursor.y -= 6
|
||||
world.mkdirty()
|
||||
}
|
||||
|
||||
if (input.isDown('a')) {
|
||||
cursor.x += 6
|
||||
world.mkdirty()
|
||||
} else if (input.isDown('d')) {
|
||||
cursor.x -= 6
|
||||
world.mkdirty()
|
||||
}
|
||||
|
||||
world.update(cursor, zoom)
|
||||
|
Loading…
Reference in New Issue
Block a user