the-world-engine
Version:
three.js based, unity like game engine for browser
85 lines (82 loc) • 2.1 kB
JavaScript
import { MathUtils, Vector2, Vector3 } from "three/src/Three";
import { Component } from "../../hierarchy_object/Component";
import { Camera } from "../render/Camera";
export class TrackCameraController extends Component {
disallowMultipleComponent=true;
requiredComponents=[ Camera ];
Ur=null;
Lr=new Vector2;
Jr=20;
Kr=1;
Nr=false;
Qr=false;
Wr=6;
start() {
if (this.Ur) {
const t = this.Xr.copy(this.Ur.transform.position);
t.z += this.Jr;
this.transform.position.copy(t);
}
}
Xr=new Vector3;
update() {
const t = this.Xr.copy(this.Ur.transform.position);
t.z += this.Jr;
t.x += this.Lr.x;
t.y += this.Lr.y;
const e = this.transform;
if (this.Qr) {
const s = this.engine.time.deltaTime;
const r = this.Wr;
const i = MathUtils.damp(e.position.x, t.x, r, s);
const a = MathUtils.damp(e.position.y, t.y, r, s);
const h = MathUtils.damp(e.position.z, t.z, r, s);
e.position.set(i, a, h);
} else {
e.position.copy(t);
}
if (this.Nr) {
e.localPosition.x = Math.round(e.localPosition.x / this.Kr) * this.Kr;
e.localPosition.y = Math.round(e.localPosition.y / this.Kr) * this.Kr;
}
}
setTrackTarget(t) {
this.Ur = t;
}
get targetOffset() {
return this.Lr;
}
set targetOffset(t) {
this.Lr.copy(t);
}
get cameraDistanceOffset() {
return this.Jr;
}
set cameraDistanceOffset(t) {
this.Jr = t;
}
get pixelPerfectUnit() {
return this.Kr;
}
set pixelPerfectUnit(t) {
this.Kr = t;
}
get pixelPerfect() {
return this.Nr;
}
set pixelPerfect(t) {
this.Nr = t;
}
get smoothTrack() {
return this.Qr;
}
set smoothTrack(t) {
this.Qr = t;
}
get smoothLambda() {
return this.Wr;
}
set smoothLambda(t) {
this.Wr = t;
}
}