flow: track current routine, isBehaving property, only behave on server

This commit is contained in:
cha0s 2019-06-05 20:12:45 -05:00
parent da941e0e39
commit 8aa6631b03

View File

@ -5,7 +5,10 @@ import {createContext} from '../context';
import {Routines} from '../item/routines';
const decorate = compose(
StateProperty('currentRoutine'),
StateProperty('currentRoutine', {
track: true,
}),
StateProperty('isBehaving'),
);
export class Behaved extends decorate(Trait) {
@ -19,6 +22,7 @@ export class Behaved extends decorate(Trait) {
static defaultState() {
return {
currentRoutine: 'initial',
isBehaving: true,
}
}
@ -31,7 +35,6 @@ export class Behaved extends decorate(Trait) {
this._context = createContext();
this._context.add('entity', this.entity);
this._currentRoutine = undefined;
this._isBehaving = true;
const routinesJSON = this.params.routines;
this._routines = (new Routines()).fromJSON(routinesJSON);
this._routines.context = this._context;
@ -48,10 +51,6 @@ export class Behaved extends decorate(Trait) {
return this._context;
}
set isBehaving(isBehaving) {
this._isBehaving = isBehaving;
}
updateCurrentRoutine(currentRoutine) {
this._currentRoutine = this._routines.routine(currentRoutine);
}
@ -64,15 +63,17 @@ export class Behaved extends decorate(Trait) {
},
dying: () => {
this._isBehaving = false;
this.entity.isBehaving = false;
},
};
}
tick(elapsed) {
if (this._currentRoutine && this._isBehaving) {
this._currentRoutine.tick(elapsed);
if (AVOCADO_SERVER) {
if (this._currentRoutine && this.entity.isBehaving) {
this._currentRoutine.tick(elapsed);
}
}
}