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() { for (const entity of this.ecs.changed(['Position'])) { this.updateCamera(entity); } } 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)); } } }