import { Container } from 'pixi.js';
/**
* Камера виртуального разрешения: позиция — центр взгляда в мировых (виртуальных) пикселях.
* Округляет смещение до целого пикселя, чтобы пиксель-арт не «дрожал».
*/
export class Camera {
x = 0;
y = 0;
/** Границы прокрутки (опционально). */
bounds: { width: number; height: number } | null = null;
constructor(
private readonly viewWidth: number,
private readonly viewHeight: number
) {}
follow(targetX: number, targetY: number): void {
this.x = targetX;
this.y = targetY;
this.clamp();
}
apply(container: Container): void {
const halfW = Math.floor(this.viewWidth / 2);
const halfH = Math.floor(this.viewHeight / 2);
container.position.set(halfW - Math.round(this.x), halfH - Math.round(this.y));
}
private clamp(): void {
if (!this.bounds) return;
const halfW = this.viewWidth / 2;
const halfH = this.viewHeight / 2;
if (this.bounds.width > this.viewWidth) {
this.x = Math.min(Math.max(this.x, halfW), this.bounds.width - halfW);
} else {
this.x = this.bounds.width / 2;
}
if (this.bounds.height > this.viewHeight) {
this.y = Math.min(Math.max(this.y, halfH), this.bounds.height - halfH);
} else {
this.y = this.bounds.height / 2;
}
}
}