a lil improvement
This commit is contained in:
parent
d2f693ccf5
commit
15eec53d50
33
src/index.js
33
src/index.js
@ -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) {
|
||||||
for (let i in this.chunks) {
|
|
||||||
let chunk = this.chunks[i]
|
|
||||||
let scaledSize = TotalSize / s
|
let scaledSize = TotalSize / s
|
||||||
|
|
||||||
// Convert to chunk-space coordinates
|
// Convert to chunk-space coordinates
|
||||||
let localX = Math.floor(cursor.x / scaledSize) * -1
|
let localX = Math.floor(cursor.x / scaledSize) * -1
|
||||||
let localY = Math.floor(cursor.y / scaledSize) * -1
|
let localY = Math.floor(cursor.y / scaledSize) * -1
|
||||||
let aw = Math.ceil(canvas.width / scaledSize) + 1
|
let localW = Math.ceil(canvas.width / scaledSize) + 1
|
||||||
let ah = Math.ceil(canvas.height / scaledSize)
|
let localH = Math.ceil(canvas.height / scaledSize)
|
||||||
|
|
||||||
if (chunk.x <= localX + aw && chunk.x + 1 >= localX &&
|
for (let i in this.chunks) {
|
||||||
chunk.y <= localY + ah && chunk.y + 1 >= localY) {
|
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)
|
Chunk.render(r, chunk, o, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user