silphius/app/ecs-systems/interactions.js
2024-07-01 18:12:53 -05:00

48 lines
1.0 KiB
JavaScript

import {System} from '@/ecs/index.js';
export default class PlantGrowth extends System {
static queries() {
return {
default: ['Interacts'],
};
}
tick() {
for (const {Direction, Interacts, Position} of this.select('default')) {
Interacts.willInteractWith = 0
const box = {
x: Position.x - 8,
y: Position.y - 8,
w: 16,
h: 16,
}
if (0 === Direction.direction) {
box.y -= 16
}
if (1 === Direction.direction) {
box.x += 16
}
if (2 === Direction.direction) {
box.y += 16
}
if (3 === Direction.direction) {
box.x -= 16
}
// todo sort
const entities = Array.from(this.ecs.system('UpdateSpatialHash').within(
box.x,
box.y,
box.w,
box.h,
));
for (let j = 0; j < entities.length; ++j) {
if (entities[j].Interactive && entities[j].Interactive.interacting) {
Interacts.willInteractWith = entities[0].id;
}
}
}
}
}