class Camera { constructor() { this.position = { x: -100, y: -100 }; this.viewBox = { x: 100, y: 100, width: 600, height: 400 }; } init(canvas) { this.viewBox.x = canvas.width / 3; this.viewBox.y = canvas.height / 3; this.viewBox.width = canvas.width - 2 * this.viewBox.x; this.viewBox.height = canvas.height - 2 * this.viewBox.y; this.position = { x: canvas.width / 2, y: canvas.height / 2 }; } update(canvas, player, delta) { this.followPlayer(player, delta); } handleResize(canvas) { this.viewBox.x = canvas.width / 3; this.viewBox.y = canvas.height / 3; this.viewBox.width = canvas.width - 2 * this.viewBox.x; this.viewBox.height = canvas.height - 2 * this.viewBox.y; } followPlayer(player) { var truePos = { x: 0, y: 0 }; truePos.x = this.position.x + player.position.x; truePos.y = this.position.y + player.position.y; if (truePos.x <= this.viewBox.x) { this.position.x -= truePos.x - this.viewBox.x; } if (player.position.x + this.position.x >= this.viewBox.x + this.viewBox.width) { this.position.x += (this.viewBox.x + this.viewBox.width) - truePos.x; } if (truePos.y <= this.viewBox.y) { this.position.y -= truePos.y - this.viewBox.y; } if (player.position.y + this.position.y >= this.viewBox.y + this.viewBox.height) { this.position.y += (this.viewBox.y + this.viewBox.height) - truePos.y; } } }