avocado-old/packages/entity/traits/existent.trait.js
2020-06-23 11:19:59 -05:00

116 lines
2.2 KiB
JavaScript

import {compose, TickingPromise} from '@avocado/core';
import {TransitionResult} from '@avocado/timing';
import {StateProperty, Trait} from '../trait';
const decorate = compose(
StateProperty('name'),
StateProperty('isTicking'),
);
export default class Existent extends decorate(Trait) {
static behaviorTypes() {
return {
destroy: {
type: 'void',
label: 'Destroy',
},
destroyGently: {
type: 'void',
label: 'Kill? Then destroy',
},
transition: {
type: 'void',
label: 'Transition $1 for $2 seconds using $3.',
args: [
['props', {
type: 'object',
}],
['duration', {
type: 'number',
}],
['easing', {
type: 'string',
}],
],
},
};
}
static defaultState() {
return {
isTicking: true,
name: 'Untitled entity',
};
}
static describeState() {
return {
isTicking: {
type: 'bool',
label: 'Is ticking',
},
name: {
type: 'string',
label: 'Name',
},
}
}
static type() {
return 'existent';
}
constructor(entity, params, state) {
super(entity, params, state);
this._isDestroying = false;
this._isTicking = this.params.isTicking;
}
methods() {
return {
destroy: () => {
if (this._isDestroying) {
return;
}
this._isDestroying = true;
this.entity.isTicking = false;
return Promise.resolve().then(() => {
this.entity.emit('destroy');
this.entity.emit('destroyed');
});
},
destroyGently: () => {
if (this.entity.is('alive')) {
this.entity.forceDeath();
}
else {
this.entity.destroy();
}
},
transition: (props, duration, easing) => {
const result = new TransitionResult(
this.entity,
props,
duration,
easing
);
return new TickingPromise(
(resolve) => {
resolve(result.promise);
},
(elapsed) => {
result.tick(elapsed);
},
);
},
};
}
}