refactor: Collider.closest
This commit is contained in:
parent
73c6d991a7
commit
947e2cf380
|
@ -1,5 +1,5 @@
|
|||
import Component from '@/ecs/component.js';
|
||||
import {intersects} from '@/util/math.js';
|
||||
import {distance, intersects} from '@/util/math.js';
|
||||
|
||||
import vector2d from './helpers/vector-2d';
|
||||
|
||||
|
@ -30,6 +30,14 @@ export default class Collider extends Component {
|
|||
}
|
||||
return aabbs;
|
||||
}
|
||||
closest(aabb) {
|
||||
const entity = ecs.get(this.entity);
|
||||
return Array.from(ecs.system('Colliders').within(aabb))
|
||||
.filter((other) => other !== entity)
|
||||
.sort(({Position: l}, {Position: r}) => {
|
||||
return distance(entity.Position, l) > distance(entity.Position, r) ? -1 : 1;
|
||||
});
|
||||
}
|
||||
isCollidingWith(other) {
|
||||
const {aabb, aabbs} = this;
|
||||
const {aabb: otherAabb, aabbs: otherAabbs} = other;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import {System} from '@/ecs/index.js';
|
||||
import {distance} from '@/util/math.js';
|
||||
|
||||
export default class Interactions extends System {
|
||||
|
||||
|
@ -11,25 +10,16 @@ export default class Interactions extends System {
|
|||
|
||||
tick() {
|
||||
for (const entity of this.select('default')) {
|
||||
const {Interacts} = entity;
|
||||
let willInteract = false;
|
||||
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;
|
||||
});
|
||||
const {Collider, Interacts} = entity;
|
||||
let willInteractWith = 0;
|
||||
const entities = Collider.closest(Interacts.aabb());
|
||||
for (const other of entities) {
|
||||
if (other === entity) {
|
||||
continue;
|
||||
}
|
||||
if (other.Interactive && other.Interactive.interacting) {
|
||||
Interacts.willInteractWith = other.id;
|
||||
willInteract = true;
|
||||
if (other.Interactive?.interacting) {
|
||||
willInteractWith = other.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!willInteract) {
|
||||
Interacts.willInteractWith = 0;
|
||||
}
|
||||
Interacts.willInteractWith = willInteractWith;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user