2025-11-23 14:49:37 +08:00
|
|
|
/**
|
|
|
|
|
* Camera System
|
|
|
|
|
* 相机系统
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-08 21:23:37 +08:00
|
|
|
import { EntitySystem, Matcher, Entity, ECSSystem } from '@esengine/esengine';
|
2025-12-01 22:28:51 +08:00
|
|
|
import { CameraComponent } from '@esengine/camera';
|
2025-11-23 14:49:37 +08:00
|
|
|
import type { EngineBridge } from '../core/EngineBridge';
|
|
|
|
|
|
|
|
|
|
@ECSSystem('Camera', { updateOrder: -100 })
|
|
|
|
|
export class CameraSystem extends EntitySystem {
|
|
|
|
|
private bridge: EngineBridge;
|
|
|
|
|
private lastAppliedCameraId: number | null = null;
|
|
|
|
|
|
|
|
|
|
constructor(bridge: EngineBridge) {
|
|
|
|
|
// Match entities with CameraComponent
|
|
|
|
|
super(Matcher.empty().all(CameraComponent));
|
|
|
|
|
this.bridge = bridge;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override onBegin(): void {
|
|
|
|
|
// Will process cameras in process()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override process(entities: readonly Entity[]): void {
|
|
|
|
|
// Use first enabled camera
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
|
if (!entity.enabled) continue;
|
|
|
|
|
|
|
|
|
|
const camera = entity.getComponent(CameraComponent);
|
|
|
|
|
if (!camera) continue;
|
|
|
|
|
|
|
|
|
|
// Only apply if camera changed
|
|
|
|
|
if (this.lastAppliedCameraId !== entity.id) {
|
|
|
|
|
this.applyCamera(camera);
|
|
|
|
|
this.lastAppliedCameraId = entity.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only use first active camera
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyCamera(camera: CameraComponent): void {
|
|
|
|
|
// Apply background color
|
|
|
|
|
const bgColor = camera.backgroundColor || '#000000';
|
|
|
|
|
const r = parseInt(bgColor.slice(1, 3), 16) / 255;
|
|
|
|
|
const g = parseInt(bgColor.slice(3, 5), 16) / 255;
|
|
|
|
|
const b = parseInt(bgColor.slice(5, 7), 16) / 255;
|
|
|
|
|
this.bridge.setClearColor(r, g, b, 1.0);
|
|
|
|
|
}
|
|
|
|
|
}
|