diff --git a/packages/entity/index.js b/packages/entity/index.js index fb69a4d..f63089e 100644 --- a/packages/entity/index.js +++ b/packages/entity/index.js @@ -94,12 +94,15 @@ export class Entity extends decorate(Resource) { this.once('destroyed', () => { this.removeAllTraits(); }); - this.initializeSynchronizedChildren(); + // Bind to prevent lookup overhead. + this.tick = this.tick.bind(this); + // Fast props. + this.position = [0, 0]; + this.visibleAabb = [0, 0, 0, 0]; + // Fast path for instance. if ('undefined' !== typeof json) { this.fromJSON(json); } - // Bind to prevent lookup overhead. - this.tick = this.tick.bind(this); } acceptPacket(packet) { diff --git a/packages/entity/list/index.js b/packages/entity/list/index.js index 2807edb..a0f80d5 100644 --- a/packages/entity/list/index.js +++ b/packages/entity/list/index.js @@ -22,7 +22,6 @@ export class EntityList extends decorate(class {}) { this._entityTickers = [] this._flatEntities = []; this._quadTree = new QuadTree(); - this.initializeSynchronizedChildren(); } *[Symbol.iterator]() { @@ -76,7 +75,7 @@ export class EntityList extends decorate(class {}) { switch (step.op) { case 'add': // New entity. Create with patch as traits. - const newEntity = (new Entity()).fromJSON({ + const newEntity = new Entity({ traits: step.value, }); newEntity.instanceUuid = uuid; diff --git a/packages/entity/trait/index.js b/packages/entity/trait/index.js index a9b3194..ab439b7 100644 --- a/packages/entity/trait/index.js +++ b/packages/entity/trait/index.js @@ -19,7 +19,6 @@ export class Trait extends decorate(class {}) { this._memoizedListeners = undefined; this.params = Object.assign({}, ctor.defaultParams(), params); this.state = I.fromJS(ctor.defaultState()).merge(I.fromJS(state)); - this.initializeSynchronizedChildren(); if (this.tick) { this.tick = this.tick.bind(this); } diff --git a/packages/entity/traits/positioned.trait.js b/packages/entity/traits/positioned.trait.js index 08ac0b7..e433deb 100644 --- a/packages/entity/traits/positioned.trait.js +++ b/packages/entity/traits/positioned.trait.js @@ -33,7 +33,8 @@ export class Positioned extends decorate(Trait) { const x = this.state.get('x') >> 2; const y = this.state.get('y') >> 2; this._position = [x, y]; - this.entity.position = [x, y]; + this.entity.position[0] = x; + this.entity.position[1] = y; if (AVOCADO_CLIENT) { this._relaxServerPositionConstraintIfNearerThan = 0; this.serverPosition = this._position; @@ -50,7 +51,8 @@ export class Positioned extends decorate(Trait) { } on_positionChanged(oldPosition, newPosition) { - this.entity.position = newPosition; + this.entity.position[0] = newPosition[0]; + this.entity.position[1] = newPosition[1]; if (AVOCADO_SERVER) { const x = newPosition[0] << 2; const y = newPosition[1] << 2; diff --git a/packages/graphics/traits/visible.trait.js b/packages/graphics/traits/visible.trait.js index be0892e..e1e8bc8 100644 --- a/packages/graphics/traits/visible.trait.js +++ b/packages/graphics/traits/visible.trait.js @@ -57,7 +57,6 @@ export class Visible extends decorate(Trait) { this._container.isVisible = this.state.get('isVisible'); } this._rawVisibleAabb = [0, 0, 0, 0]; - this.entity.visibleAabb = [0, 0, 0, 0]; this.scheduledBoundingBoxUpdate = true; this.trackPosition = this.params.trackPosition; const scale = this.state.get('visibleScale'); @@ -184,10 +183,14 @@ export class Visible extends decorate(Trait) { } translateVisibleAabb() { - this.entity.visibleAabb = Rectangle.translated( + const visibleAabb = Rectangle.translated( this._rawVisibleAabb, this.entity.position ); + this.entity.visibleAabb[0] = visibleAabb[0]; + this.entity.visibleAabb[1] = visibleAabb[1]; + this.entity.visibleAabb[2] = visibleAabb[2]; + this.entity.visibleAabb[3] = visibleAabb[3]; this.entity.emit('visibleAabbChanged'); } diff --git a/packages/state/synchronized.js b/packages/state/synchronized.js index 5cc6cb2..61fac57 100644 --- a/packages/state/synchronized.js +++ b/packages/state/synchronized.js @@ -14,12 +14,17 @@ export function Synchronized(Superclass) { constructor(...args) { super(...args); this.state = I.Map(); + this._childrenNeedInitialization = true; this._childrenWithState = [] this._childrenWithoutState = []; this._childrenTickers = []; } - initializeSynchronizedChildren() { + ensureChildrenAreSynchronized() { + if (!this._childrenNeedInitialization) { + return; + } + this._childrenNeedInitialization = false; const synchronizedChildren = this.synchronizedChildren(); for (let i = 0; i < synchronizedChildren.length; ++i) { const key = synchronizedChildren[i]; @@ -84,6 +89,7 @@ export function Synchronized(Superclass) { } tickSynchronized(elapsed) { + this.ensureChildrenAreSynchronized(); for (let i = 0; i < this._childrenTickers.length; ++i) { this._childrenTickers[i](elapsed); } diff --git a/packages/topdown/layer.js b/packages/topdown/layer.js index 649077f..9e96801 100644 --- a/packages/topdown/layer.js +++ b/packages/topdown/layer.js @@ -38,7 +38,6 @@ export class Layer extends decorate(class {}) { this.on('tilesetUriChanged', this.onTilesetUriChanged, this); this.onTilesetUriChanged(); this.on('worldChanged', this.onWorldChanged, this); - this.initializeSynchronizedChildren(); } addEntity(entity) { diff --git a/packages/topdown/layers.js b/packages/topdown/layers.js index d8c5675..c95ddd6 100644 --- a/packages/topdown/layers.js +++ b/packages/topdown/layers.js @@ -15,7 +15,6 @@ export class Layers extends decorate(class {}) { constructor() { super(); this.layers = {}; - this.initializeSynchronizedChildren(); } *[Symbol.iterator]() { diff --git a/packages/topdown/room.js b/packages/topdown/room.js index c216405..dc8c53d 100644 --- a/packages/topdown/room.js +++ b/packages/topdown/room.js @@ -34,7 +34,6 @@ export class Room extends decorate(class {}) { this.layers.on('layerAdded', this.onLayerAdded, this); this.on('sizeChanged', this.onSizeChanged, this); this.on('worldChanged', this.onWorldChanged, this); - this.initializeSynchronizedChildren(); } addEntityToLayer(entity, layerIndex = 0) { diff --git a/packages/topdown/tiles.js b/packages/topdown/tiles.js index e37468b..c64f362 100644 --- a/packages/topdown/tiles.js +++ b/packages/topdown/tiles.js @@ -17,7 +17,6 @@ export class Tiles extends decorate(class {}) { constructor() { super(); this.data = I.List(); - this.initializeSynchronizedChildren(); } forEachTile(fn) {