feat: collider prediction

This commit is contained in:
cha0s 2024-11-16 06:34:37 -06:00
parent e540d98c46
commit 7bafc4702a
3 changed files with 31 additions and 10 deletions

View File

@ -32,7 +32,7 @@ export default class Collider extends Component {
}
return aabbs;
}
checkCollision(other) {
checkCollision(other, {runScripts}) {
if (!this.isColliding || !other.isColliding) {
return;
}
@ -45,7 +45,7 @@ export default class Collider extends Component {
if (0 === activeIntersections.size) {
return;
}
this.endIntersections(other, intersections);
this.endIntersections(other, intersections, {runScripts});
return;
}
for (const intersection of intersections) {
@ -75,7 +75,7 @@ export default class Collider extends Component {
}
}
if (!hasMatchingIntersection) {
if (this.collisionStartScript) {
if (runScripts && this.collisionStartScript) {
const script = this.collisionStartScript.clone();
script.locals.entity = thisEntity;
script.locals.other = otherEntity;
@ -85,7 +85,7 @@ export default class Collider extends Component {
ecs.addDestructionDependency(otherEntity.id, promise);
ecs.addDestructionDependency(thisEntity.id, promise);
}
if (other.collisionStartScript) {
if (runScripts && other.collisionStartScript) {
const script = other.collisionStartScript.clone();
script.locals.entity = otherEntity;
script.locals.other = thisEntity;
@ -125,7 +125,7 @@ export default class Collider extends Component {
}
this.$$intersections.clear();
}
endIntersections(other, intersections) {
endIntersections(other, intersections, {runScripts}) {
const otherEntity = ecs.get(other.entity);
const thisEntity = ecs.get(this.entity);
for (const intersection of intersections) {
@ -133,7 +133,7 @@ export default class Collider extends Component {
intersection.entity.bodies[intersection.i],
intersection.other.bodies[intersection.j],
];
if (this.collisionEndScript) {
if (runScripts && this.collisionEndScript) {
const script = this.collisionEndScript.clone();
script.locals.other = otherEntity;
script.locals.pair = [body, otherBody];
@ -142,7 +142,7 @@ export default class Collider extends Component {
ecs.addDestructionDependency(thisEntity.id, promise);
ecs.addDestructionDependency(otherEntity.id, promise);
}
if (other.collisionEndScript) {
if (runScripts && other.collisionEndScript) {
const script = other.collisionEndScript.clone();
script.locals.other = thisEntity;
script.locals.pair = [otherBody, body];

View File

@ -2,6 +2,24 @@ import {System} from '@/silphius/ecs/index.js';
export default class Colliders extends System {
predict(entity) {
if (!entity.Collider) {
return;
}
const within = this.ecs.system('MaintainColliderHash').collisions(entity);
for (const other of within) {
if (entity === other || !other.Collider) {
continue;
}
entity.Collider.checkCollision(other.Collider, {runScripts: false});
}
for (const [other, intersections] of entity.Collider.$$intersections) {
if (!within.has(this.ecs.get(other.entity))) {
entity.Collider.endIntersections(other, intersections, {runScripts: false});
}
}
}
static get priority() {
return {
after: 'MaintainColliderHash',
@ -29,11 +47,11 @@ export default class Colliders extends System {
continue;
}
checked.get(other).add(entity);
entity.Collider.checkCollision(other.Collider);
entity.Collider.checkCollision(other.Collider, {runScripts: true});
}
for (const [other, intersections] of entity.Collider.$$intersections) {
if (!within.has(this.ecs.get(other.entity))) {
entity.Collider.endIntersections(other, intersections);
entity.Collider.endIntersections(other, intersections, {runScripts: true});
}
}
}

View File

@ -19,6 +19,7 @@ const Action = {
const actions = new Map();
let ecs = new PredictionEcs({Components, Systems});
ecs.system('MaintainColliderHash').active = true;
let mainEntityId = 0;
function applyClientActions(elapsed) {
@ -64,7 +65,9 @@ function applyClientActions(elapsed) {
continue;
}
}
ecs.predict(main, action.stop - action.start);
const actionElapsed = action.stop - action.start;
ecs.tick(actionElapsed)
ecs.predict(main, actionElapsed);
}
for (const id of finished) {
actions.delete(id);