avocado-old/packages/entity/traits/existent.trait.js

73 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-04-19 17:10:32 -05:00
import {compose, TickingPromise} from '@avocado/core';
2019-04-28 23:45:03 -05:00
import {TransitionResult} from '@avocado/timing';
2019-03-17 23:45:48 -05:00
2019-03-23 23:24:18 -05:00
import {StateProperty, Trait} from '../trait';
2019-03-17 23:45:48 -05:00
const decorate = compose(
2019-03-23 23:24:18 -05:00
StateProperty('name'),
StateProperty('isTicking'),
2019-03-17 23:45:48 -05:00
);
2019-05-05 04:26:35 -05:00
export class Existent extends decorate(Trait) {
2019-03-17 23:45:48 -05:00
static defaultState() {
return {
name: 'Untitled entity',
isTicking: true,
2019-03-17 23:45:48 -05:00
};
}
2019-05-05 04:26:35 -05:00
static type() {
return 'existent';
}
constructor(entity, params, state) {
super(entity, params, state);
2019-05-16 15:19:57 -05:00
this._isDestroying = false;
2019-05-04 14:06:47 -05:00
this._isTicking = this.params.isTicking;
2019-03-19 11:25:42 -05:00
}
2019-03-20 18:32:54 -05:00
methods() {
2019-03-17 23:45:48 -05:00
return {
destroy: () => {
2019-05-16 15:19:57 -05:00
if (this._isDestroying) {
return;
}
this._isDestroying = true;
this.entity.isTicking = false;
2019-03-17 23:45:48 -05:00
this.entity.emit('destroy');
this.entity.emit('destroyed');
},
destroyGently: () => {
if (this.entity.is('alive')) {
this.entity.forceDeath();
}
else {
this.entity.destroy();
}
},
2019-04-19 17:10:32 -05:00
transition: (props, duration, easing) => {
const result = new TransitionResult(
this.entity,
props,
duration,
easing
);
return new TickingPromise(
(resolve) => {
resolve(result.promise);
},
(elapsed) => {
result.tick(elapsed);
},
);
2019-04-19 17:10:32 -05:00
},
2019-03-17 23:45:48 -05:00
};
}
}