refactor: ticking promise

This commit is contained in:
cha0s 2019-09-08 08:12:31 -05:00
parent a856950931
commit 4f6869863c

View File

@ -71,7 +71,7 @@ export class Alive extends decorate(Trait) {
this._deathSound = this.params.deathSound;
const conditionJSON = this.params.deathCondition;
this._deathCondition = behaviorItemFromJSON(conditionJSON);
this._isDying = false;
this._dyingTickingPromise = false;
}
destroy() {
@ -123,15 +123,17 @@ export class Alive extends decorate(Trait) {
},
forceDeath: () => {
if (this._isDying) {
if (this._dyingTickingPromise) {
return;
}
this._isDying = true;
this.entity.emit('dying');
this._deathActions.once('actionsFinished', () => {
if (this.entity.is('existent')) {
this.entity.destroy();
this._dyingTickingPromise = this._deathActions.tickingPromise(
this._context
)
this._dyingTickingPromise.then(() => {
if (!this.entity.is('existent')) {
throw new Error("STATE PROBLEM: non-existent entity up for death");
}
this.entity.destroy();
});
},
@ -139,8 +141,8 @@ export class Alive extends decorate(Trait) {
}
tick(elapsed) {
if (this._isDying) {
this._deathActions.tick(this._context, elapsed);
if (this._dyingTickingPromise) {
this._dyingTickingPromise.tick(elapsed);
}
else {
this.entity.dieIfPossible();