slight assets change, add background layer

This commit is contained in:
Evert Prants 2020-01-10 21:10:38 +02:00
parent d04cfc425f
commit 71850fc7f9
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 33 additions and 11 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -21,13 +21,14 @@ class Debugging {
ctx.fillText('loaded ' + world.chunks.length, 4, 16 * 4) ctx.fillText('loaded ' + world.chunks.length, 4, 16 * 4)
ctx.fillText('drawn ' + world._lastDrawCount, 4, 16 * 5) ctx.fillText('drawn ' + world._lastDrawCount, 4, 16 * 5)
ctx.fillText('updates ' + world._lastUpdateCount, 4, 16 * 6) ctx.fillText('updates ' + world._lastUpdateCount, 4, 16 * 6)
ctx.fillText('active ' + world._active.length, 4, 16 * 7)
// Mouse // Mouse
let mpos = Input.mouse.pos let mpos = Input.mouse.pos
let mpin = world.pickMouse(vp, mpos) 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) { if (mpin.chunk) {
ctx.fillText('mouse-in-chunk (x: ' + mpin.chunk.x + '; y: ' + mpin.chunk.y + ')', 4, 16 * 9) 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 * 10) ctx.fillText('mouse-in-tile (x: ' + mpin.tile.x + '; y: ' + mpin.tile.y + ')', 4, 16 * 11)
} }
} }

View File

@ -91,6 +91,19 @@ class TileLayer {
ctx.drawImage(map.image, tileCoords.x, tileCoords.y, map.tile, map.tile, ctx.drawImage(map.image, tileCoords.x, tileCoords.y, map.tile, map.tile,
coords.x * this.tile, coords.y * this.tile, this.tile, this.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) { update (dt) {
@ -101,6 +114,7 @@ class TileLayer {
class TilePhysicsLayer extends TileLayer { class TilePhysicsLayer extends TileLayer {
constructor (size = 16, tileSize = 16) { constructor (size = 16, tileSize = 16) {
super('col', size, tileSize) super('col', size, tileSize)
this.empty = false
} }
draw () {} draw () {}
@ -109,6 +123,7 @@ class TilePhysicsLayer extends TileLayer {
generateFromTiles (tiles) { generateFromTiles (tiles) {
this.tiles = [] this.tiles = []
this.empty = true
for (let i in tiles.tiles) { for (let i in tiles.tiles) {
let t = tiles.tiles[i] let t = tiles.tiles[i]
let p = tiles.toXY(parseInt(i)) let p = tiles.toXY(parseInt(i))
@ -128,11 +143,13 @@ class TilePhysicsLayer extends TileLayer {
continue continue
} }
this.empty = false
this.tiles[i] = 1 this.tiles[i] = 1
} }
} }
collide (chunk, obj) { collide (chunk, obj) {
if (this.empty) return false
let absPos = chunk.absPos let absPos = chunk.absPos
for (let i in this.tiles) { for (let i in this.tiles) {
let t = this.tiles[i] let t = this.tiles[i]
@ -177,14 +194,17 @@ class Chunk {
let y = Math.ceil(heightMap.getHeight(tileAbs.x) * 5 / 2) - 4 let y = Math.ceil(heightMap.getHeight(tileAbs.x) * 5 / 2) - 4
if (tileAbs.y < y) { if (tileAbs.y < y) {
fgLayer.tiles.push(-1) fgLayer.tiles.push(-1)
bgLayer.tiles.push(-1)
continue continue
} }
if (tileAbs.y === y) { if (tileAbs.y === y) {
fgLayer.tiles.push(tileMap.indexOf('GRASS_TOP')) fgLayer.tiles.push(tileMap.indexOf('GRASS_TOP'))
bgLayer.tiles.push(tileMap.indexOf('DIRT'))
continue continue
} }
if (tileAbs.y < y + 10) { if (tileAbs.y < y + 10) {
fgLayer.tiles.push(tileMap.indexOf('DIRT')) fgLayer.tiles.push(tileMap.indexOf('DIRT'))
bgLayer.tiles.push(tileMap.indexOf('DIRT'))
continue continue
} }
if (tileAbs.y > heightMap.falloff - y + 64) { if (tileAbs.y > heightMap.falloff - y + 64) {
@ -192,6 +212,7 @@ class Chunk {
continue continue
} }
fgLayer.tiles.push(tileMap.indexOf('STONE')) fgLayer.tiles.push(tileMap.indexOf('STONE'))
bgLayer.tiles.push(tileMap.indexOf('STONE'))
} }
clLayer.generateFromTiles(fgLayer) clLayer.generateFromTiles(fgLayer)
@ -301,7 +322,7 @@ class World {
this._unloadTick = 0 this._unloadTick = 0
this._lastDrawCount = 0 this._lastDrawCount = 0
this._lastUpdateCount = 0 this._lastUpdateCount = 0
this._collide = [] this._active = []
} }
getChunk (x, y) { getChunk (x, y) {
@ -313,10 +334,10 @@ class World {
} }
update (dt, vp) { update (dt, vp) {
this._collide = [] this._active = []
let posPoint = vp.chunkIn(this.chunkSize * this.tileSize) let posPoint = vp.chunkIn(this.chunkSize * this.tileSize)
for (let x = posPoint.x - 4; x < posPoint.x + 5; x++) { for (let x = posPoint.x - 3; x < posPoint.x + 4; x++) {
for (let y = posPoint.y - 4; y < posPoint.y + 5; y++) { for (let y = posPoint.y - 2; y < posPoint.y + 3; y++) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) continue if (x < 0 || y < 0 || x >= this.width || y >= this.height) continue
let exists = this.getChunk(x, y) let exists = this.getChunk(x, y)
if (!exists) { if (!exists) {
@ -325,7 +346,7 @@ class World {
this.chunks.push(n) this.chunks.push(n)
continue continue
} }
this._collide.push(exists) this._active.push(exists)
} }
} }
@ -378,9 +399,9 @@ class World {
} }
collide (obj) { collide (obj) {
if (!this._collide.length) return null if (!this._active.length) return null
for (let i in this._collide) { for (let i in this._active) {
let c = this._collide[i] let c = this._active[i]
let collide = c.collide(obj) let collide = c.collide(obj)
if (collide) return collide if (collide) return collide
} }