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; this._deathSound = this.params.deathSound;
const conditionJSON = this.params.deathCondition; const conditionJSON = this.params.deathCondition;
this._deathCondition = behaviorItemFromJSON(conditionJSON); this._deathCondition = behaviorItemFromJSON(conditionJSON);
this._isDying = false; this._dyingTickingPromise = false;
} }
destroy() { destroy() {
@ -123,15 +123,17 @@ export class Alive extends decorate(Trait) {
}, },
forceDeath: () => { forceDeath: () => {
if (this._isDying) { if (this._dyingTickingPromise) {
return; return;
} }
this._isDying = true; this._dyingTickingPromise = this._deathActions.tickingPromise(
this.entity.emit('dying'); this._context
this._deathActions.once('actionsFinished', () => { )
if (this.entity.is('existent')) { this._dyingTickingPromise.then(() => {
this.entity.destroy(); 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) { tick(elapsed) {
if (this._isDying) { if (this._dyingTickingPromise) {
this._deathActions.tick(this._context, elapsed); this._dyingTickingPromise.tick(elapsed);
} }
else { else {
this.entity.dieIfPossible(); this.entity.dieIfPossible();