feat: _fastDirtyCheck for traits and entities, cleanPackets was getting rough

This commit is contained in:
cha0s 2019-10-03 00:53:56 -05:00
parent b6dd246ec1
commit 9f2e2960c4
4 changed files with 18 additions and 0 deletions

View File

@ -92,6 +92,7 @@ export class Entity extends decorate(Resource) {
constructor(json, jsonext) { constructor(json, jsonext) {
super(); super();
this._fastDirtyCheck = true;
this._hooks = {}; this._hooks = {};
this._hydrationPromise = undefined; this._hydrationPromise = undefined;
this._json = Entity.jsonWithDefaults(json); this._json = Entity.jsonWithDefaults(json);
@ -205,9 +206,13 @@ export class Entity extends decorate(Resource) {
} }
cleanPackets() { cleanPackets() {
if (!this._fastDirtyCheck) {
return;
}
for (let i = 0; i < this._traitsFlat.length; i++) { for (let i = 0; i < this._traitsFlat.length; i++) {
this._traitsFlat[i].cleanPackets(); this._traitsFlat[i].cleanPackets();
} }
this._fastDirtyCheck = false;
} }
fromJSON(json) { fromJSON(json) {

View File

@ -12,6 +12,7 @@ export class Trait extends decorate(class {}) {
super(); super();
this.entity = entity; this.entity = entity;
const ctor = this.constructor; const ctor = this.constructor;
this._fastDirtyCheck = true;
this._memoizedListeners = undefined; this._memoizedListeners = undefined;
this.params = Object.assign({}, ctor.defaultParams(), params); this.params = Object.assign({}, ctor.defaultParams(), params);
this.state = Object.assign({}, ctor.defaultState(), state); this.state = Object.assign({}, ctor.defaultState(), state);
@ -28,9 +29,13 @@ export class Trait extends decorate(class {}) {
} }
cleanPackets() { cleanPackets() {
if (!this._fastDirtyCheck) {
return;
}
for (const key in this.state) { for (const key in this.state) {
this.previousState[key] = this.state[key]; this.previousState[key] = this.state[key];
} }
this._fastDirtyCheck = false;
} }
static contextType() { static contextType() {
@ -148,6 +153,10 @@ export function StateProperty(key, meta = {}) {
return this.${transformedProperty}; return this.${transformedProperty};
`); `);
meta.set = meta.set || new Function('value', ` meta.set = meta.set || new Function('value', `
if (value !== this.${transformedProperty}) {
this._fastDirtyCheck = true;
this.entity._fastDirtyCheck = true;
}
this.${transformedProperty} = value; this.${transformedProperty} = value;
this.state['${key}'] = value; this.state['${key}'] = value;
`); `);

View File

@ -140,6 +140,8 @@ export class Alive extends decorate(Trait) {
return; return;
} }
this.state.isDying = true; this.state.isDying = true;
this.entity._fastDirtyCheck = true;
this._fastDirtyCheck = true;
this._dyingTickingPromise = this._deathActions.tickingPromise( this._dyingTickingPromise = this._deathActions.tickingPromise(
this._context this._context
) )

View File

@ -63,6 +63,8 @@ export class Positioned extends decorate(Trait) {
if (AVOCADO_SERVER) { if (AVOCADO_SERVER) {
this.state.x = newPosition[0]; this.state.x = newPosition[0];
this.state.y = newPosition[1]; this.state.y = newPosition[1];
this.entity._fastDirtyCheck = true;
this._fastDirtyCheck = true;
} }
this.entity.emit('positionChanged', oldPosition, newPosition); this.entity.emit('positionChanged', oldPosition, newPosition);
} }