30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
import Phaser from 'phaser';
|
|
import { CAMERA } from '../config.js';
|
|
|
|
export default class CameraRig {
|
|
constructor(scene, target, bounds) {
|
|
this.scene = scene;
|
|
this.target = target;
|
|
this.cam = scene.cameras.main;
|
|
|
|
if (bounds) {
|
|
this.cam.setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
}
|
|
|
|
this.cam.setDeadzone(CAMERA.deadzoneWidth, CAMERA.deadzoneHeight);
|
|
this.cam.startFollow(target, true, CAMERA.lerpX, CAMERA.lerpY);
|
|
}
|
|
|
|
// Leans the bus toward the left edge of the screen at rest and out to
|
|
// CAMERA.maxSpeedScreenFraction of the way across at speed, so there's
|
|
// more visible road ahead the faster the bus is moving. Driven through
|
|
// Phaser's followOffset (rather than setting scroll ourselves) so it
|
|
// still rides Phaser's own per-frame lerp smoothing and bounds clamping.
|
|
update() {
|
|
const speed = Math.abs(this.target.body.velocity.x);
|
|
const t = Phaser.Math.Clamp(speed / CAMERA.lookaheadMaxSpeed, 0, 1);
|
|
const screenFraction = Phaser.Math.Linear(CAMERA.restScreenFraction, CAMERA.maxSpeedScreenFraction, t);
|
|
this.cam.followOffset.x = (screenFraction - 0.5) * this.cam.width;
|
|
}
|
|
}
|