refactor: client behavior

This commit is contained in:
cha0s 2021-02-13 08:13:28 -06:00
parent 87a371a9cb
commit bc9a3b9af1

View File

@ -170,6 +170,7 @@ export default (latus) => class Behaved extends decorate(Trait) {
},
]);
this.updateCurrentRoutine(this.state.currentRoutine);
super.isBehaving = 'client' !== process.env.SIDE;
}
methods() {
@ -188,53 +189,51 @@ export default (latus) => class Behaved extends decorate(Trait) {
}
tick(elapsed) {
if ('client' !== process.env.SIDE) {
if (this.entity.isBehaving) {
this.#accumulator += elapsed;
if (this.#accumulator >= STATIC_INTERVAL) {
for (let i = 0; i < this.#daemons.length; ++i) {
this.#daemons[i].tick(this.#context, elapsed);
if (this.entity.isBehaving) {
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;
}
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);
});
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);
}
this.#accumulator -= STATIC_INTERVAL;
}
if (this.#currentRoutine) {
this.#currentRoutine.tick(this.#context, elapsed);
}
}
}