2020-01-08 20:07:58 +00:00
|
|
|
import { ctx } from './canvas'
|
2020-01-09 14:37:18 +00:00
|
|
|
import Input from './input'
|
2020-01-08 20:07:58 +00:00
|
|
|
|
|
|
|
class Debugging {
|
2020-01-08 22:01:07 +00:00
|
|
|
constructor () {
|
|
|
|
this.overlay = document.createElement('div')
|
|
|
|
this.overlay.style = 'color:#fff;position:absolute;top:15px;right:15px;background-color:hsla(0,0%,47%,0.5);padding:10px;display:flex;flex-direction:column;'
|
|
|
|
document.body.appendChild(this.overlay)
|
|
|
|
this.drawGrid = false
|
2020-01-09 14:37:18 +00:00
|
|
|
this.infotext = true
|
|
|
|
this.addCheckbox(this, 'infotext')
|
2020-01-08 22:01:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:07:58 +00:00
|
|
|
draw (vp, world, fps) {
|
2020-01-09 14:37:18 +00:00
|
|
|
if (!this.infotext) return
|
2020-01-08 20:07:58 +00:00
|
|
|
let p = vp.chunkIn(world.chunkSize * world.tileSize)
|
|
|
|
ctx.fillStyle = '#fff'
|
|
|
|
ctx.fillText(fps + ' fps', 4, 16)
|
2020-01-08 22:01:07 +00:00
|
|
|
ctx.fillText('cam (x: ' + vp.x + '; y: ' + vp.y + ')', 4, 16 * 2)
|
|
|
|
ctx.fillText('cam-in-chunk (x: ' + p.x + '; y: ' + p.y + ')', 4, 16 * 3)
|
|
|
|
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)
|
2020-01-10 19:10:38 +00:00
|
|
|
ctx.fillText('active ' + world._active.length, 4, 16 * 7)
|
2020-01-09 14:37:18 +00:00
|
|
|
// Mouse
|
|
|
|
let mpos = Input.mouse.pos
|
|
|
|
let mpin = world.pickMouse(vp, mpos)
|
2020-01-10 19:10:38 +00:00
|
|
|
ctx.fillText('mouse (x: ' + mpos.x + '; y: ' + mpos.y + ')', 4, 16 * 9)
|
2020-01-09 14:37:18 +00:00
|
|
|
if (mpin.chunk) {
|
2020-01-10 19:10:38 +00:00
|
|
|
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)
|
2020-01-09 14:37:18 +00:00
|
|
|
}
|
2020-01-08 20:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createSliders (obj, args, fn) {
|
|
|
|
for (let a in args) {
|
|
|
|
let min = args[a][0]
|
|
|
|
let max = args[a][1]
|
|
|
|
let step = args[a][2]
|
|
|
|
let div = document.createElement('div')
|
|
|
|
div.style = 'display:flex;flex-direction:row;'
|
|
|
|
let name = document.createElement('span')
|
|
|
|
let value = document.createElement('value')
|
|
|
|
let slider = document.createElement('input')
|
|
|
|
slider.style = 'flex-grow:1;'
|
|
|
|
slider.type = 'range'
|
|
|
|
slider.min = min
|
|
|
|
slider.max = max
|
|
|
|
slider.step = step
|
|
|
|
slider.value = obj[a]
|
|
|
|
name.innerHTML = a
|
|
|
|
value.innerHTML = obj[a]
|
|
|
|
slider.addEventListener('input', function (e) {
|
|
|
|
obj[a] = parseFloat(slider.value)
|
|
|
|
value.innerHTML = slider.value
|
2020-01-09 14:37:18 +00:00
|
|
|
fn && fn(a, slider.value)
|
2020-01-08 20:07:58 +00:00
|
|
|
})
|
|
|
|
div.appendChild(name)
|
|
|
|
div.appendChild(slider)
|
|
|
|
div.appendChild(value)
|
2020-01-08 22:01:07 +00:00
|
|
|
this.overlay.appendChild(div)
|
2020-01-08 20:07:58 +00:00
|
|
|
}
|
2020-01-08 22:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addCheckbox (obj, arg, fn) {
|
|
|
|
let div = document.createElement('div')
|
|
|
|
div.style = 'display:flex;flex-direction:row;'
|
|
|
|
let name = document.createElement('span')
|
|
|
|
let checkbox = document.createElement('input')
|
|
|
|
name.style = 'flex-grow:1;'
|
|
|
|
name.innerHTML = arg
|
|
|
|
checkbox.type = 'checkbox'
|
|
|
|
checkbox.checked = obj[arg] === true
|
|
|
|
checkbox.addEventListener('change', function (e) {
|
|
|
|
obj[arg] = checkbox.checked
|
2020-01-09 14:37:18 +00:00
|
|
|
fn && fn(arg, obj[arg])
|
2020-01-08 22:01:07 +00:00
|
|
|
})
|
|
|
|
div.appendChild(checkbox)
|
|
|
|
div.appendChild(name)
|
|
|
|
this.overlay.appendChild(div)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkGrid (ctx, chunk, view) {
|
|
|
|
if (!this.drawGrid) return
|
|
|
|
// Inner grid
|
|
|
|
ctx.lineWidth = 0.15
|
|
|
|
ctx.strokeStyle = '#041fff'
|
|
|
|
for (let x = 0; x <= chunk.fullSize; x += chunk.tile) {
|
|
|
|
ctx.beginPath()
|
|
|
|
// Vertical lines
|
|
|
|
ctx.moveTo(0 + x, 0)
|
|
|
|
ctx.lineTo(0 + x, chunk.fullSize)
|
|
|
|
|
|
|
|
// Horizontal lines
|
|
|
|
ctx.moveTo(0, 0 + x)
|
|
|
|
ctx.lineTo(chunk.fullSize, 0 + x)
|
|
|
|
|
|
|
|
// Close
|
|
|
|
ctx.closePath()
|
|
|
|
ctx.stroke()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chunk border
|
|
|
|
ctx.lineWidth = 1
|
|
|
|
ctx.strokeStyle = '#040404'
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(0.5, 0)
|
|
|
|
ctx.lineTo(0.5, chunk.fullSize)
|
|
|
|
ctx.moveTo(0.5, chunk.fullSize)
|
|
|
|
ctx.lineTo(chunk.fullSize, chunk.fullSize)
|
|
|
|
ctx.closePath()
|
|
|
|
ctx.stroke()
|
|
|
|
|
2020-01-10 18:46:14 +00:00
|
|
|
// Draw colliders
|
|
|
|
ctx.fillStyle = '#00aaff'
|
|
|
|
let collider = chunk.getLayer('col')
|
|
|
|
for (let i in collider.tiles) {
|
|
|
|
let p = collider.toXY(parseInt(i))
|
|
|
|
if (collider.tiles[i] === 0) continue
|
|
|
|
ctx.fillRect(p.x * chunk.tile, p.y * chunk.tile, chunk.tile, chunk.tile)
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:01:07 +00:00
|
|
|
// Chunk index
|
|
|
|
ctx.fillStyle = '#fff'
|
|
|
|
ctx.fillText(chunk.x + ';' + chunk.y, 5, 16)
|
2020-01-08 20:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Debugging()
|