refactor: World::tick heat off

This commit is contained in:
cha0s 2019-04-16 14:04:11 -05:00
parent bbe6e48ab1
commit 8d55357aad
2 changed files with 17 additions and 4 deletions

View File

@ -4,7 +4,7 @@
- ❌ Synchronized should be a mixin
- ❌ Behavior items should derive type/name
- ✔ Precompile behavior traversals
- Abstract physics world tick (hot position check, flat entity list)
- Abstract physics world tick (hot position check, flat entity list)
- ❌ Cache current routine for Behaved tick
- ✔ Apply patches to matter.js
( https://github.com/liabru/matter-js/pulls?utf8=%E2%9C%93&q=is%3Apr+author%3Abchevalier )

View File

@ -13,21 +13,34 @@ export class AbstractWorld extends decorate(class {}) {
constructor() {
super();
this.entities = new Map();
this.entitiesList = [];
}
associateBodyWithEntity(body, entity) {
this.entities.set(body, entity);
this.entitiesList.push(entity);
body.position = entity.position;
}
removeBody(body) {
this.entities.delete(body);
if (this.entities.has(body)) {
const entity = this.entities.get(body);
this.entities.delete(body);
const index = this.entitiesList.indexOf(entity);
if (-1 !== index) {
this.entitiesList.splice(index, 1);
}
}
}
tick(elapsed) {
// Propagate position updates.
for (const entity of this.entities.values()) {
if (Vector.equalsClose(entity.position, entity.body.position)) {
for (let i = 0; i < this.entitiesList.length; ++i) {
const entity = this.entitiesList[i];
const [entityX, entityY] = entity.position;
const [bodyX, bodyY] = entity.body.position;
// Hot.
if (entityX === bodyX && entityY === bodyY) {
continue;
}
entity.position = entity.body.position;