perf: flat tickers

This commit is contained in:
cha0s 2019-05-05 20:04:59 -05:00
parent 4bece84a47
commit fa939ac3fc
2 changed files with 24 additions and 20 deletions

View File

@ -62,8 +62,8 @@ export class Entity extends decorate(Resource) {
this.isDirty = true;
this._traits = {};
this._traitsFlat = [];
this._traitsToTick = [];
this._traitsToRenderTick = [];
this._traitTickers = [];
this._traitRenderTickers = [];
this._traitsAcceptingPackets = [];
this.once('destroyed', () => {
this.removeAllTraits();
@ -137,10 +137,10 @@ export class Entity extends decorate(Resource) {
this._traits[type] = instance;
this._traitsFlat.push(instance);
if ('tick' in instance) {
this._traitsToTick.push(instance);
this._traitTickers.push(instance.tick);
}
if ('renderTick' in instance) {
this._traitsToRenderTick.push(instance);
this._traitRenderTickers.push(instance.renderTick);
}
if ('acceptPacket' in instance) {
this._traitsAcceptingPackets.push(instance);
@ -223,9 +223,8 @@ export class Entity extends decorate(Resource) {
}
renderTick(elapsed) {
for (let i = 0; i < this._traitsToRenderTick.length; i++) {
const instance = this._traitsToRenderTick[i];
instance.renderTick(elapsed);
for (let i = 0; i < this._traitRenderTickers.length; i++) {
this._traitRenderTickers[i](elapsed);
}
}
@ -268,13 +267,17 @@ export class Entity extends decorate(Resource) {
// Remove instance.
delete this._traits[type];
this._traitsFlat.splice(this._traitsFlat.indexOf(instance), 1);
const tickIndex = this._traitsToTick.indexOf(instance);
if (-1 !== tickIndex) {
this._traitsToTick.splice(tickIndex, 1);
if ('tick' in instance) {
this._traitTickers.splice(
this._traitTickers.indexOf(instance.tick),
1
);
}
const renderTickIndex = this._traitsToRenderTick.indexOf(instance);
if (-1 !== renderTickIndex) {
this._traitsToRenderTick.splice(renderTickIndex, 1);
if ('renderTick' in instance) {
this._traitRenderTickers.splice(
this._traitRenderTickers.indexOf(instance.renderTick),
1
);
}
const acceptPacketIndex = this._traitsAcceptingPackets.indexOf(instance);
if (-1 !== acceptPacketIndex) {
@ -297,13 +300,8 @@ export class Entity extends decorate(Resource) {
}
tick(elapsed) {
for (let i = 0; i < this._traitsToTick.length; i++) {
const instance = this._traitsToTick[i];
// If .destroy is called immediately when ticking a trait, then the
// entity will go away.
if (instance.entity) {
instance.tick(elapsed);
}
for (let i = 0; i < this._traitTickers.length; i++) {
this._traitTickers[i](elapsed);
}
if (AVOCADO_SERVER) {
this.tickMutateState();

View File

@ -21,6 +21,12 @@ export class Trait extends decorate(class {}) {
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);
}
if (this.renderTick) {
this.renderTick = this.renderTick.bind(this);
}
}
destroy() {}