silphius/app/ecs-systems/follow-camera.js
2024-06-11 02:56:29 -05:00

33 lines
776 B
JavaScript

import {RESOLUTION} from '@/constants.js'
import {System} from '@/ecs/index.js';
export default class FollowCamera extends System {
reindex(entities) {
super.reindex(entities);
for (const id of entities) {
this.updateCamera(this.ecs.get(id));
}
}
tick() {
const {diff} = this.ecs;
for (const id in diff) {
if (diff[id].Position) {
this.updateCamera(this.ecs.get(id));
}
}
}
updateCamera(entity) {
const {Camera, Position} = entity;
if (Camera && Position) {
const {AreaSize: {x, y}} = this.ecs.get(1);
const [hx, hy] = [RESOLUTION.x / 2, RESOLUTION.y / 2];
Camera.x = Math.max(hx, Math.min(Position.x, x - hx));
Camera.y = Math.max(hy, Math.min(Position.y, y - hy));
}
}
}