import {compose} from '@avocado/core'; import {StateProperty, Trait} from '@avocado/entity'; import {createContext} from '../context'; import {Routines} from '../item/routines'; const decorate = compose( StateProperty('currentRoutine'), ); export class Behaved extends decorate(Trait) { static defaultParams() { return { routines: {}, }; } static defaultState() { return { currentRoutine: 'initial', } } initialize() { this._context = createContext(); this._context.add('entity', this.entity); this._currentRoutine = undefined; this._isBehaving = true; const routinesJSON = this.params.get('routines').toJS(); this._routines = (new Routines()).fromJSON(routinesJSON); this._routines.context = this._context; this.updateCurrentRoutine(); } get context() { return this._context; } set isBehaving(isBehaving) { this._isBehaving = isBehaving; } updateCurrentRoutine() { this._currentRoutine = this._routines.routine( this.entity.currentRoutine ); } listeners() { return { currentRoutineChanged: () => { this.updateCurrentRoutine(); }, dying: () => { this._isBehaving = false; }, }; } tick(elapsed) { if (this._currentRoutine && this._isBehaving) { this._currentRoutine.tick(elapsed); } } }