diff --git a/packages/entity/entity.synchronized.js b/packages/entity/entity.synchronized.js index 692fb7f..96044e6 100644 --- a/packages/entity/entity.synchronized.js +++ b/packages/entity/entity.synchronized.js @@ -92,6 +92,7 @@ export class Entity extends decorate(Resource) { constructor(json, jsonext) { super(); + this._fastDirtyCheck = true; this._hooks = {}; this._hydrationPromise = undefined; this._json = Entity.jsonWithDefaults(json); @@ -205,9 +206,13 @@ export class Entity extends decorate(Resource) { } cleanPackets() { + if (!this._fastDirtyCheck) { + return; + } for (let i = 0; i < this._traitsFlat.length; i++) { this._traitsFlat[i].cleanPackets(); } + this._fastDirtyCheck = false; } fromJSON(json) { diff --git a/packages/entity/trait/index.js b/packages/entity/trait/index.js index 1a9baba..37ed3d6 100644 --- a/packages/entity/trait/index.js +++ b/packages/entity/trait/index.js @@ -12,6 +12,7 @@ export class Trait extends decorate(class {}) { super(); this.entity = entity; const ctor = this.constructor; + this._fastDirtyCheck = true; this._memoizedListeners = undefined; this.params = Object.assign({}, ctor.defaultParams(), params); this.state = Object.assign({}, ctor.defaultState(), state); @@ -28,9 +29,13 @@ export class Trait extends decorate(class {}) { } cleanPackets() { + if (!this._fastDirtyCheck) { + return; + } for (const key in this.state) { this.previousState[key] = this.state[key]; } + this._fastDirtyCheck = false; } static contextType() { @@ -148,6 +153,10 @@ export function StateProperty(key, meta = {}) { return this.${transformedProperty}; `); meta.set = meta.set || new Function('value', ` + if (value !== this.${transformedProperty}) { + this._fastDirtyCheck = true; + this.entity._fastDirtyCheck = true; + } this.${transformedProperty} = value; this.state['${key}'] = value; `); diff --git a/packages/entity/traits/alive.trait.js b/packages/entity/traits/alive.trait.js index 0fe2732..e8cf785 100644 --- a/packages/entity/traits/alive.trait.js +++ b/packages/entity/traits/alive.trait.js @@ -140,6 +140,8 @@ export class Alive extends decorate(Trait) { return; } this.state.isDying = true; + this.entity._fastDirtyCheck = true; + this._fastDirtyCheck = true; this._dyingTickingPromise = this._deathActions.tickingPromise( this._context ) diff --git a/packages/entity/traits/positioned.trait.js b/packages/entity/traits/positioned.trait.js index b3c4115..222f8c7 100644 --- a/packages/entity/traits/positioned.trait.js +++ b/packages/entity/traits/positioned.trait.js @@ -63,6 +63,8 @@ export class Positioned extends decorate(Trait) { if (AVOCADO_SERVER) { this.state.x = newPosition[0]; this.state.y = newPosition[1]; + this.entity._fastDirtyCheck = true; + this._fastDirtyCheck = true; } this.entity.emit('positionChanged', oldPosition, newPosition); }