a lil improvement

This commit is contained in:
Evert Prants 2019-08-03 13:56:31 +03:00
parent d2f693ccf5
commit 15eec53d50
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -138,6 +138,11 @@ class Chunk {
class ChunkWorld { class ChunkWorld {
constructor () { constructor () {
this.chunks = [] this.chunks = []
this.dirty = true
}
mkdirty () {
this.dirty = true
} }
static generateTerrain (c) { static generateTerrain (c) {
@ -181,13 +186,15 @@ class ChunkWorld {
} }
update (pos, scale) { update (pos, scale) {
if (!this.dirty) return
let positions = ChunkWorld.getPositionsInViewport(pos, scale) let positions = ChunkWorld.getPositionsInViewport(pos, scale)
let generated = false
for (let p in positions) { for (let p in positions) {
let cpos = positions[p] let cpos = positions[p]
let found = false let found = false
for (let i in this.chunks) { for (let i in this.chunks) {
let chunk = this.chunks[i] 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 found = true
break break
} }
@ -195,25 +202,33 @@ class ChunkWorld {
if (!found) { if (!found) {
let chunk = new Chunk(cpos.x, cpos.y) let chunk = new Chunk(cpos.x, cpos.y)
generated = true
ChunkWorld.generateTerrain(chunk) ChunkWorld.generateTerrain(chunk)
this.chunks.push(chunk) this.chunks.push(chunk)
break break
} }
} }
// We generated nothing this time around, mark it as not dirty
if (!generated) {
this.dirty = false
}
} }
render (r, o, s) { 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) { for (let i in this.chunks) {
let chunk = this.chunks[i] 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 && if (chunk.x <= localX + localW && chunk.x + 1 >= localX &&
chunk.y <= localY + ah && chunk.y + 1 >= localY) { chunk.y <= localY + localH && chunk.y + 1 >= localY) {
Chunk.render(r, chunk, o, s) Chunk.render(r, chunk, o, s)
} }
} }
@ -222,7 +237,7 @@ class ChunkWorld {
let world = new ChunkWorld() let world = new ChunkWorld()
let input = new Input(canvas) 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 zoom = 2
let tiles = [ let tiles = [
@ -232,7 +247,7 @@ let tiles = [
new Tile('stone', '#515151') new Tile('stone', '#515151')
] ]
let cursor = {x: 0, y: 0} let cursor = { x: 0, y: 0 }
function render () { function render () {
ctx.fillStyle = '#00aaff' ctx.fillStyle = '#00aaff'
@ -243,14 +258,18 @@ function render () {
function update (dt) { function update (dt) {
if (input.isDown('w')) { if (input.isDown('w')) {
cursor.y += 6 cursor.y += 6
world.mkdirty()
} else if (input.isDown('s')) { } else if (input.isDown('s')) {
cursor.y -= 6 cursor.y -= 6
world.mkdirty()
} }
if (input.isDown('a')) { if (input.isDown('a')) {
cursor.x += 6 cursor.x += 6
world.mkdirty()
} else if (input.isDown('d')) { } else if (input.isDown('d')) {
cursor.x -= 6 cursor.x -= 6
world.mkdirty()
} }
world.update(cursor, zoom) world.update(cursor, zoom)