chore: perf

This commit is contained in:
cha0s 2021-02-11 18:07:47 -06:00
parent 78416fbb72
commit 6b7405c90f

View File

@ -14,8 +14,12 @@ const decorate = compose(
StateProperty('isBehaving'),
);
const STATIC_INTERVAL = 0.25;
export default (latus) => class Behaved extends decorate(Trait) {
#accumulator = 0;
#collectives = [];
#context = new Context({}, latus);
@ -186,47 +190,51 @@ export default (latus) => class Behaved extends decorate(Trait) {
tick(elapsed) {
if ('client' !== process.env.SIDE) {
if (this.entity.isBehaving) {
for (let i = 0; i < this.#daemons.length; ++i) {
this.#daemons[i].tick(this.#context, elapsed);
this.#accumulator += elapsed;
if (this.#accumulator >= STATIC_INTERVAL) {
for (let i = 0; i < this.#daemons.length; ++i) {
this.#daemons[i].tick(this.#context, elapsed);
}
for (let i = 0; i < this.#collectives.length; ++i) {
const [
key,
{
find,
max,
reset,
threshold,
},
] = this.#collectives[i];
if (this.entity.activeCollective && this.entity.activeCollective !== key) {
// eslint-disable-next-line no-continue
continue;
}
const crew = find(this.#context);
const isInCrew = -1 !== crew.indexOf(this.entity);
const hasLength = (
!this.entity.activeCollective
|| 'threshold' === this.entity.context.get('crew').length
)
? crew.length >= threshold
: crew.length > this.entity.context.get('crew').length;
if (isInCrew && hasLength && crew.length < max) {
crew.forEach((entity, i) => {
// eslint-disable-next-line no-param-reassign
entity.activeCollective = key;
const {context} = entity;
context.add('crew', crew);
context.add('index', i);
reset().forEach((expr) => {
expr(context);
});
});
}
}
this.#accumulator -= STATIC_INTERVAL;
}
if (this.#currentRoutine) {
this.#currentRoutine.tick(this.#context, elapsed);
}
for (let i = 0; i < this.#collectives.length; ++i) {
const [
key,
{
find,
max,
reset,
threshold,
},
] = this.#collectives[i];
if (this.entity.activeCollective && this.entity.activeCollective !== key) {
// eslint-disable-next-line no-continue
continue;
}
const crew = find(this.#context);
const isInCrew = -1 !== crew.indexOf(this.entity);
const hasLength = (
!this.entity.activeCollective
|| 'threshold' === this.entity.context.get('crew').length
)
? crew.length >= threshold
: crew.length > this.entity.context.get('crew').length;
if (isInCrew && hasLength && crew.length < max) {
crew.forEach((entity, i) => {
// eslint-disable-next-line no-param-reassign
entity.activeCollective = key;
const {context} = entity;
context.add('crew', crew);
context.add('index', i);
reset().forEach((expr) => {
expr(context);
});
});
}
}
}
}
}