diff --git a/packages/entity/traits/listed.trait.js b/packages/entity/traits/listed.trait.js index 429dbfe..395eceb 100644 --- a/packages/entity/traits/listed.trait.js +++ b/packages/entity/traits/listed.trait.js @@ -10,15 +10,13 @@ export class Listed extends Trait { constructor(entity, params, state) { super(entity, params, state); - this.entity.list = undefined; + this.entity.list = null; this.quadTreeAabb = []; this.quadTreeNodes = []; } destroy() { - this.removeQuadTreeNodes(); - delete this.entity.list; - this.entity.emit('removedFromList'); + this.entity.removeFromList(); } addQuadTreeNodes() { @@ -96,9 +94,13 @@ export class Listed extends Trait { return { removeFromList: () => { + const list = this.entity.list; + if (!list) { + return; + } this.removeQuadTreeNodes(); - this.entity.list = undefined; - this.entity.emit('removedFromList'); + this.entity.list = null; + this.entity.emit('removedFromList', list); }, setIntoList: (list) => { diff --git a/packages/topdown/layer.js b/packages/topdown/layer.js index a690d0d..65fc45a 100644 --- a/packages/topdown/layer.js +++ b/packages/topdown/layer.js @@ -116,11 +116,12 @@ export class Layer extends decorate(class {}) { } onEntityAddedToLayer(entity) { - entity.layer = this; + entity.setIntoLayer(this); this.emit('entityAdded', entity) } onEntityRemovedFromLayer(entity) { + entity.removeFromLayer(); this.emit('entityRemoved', entity); } diff --git a/packages/topdown/traits/layered.trait.js b/packages/topdown/traits/layered.trait.js index 6209b27..55446a8 100644 --- a/packages/topdown/traits/layered.trait.js +++ b/packages/topdown/traits/layered.trait.js @@ -8,21 +8,31 @@ export class Layered extends Trait { constructor(entity, params, state) { super(entity, params, state); - this._layer = undefined; + this.entity.layer = null; } destroy() { - delete this._layer; - this.entity.emit('removedFromLayer'); + this.entity.removeFromLayer(); } - get layer() { - return this._layer; - } + methods() { + return { - set layer(layer) { - this._layer = layer; - this.entity.emit('addedToLayer'); + removeFromLayer: () => { + const layer = this.entity.layer; + if (!layer) { + return; + } + this.entity.layer = null; + this.entity.emit('removedFromLayer', layer); + }, + + setIntoLayer: (layer) => { + this.entity.layer = layer; + this.entity.emit('addedToLayer'); + }, + + }; } } diff --git a/packages/topdown/traits/roomed.trait.js b/packages/topdown/traits/roomed.trait.js index 953c657..3c31e47 100644 --- a/packages/topdown/traits/roomed.trait.js +++ b/packages/topdown/traits/roomed.trait.js @@ -7,9 +7,7 @@ export class Roomed extends Trait { } destroy() { - const room = this.entity.room; - this.entity.room = null; - this.entity.emit('removedFromRoom', room); + this.entity.removeFromRoom(); } methods() { @@ -17,7 +15,10 @@ export class Roomed extends Trait { removeFromRoom: () => { const room = this.entity.room; - this.entity.room = undefined; + if (!room) { + return; + } + this.entity.room = null; this.entity.emit('removedFromRoom', room); },