Camera momentum implemented.
This commit is contained in:
parent
acd8b92731
commit
46a64668af
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 9.7 KiB |
85
js/Camera.js
85
js/Camera.js
|
@ -13,6 +13,7 @@ export default class Camera
|
|||
this.borderRight = undefined;
|
||||
this.speedX = 0;
|
||||
this.speedY = 0;
|
||||
this.speedMax = 12;
|
||||
}
|
||||
|
||||
centerCamera(x, y)
|
||||
|
@ -33,28 +34,84 @@ export default class Camera
|
|||
}
|
||||
}
|
||||
|
||||
facePosition(x, y, delta, reactionSpeed = 1)
|
||||
centerCameraX(x)
|
||||
{
|
||||
let positionX = x - this.width * 0.5;
|
||||
let positionY = y - this.height * 0.5;
|
||||
this.position.x = x - this.width * 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 (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 (distanceX < 0.01) {
|
||||
this.position.x = positionX;
|
||||
lockCameraIntoBorders()
|
||||
{
|
||||
this.lockCameraIntoHorizontalBorders();
|
||||
this.lockCameraIntoVerticalBorders();
|
||||
}
|
||||
|
||||
if (this.position.y !== positionY) {
|
||||
this.position.y += distanceY * delta * reactionSpeed;
|
||||
lockCameraIntoHorizontalBorders()
|
||||
{
|
||||
let hasBeenLocked = false;
|
||||
|
||||
if (this.position.x < this.borderLeft) {
|
||||
this.position.x = this.borderLeft;
|
||||
hasBeenLocked = true;
|
||||
} else if (this.borderRight !== undefined && this.position.x + this.width > this.borderRight) {
|
||||
this.position.x = this.borderRight - this.width;
|
||||
hasBeenLocked = true;
|
||||
}
|
||||
|
||||
if (distanceY < 0.01) {
|
||||
this.position.y = positionY;
|
||||
return hasBeenLocked;
|
||||
}
|
||||
|
||||
lockCameraIntoVerticalBorders()
|
||||
{
|
||||
let hasBeenLocked = false;
|
||||
|
||||
if (this.position.y < this.borderTop) {
|
||||
this.position.y = this.borderTop;
|
||||
hasBeenLocked = true;
|
||||
} else if (this.borderBottom !== undefined && this.position.y + this.height > this.borderBottom) {
|
||||
this.position.y = this.borderBottom - this.height;
|
||||
hasBeenLocked = true;
|
||||
}
|
||||
|
||||
return hasBeenLocked;
|
||||
}
|
||||
|
||||
focusPosition(x, y, reactionLatency = 1)
|
||||
{
|
||||
this.focusPositionX(x, reactionLatency);
|
||||
this.focusPositionY(y, reactionLatency);
|
||||
}
|
||||
|
||||
focusPositionX(x, reactionLatency = 1)
|
||||
{
|
||||
let centerX = this.position.x + this.width * 0.5;
|
||||
let distanceX = x - centerX;
|
||||
|
||||
if (!this.lockCameraIntoHorizontalBorders() && distanceX !== 0) {
|
||||
if (Math.abs(distanceX) < 1) {
|
||||
this.position.x = x - this.width * 0.5;
|
||||
} else {
|
||||
this.position.x += distanceX * (1 / reactionLatency);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
focusPositionY(y, reactionLatency = 1)
|
||||
{
|
||||
let centerY = this.position.y + this.height * 0.5;
|
||||
let distanceY = y - centerY;
|
||||
|
||||
if (distanceY !== 0) {
|
||||
if (Math.abs(distanceY) < 1) {
|
||||
this.position.y = y - this.height * 0.5;
|
||||
} else {
|
||||
this.position.y += distanceY * (1 / reactionLatency);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -128,11 +128,18 @@ function MainLoop(timestamp)
|
|||
)
|
||||
}
|
||||
|
||||
camera.focusPosition(
|
||||
mrCroc.position.x - mrCroc.getWidth() * 0.5,
|
||||
mrCroc.position.y - mrCroc.getHeight() * 0.5,
|
||||
20
|
||||
);
|
||||
camera.lockCameraIntoBorders();
|
||||
|
||||
/* Drawing */
|
||||
if (timestamp - lastRendered >= FRAME_DURATION) {
|
||||
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
||||
|
||||
camera.centerCamera(mrCroc.position.x - mrCroc.getWidth() * 0.5, mrCroc.position.y - mrCroc.getHeight() * 0.5);
|
||||
// camera.centerCamera(mrCroc.position.x - mrCroc.getWidth() * 0.5, mrCroc.position.y - mrCroc.getHeight() * 0.5);
|
||||
architecture.draw(context, camera);
|
||||
mrCroc.draw(context, camera);
|
||||
gisela.draw(context, camera);
|
||||
|
|
Loading…
Reference in New Issue