From 15eec53d50094dbcd904921a90d48696fa319989 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sat, 3 Aug 2019 13:56:31 +0300 Subject: [PATCH] a lil improvement --- src/index.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 8711cc6..faa36fe 100644 --- a/src/index.js +++ b/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) { + 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 localW = Math.ceil(canvas.width / scaledSize) + 1 + let localH = Math.ceil(canvas.height / scaledSize) + 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) - if (chunk.x <= localX + aw && chunk.x + 1 >= localX && - chunk.y <= localY + ah && chunk.y + 1 >= localY) { + 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)