From 8d55357aadcac5f334919e7efa662b92e2a1ac2a Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 16 Apr 2019 14:04:11 -0500 Subject: [PATCH] refactor: World::tick heat off --- TODO.md | 2 +- packages/physics/abstract/world.js | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index ad7c92d..8f7ef6f 100644 --- a/TODO.md +++ b/TODO.md @@ -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 ) diff --git a/packages/physics/abstract/world.js b/packages/physics/abstract/world.js index c47c4e7..6bf4514 100644 --- a/packages/physics/abstract/world.js +++ b/packages/physics/abstract/world.js @@ -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;