refactor: collider instead of visible aabb

This commit is contained in:
cha0s 2024-07-02 16:16:55 -05:00
parent cca1445043
commit fd40759d41
3 changed files with 34 additions and 25 deletions

View File

@ -2,7 +2,26 @@ import Component from '@/ecs/component.js';
export default class Interacts extends Component {
instanceFromSchema() {
const {ecs} = this;
return class ControlledInstance extends super.instanceFromSchema() {
aabb() {
const {Direction, Position} = ecs.get(this.entity);
let x0 = Position.x - 8;
let y0 = Position.y - 8;
if (0 === Direction.direction) {
y0 -= 16
}
if (1 === Direction.direction) {
x0 += 16
}
if (2 === Direction.direction) {
y0 += 16
}
if (3 === Direction.direction) {
x0 -= 16
}
return {x0, x1: x0 + 15, y0, y1: y0 + 15};
}
toJSON() {
return {};
}

View File

@ -1,4 +1,5 @@
import {System} from '@/ecs/index.js';
import {distance} from '@/util/math.js';
export default class Interactions extends System {
@ -9,32 +10,21 @@ export default class Interactions extends System {
}
tick() {
for (const {Direction, Interacts, Position} of this.select('default')) {
for (const entity of this.select('default')) {
const {Interacts} = entity;
Interacts.willInteractWith = 0
let x0 = Position.x - 8;
let y0 = Position.y - 8;
if (0 === Direction.direction) {
y0 -= 16
}
if (1 === Direction.direction) {
x0 += 16
}
if (2 === Direction.direction) {
y0 += 16
}
if (3 === Direction.direction) {
x0 -= 16
}
// todo sort
const entities = Array.from(this.ecs.system('VisibleAabbs').within({
x0,
x1: x0 + 15,
y0,
y1: y0 + 15,
}));
for (let j = 0; j < entities.length; ++j) {
if (entities[j].Interactive && entities[j].Interactive.interacting) {
Interacts.willInteractWith = entities[0].id;
const entities = Array.from(this.ecs.system('Colliders').within(Interacts.aabb()))
.filter((other) => other !== entity)
.sort(({Position: l}, {Position: r}) => {
return distance(entity.Position, l) > distance(entity.Position, r) ? -1 : 1;
});
for (const other of entities) {
if (other === entity) {
continue;
}
if (other.Interactive && other.Interactive.interacting) {
Interacts.willInteractWith = other.id;
}
}
}

View File

@ -3,7 +3,7 @@ const filtered = []
for (let i = 0; i < projected.length; ++i) {
const x0 = projected[i].x * layer.tileSize.x;
const y0 = projected[i].y * layer.tileSize.y;
const entities = Array.from(ecs.system('VisibleAabbs').within({
const entities = Array.from(ecs.system('Colliders').within({
x0,
x1: x0 + layer.tileSize.x - 1,
y0,