silphius/app/ecs/systems/visible-aabbs.js

87 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-07-02 14:41:54 -05:00
import {System} from '@/ecs/index.js';
import SpatialHash from '@/util/spatial-hash.js';
export default class VisibleAabbs extends System {
hash;
deindex(entities) {
super.deindex(entities);
for (const id of entities) {
this.hash.remove(id);
}
}
static get priority() {
return {
after: 'IntegratePhysics',
};
}
reindex(entities) {
for (const id of entities) {
if (1 === id) {
2024-07-30 09:56:53 -05:00
const {AreaSize} = this.ecs.get(1)
if (AreaSize) {
const {x, y} = AreaSize;
if (
!this.hash ||
(
this.hash.area.x !== x
|| this.hash.area.y !== y
)
) {
this.hash = new SpatialHash(this.ecs.get(1).AreaSize);
}
2024-07-23 17:04:17 -05:00
}
2024-07-02 14:41:54 -05:00
}
}
super.reindex(entities);
for (const id of entities) {
this.updateHash(this.ecs.get(id));
}
}
updateHash(entity) {
2024-07-30 09:56:53 -05:00
if (!this.hash) {
return;
}
2024-07-02 14:41:54 -05:00
if (!entity.VisibleAabb) {
2024-07-03 16:13:14 -05:00
this.hash.remove(entity.id);
2024-07-02 14:41:54 -05:00
return;
}
this.hash.update(entity.VisibleAabb, entity.id);
}
tick() {
for (const entity of this.ecs.changed(['Position'])) {
const {Position: {x, y}, Sprite, VisibleAabb} = entity;
if (VisibleAabb) {
let size = undefined;
if (Sprite) {
2024-07-30 09:56:53 -05:00
size = Sprite.size;
2024-07-02 14:41:54 -05:00
}
/* v8 ignore next 3 */
if (!size) {
throw new Error(`no size for aabb for entity ${entity.id}(${JSON.stringify(entity.toJSON(), null, 2)})`);
}
VisibleAabb.x0 = x - Sprite.anchor.x * size.w;
VisibleAabb.x1 = x + (1 - Sprite.anchor.x) * size.w;
VisibleAabb.y0 = y - Sprite.anchor.y * size.h;
VisibleAabb.y1 = y + (1 - Sprite.anchor.y) * size.h;
this.updateHash(entity);
}
}
}
within(query) {
const within = new Set();
2024-07-03 16:13:14 -05:00
for (const id of this.hash.within(query)) {
within.add(this.ecs.get(id));
2024-07-02 14:41:54 -05:00
}
return within;
}
}