tilegame/src/viewport.js

44 lines
907 B
JavaScript

import { canvas } from './canvas'
class Viewport {
constructor (x, y, scale = 1) {
this.x = x
this.y = y
this.scale = scale
}
get width () {
return canvas.width
}
get height () {
return canvas.height
}
get adjustCentered () {
return { x: Math.floor(this.x + this.width / 2), y: Math.floor(this.y + this.height / 2) }
}
chunkIn (size) {
let adj = this.adjustCentered
return { x: Math.floor(adj.x / size), y: Math.floor(adj.y / size) }
}
update (dt, world) {
let full = world.chunkSize * world.tileSize
if (this.x < 0) {
this.x = 0
} else if (this.x + this.width > world.width * full) {
this.x = (full * world.width) - this.width
}
if (this.y < 0) {
this.y = 0
} else if (this.y + this.height > world.height * full) {
this.y = (full * world.height) - this.height
}
}
}
export default Viewport