refactor: collider instead of visible aabb
This commit is contained in:
parent
cca1445043
commit
fd40759d41
|
@ -2,7 +2,26 @@ import Component from '@/ecs/component.js';
|
||||||
|
|
||||||
export default class Interacts extends Component {
|
export default class Interacts extends Component {
|
||||||
instanceFromSchema() {
|
instanceFromSchema() {
|
||||||
|
const {ecs} = this;
|
||||||
return class ControlledInstance extends super.instanceFromSchema() {
|
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() {
|
toJSON() {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {System} from '@/ecs/index.js';
|
import {System} from '@/ecs/index.js';
|
||||||
|
import {distance} from '@/util/math.js';
|
||||||
|
|
||||||
export default class Interactions extends System {
|
export default class Interactions extends System {
|
||||||
|
|
||||||
|
@ -9,32 +10,21 @@ export default class Interactions extends System {
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
for (const {Direction, Interacts, Position} of this.select('default')) {
|
for (const entity of this.select('default')) {
|
||||||
|
const {Interacts} = entity;
|
||||||
Interacts.willInteractWith = 0
|
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
|
// todo sort
|
||||||
const entities = Array.from(this.ecs.system('VisibleAabbs').within({
|
const entities = Array.from(this.ecs.system('Colliders').within(Interacts.aabb()))
|
||||||
x0,
|
.filter((other) => other !== entity)
|
||||||
x1: x0 + 15,
|
.sort(({Position: l}, {Position: r}) => {
|
||||||
y0,
|
return distance(entity.Position, l) > distance(entity.Position, r) ? -1 : 1;
|
||||||
y1: y0 + 15,
|
});
|
||||||
}));
|
for (const other of entities) {
|
||||||
for (let j = 0; j < entities.length; ++j) {
|
if (other === entity) {
|
||||||
if (entities[j].Interactive && entities[j].Interactive.interacting) {
|
continue;
|
||||||
Interacts.willInteractWith = entities[0].id;
|
}
|
||||||
|
if (other.Interactive && other.Interactive.interacting) {
|
||||||
|
Interacts.willInteractWith = other.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ const filtered = []
|
||||||
for (let i = 0; i < projected.length; ++i) {
|
for (let i = 0; i < projected.length; ++i) {
|
||||||
const x0 = projected[i].x * layer.tileSize.x;
|
const x0 = projected[i].x * layer.tileSize.x;
|
||||||
const y0 = projected[i].y * layer.tileSize.y;
|
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,
|
x0,
|
||||||
x1: x0 + layer.tileSize.x - 1,
|
x1: x0 + layer.tileSize.x - 1,
|
||||||
y0,
|
y0,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user