mr-crocs-adventures/js/Camera.js

60 lines
1.8 KiB
JavaScript

import GeometryPoint from "./geometry/GeometryPoint.js";
export default class Camera
{
constructor(x = 0, y = 0, width = window.innerWidth, height = window.innerHeight)
{
this.position = new GeometryPoint(x, y);
this.width = width;
this.height = height;
this.borderTop = 0;
this.borderBottom = undefined;
this.borderLeft = 0;
this.borderRight = undefined;
this.speedX = 0;
this.speedY = 0;
}
centerCamera(x, y)
{
this.position.x = x - this.width * 0.5;
this.position.y = y - this.height * 0.5;
if (this.position.x < this.borderLeft) {
this.position.x = this.borderLeft;
} else if (this.borderRight !== undefined && this.position.x + this.width > this.borderRight) {
this.position.x = this.borderRight - this.width;
}
if (this.position.y < this.borderTop) {
this.position.y = this.borderTop;
} else if (this.borderBottom !== undefined && this.position.y + this.height > this.borderBottom) {
this.position.y = this.borderBottom - this.height;
}
}
facePosition(x, y, delta, reactionSpeed = 1)
{
let positionX = x - this.width * 0.5;
let positionY = y - this.height * 0.5;
let distanceX = this.position.x - positionX;
let distanceY = this.position.y - positionY;
if (this.position.x !== positionX) {
this.position.x += distanceX * delta * reactionSpeed;
}
if (distanceX < 0.01) {
this.position.x = positionX;
}
if (this.position.y !== positionY) {
this.position.y += distanceY * delta * reactionSpeed;
}
if (distanceY < 0.01) {
this.position.y = positionY;
}
}
}