diff --git a/assets/ground.png b/assets/ground.png index 1c93153..353fe35 100644 Binary files a/assets/ground.png and b/assets/ground.png differ diff --git a/src/debug.js b/src/debug.js index 5e0124f..4833ca6 100644 --- a/src/debug.js +++ b/src/debug.js @@ -21,13 +21,14 @@ class Debugging { ctx.fillText('loaded ' + world.chunks.length, 4, 16 * 4) ctx.fillText('drawn ' + world._lastDrawCount, 4, 16 * 5) ctx.fillText('updates ' + world._lastUpdateCount, 4, 16 * 6) + ctx.fillText('active ' + world._active.length, 4, 16 * 7) // Mouse let mpos = Input.mouse.pos let mpin = world.pickMouse(vp, mpos) - ctx.fillText('mouse (x: ' + mpos.x + '; y: ' + mpos.y + ')', 4, 16 * 8) + ctx.fillText('mouse (x: ' + mpos.x + '; y: ' + mpos.y + ')', 4, 16 * 9) if (mpin.chunk) { - ctx.fillText('mouse-in-chunk (x: ' + mpin.chunk.x + '; y: ' + mpin.chunk.y + ')', 4, 16 * 9) - ctx.fillText('mouse-in-tile (x: ' + mpin.tile.x + '; y: ' + mpin.tile.y + ')', 4, 16 * 10) + ctx.fillText('mouse-in-chunk (x: ' + mpin.chunk.x + '; y: ' + mpin.chunk.y + ')', 4, 16 * 10) + ctx.fillText('mouse-in-tile (x: ' + mpin.tile.x + '; y: ' + mpin.tile.y + ')', 4, 16 * 11) } } diff --git a/src/tiles.js b/src/tiles.js index 074a35e..ffc2d59 100644 --- a/src/tiles.js +++ b/src/tiles.js @@ -91,6 +91,19 @@ class TileLayer { ctx.drawImage(map.image, tileCoords.x, tileCoords.y, map.tile, map.tile, coords.x * this.tile, coords.y * this.tile, this.tile, this.tile) } + + // Add some darkness to the BG layer + if (this.name === 'bg') { + ctx.globalAlpha = 0.3 + ctx.fillStyle = '#000' + for (let i in this.tiles) { + let tilei = this.tiles[i] + if (tilei === -1) continue + let coords = this.toXY(parseInt(i)) + ctx.fillRect(coords.x * this.tile, coords.y * this.tile, this.tile, this.tile) + } + ctx.globalAlpha = 1 + } } update (dt) { @@ -101,6 +114,7 @@ class TileLayer { class TilePhysicsLayer extends TileLayer { constructor (size = 16, tileSize = 16) { super('col', size, tileSize) + this.empty = false } draw () {} @@ -109,6 +123,7 @@ class TilePhysicsLayer extends TileLayer { generateFromTiles (tiles) { this.tiles = [] + this.empty = true for (let i in tiles.tiles) { let t = tiles.tiles[i] let p = tiles.toXY(parseInt(i)) @@ -128,11 +143,13 @@ class TilePhysicsLayer extends TileLayer { continue } + this.empty = false this.tiles[i] = 1 } } collide (chunk, obj) { + if (this.empty) return false let absPos = chunk.absPos for (let i in this.tiles) { let t = this.tiles[i] @@ -177,14 +194,17 @@ class Chunk { let y = Math.ceil(heightMap.getHeight(tileAbs.x) * 5 / 2) - 4 if (tileAbs.y < y) { fgLayer.tiles.push(-1) + bgLayer.tiles.push(-1) continue } if (tileAbs.y === y) { fgLayer.tiles.push(tileMap.indexOf('GRASS_TOP')) + bgLayer.tiles.push(tileMap.indexOf('DIRT')) continue } if (tileAbs.y < y + 10) { fgLayer.tiles.push(tileMap.indexOf('DIRT')) + bgLayer.tiles.push(tileMap.indexOf('DIRT')) continue } if (tileAbs.y > heightMap.falloff - y + 64) { @@ -192,6 +212,7 @@ class Chunk { continue } fgLayer.tiles.push(tileMap.indexOf('STONE')) + bgLayer.tiles.push(tileMap.indexOf('STONE')) } clLayer.generateFromTiles(fgLayer) @@ -301,7 +322,7 @@ class World { this._unloadTick = 0 this._lastDrawCount = 0 this._lastUpdateCount = 0 - this._collide = [] + this._active = [] } getChunk (x, y) { @@ -313,10 +334,10 @@ class World { } update (dt, vp) { - this._collide = [] + this._active = [] let posPoint = vp.chunkIn(this.chunkSize * this.tileSize) - for (let x = posPoint.x - 4; x < posPoint.x + 5; x++) { - for (let y = posPoint.y - 4; y < posPoint.y + 5; y++) { + for (let x = posPoint.x - 3; x < posPoint.x + 4; x++) { + for (let y = posPoint.y - 2; y < posPoint.y + 3; y++) { if (x < 0 || y < 0 || x >= this.width || y >= this.height) continue let exists = this.getChunk(x, y) if (!exists) { @@ -325,7 +346,7 @@ class World { this.chunks.push(n) continue } - this._collide.push(exists) + this._active.push(exists) } } @@ -378,9 +399,9 @@ class World { } collide (obj) { - if (!this._collide.length) return null - for (let i in this._collide) { - let c = this._collide[i] + if (!this._active.length) return null + for (let i in this._active) { + let c = this._active[i] let collide = c.collide(obj) if (collide) return collide }