the-world-engine
Version:
three.js based, unity like game engine for browser
86 lines (84 loc) • 2.06 kB
JavaScript
import { OrderedSet } from "js-sdsl";
import { EventContainer } from "../collection/EventContainer";
export class CameraContainer {
ir=null;
nr;
ar;
sr;
hr;
constructor(t) {
this.nr = new Map;
this.ar = new OrderedSet(undefined, ((t, r) => {
if (t.info.priority === r.info.priority) {
return t.camera.instanceId - r.camera.instanceId;
}
return r.info.priority - t.info.priority;
}));
this.sr = t;
this.hr = new EventContainer;
}
get camera() {
return this.ir?.camera ?? null;
}
get currentCameraPriority() {
return this.ir?.info.priority ?? Number.MIN_SAFE_INTEGER;
}
addCamera(t, r) {
this.nr.set(t, r);
this.ar.insert({
camera: t,
info: r
});
this.ur();
}
removeCamera(t) {
const r = this.nr.get(t);
if (!r) return;
this.ar.eraseElementByKey({
camera: t,
info: r
});
this.nr.delete(t);
this.ur();
}
changeCameraPriority(t, r) {
const e = this.nr.get(t);
if (!e) return;
this.ar.eraseElementByKey({
camera: t,
info: e
});
e.priority = r;
this.ar.insert({
camera: t,
info: e
});
this.ur();
}
changeCameraBackgroundColor(t, r) {
const e = this.nr.get(t);
if (!e) return;
e.backgroundColor = r;
if (this.ir?.camera === t) {
this.sr(r);
}
}
ur() {
if (this.ar.size() === 0) {
this.ir = null;
return;
}
const t = this.ar.front();
if (!t) {
this.ir = null;
return;
}
if (this.ir?.camera === t.camera) return;
this.ir = t;
this.sr(this.ir.info.backgroundColor);
this.hr.invoke(this.ir.camera);
}
get onCameraChanged() {
return this.hr;
}
}