humus-old/common/combat/alive.trait.js

58 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-04-19 14:51:05 -05:00
import {compose} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/entity';
const decorate = compose(
StateProperty('life', {
track: true,
}),
StateProperty('maxLife', {
track: true,
}),
);
export class Alive extends decorate(Trait) {
static defaultState() {
return {
life: 100,
maxLife: 100,
};
}
listeners() {
return {
tookDamage: (damage) => {
this.entity.life -= damage.amount;
// Clamp health between 0 and max.
this.entity.life = Math.min(
Math.max(0, this.entity.life),
this.entity.maxLife
);
},
}
}
tick(elapsed) {
// ded
// @todo Custom death conditions.
if (this.entity.life <= 0) {
const allowDeath = this.entity.invokeHookFlat('allowDeath');
const overrides = allowDeath.filter((response) => {
return false === response;
});
// Something saved the day.
if (overrides.length > 0) {
}
else {
// It's a good day to die.
this.entity.destroy();
}
}
}
}