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'),
|
2019-04-11 23:49:37 -05:00
|
|
|
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',
|
2019-04-11 23:49:37 -05:00
|
|
|
isTicking: true,
|
2019-03-17 23:45:48 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:26:35 -05:00
|
|
|
static type() {
|
|
|
|
return 'existent';
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:03:43 -05:00
|
|
|
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;
|
2019-04-11 23:49:37 -05:00
|
|
|
this.entity.isTicking = false;
|
2019-03-17 23:45:48 -05:00
|
|
|
this.entity.emit('destroy');
|
|
|
|
this.entity.emit('destroyed');
|
|
|
|
},
|
|
|
|
|
2019-05-14 05:58:04 -05:00
|
|
|
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
|
|
|
|
);
|
2019-09-08 05:34:17 -05:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|