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