feat: living kitties!

This commit is contained in:
cha0s 2019-04-19 14:51:05 -05:00
parent f3bc4b8ecb
commit 81dd1c4562
4 changed files with 85 additions and 5 deletions

View File

@ -0,0 +1,57 @@
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();
}
}
}
}

View File

@ -73,6 +73,10 @@ export class DamageEmitter {
this.emitter.createParticle(initializers, behaviors);
}
hasParticles() {
return this.emitter.particles.length > 0;
}
tick(elapsed) {
this.proton.tick(elapsed);
}

View File

@ -50,6 +50,22 @@ export class Vulnerable extends Trait {
}
}
hooks() {
return {
afterDestructionTickers: () => {
return (elapsed) => {
if (!hasGraphics) {
return true;
}
this.emitter.tick(elapsed);
return !this.emitter.hasParticles();
};
},
}
}
listeners() {
return {
@ -92,12 +108,14 @@ export class Vulnerable extends Trait {
if (!this.damageList[entity.instanceUuid]) {
this.damageList[entity.instanceUuid] = [];
}
this.damageList[entity.instanceUuid].push({
const damage = {
id: this.damageId++,
isDamage,
amount,
damageSpec,
});
};
this.damageList[entity.instanceUuid].push(damage);
this.entity.emit('tookDamage', damage);
}
},
@ -105,6 +123,9 @@ export class Vulnerable extends Trait {
}
tick(elapsed) {
if (hasGraphics) {
this.emitter.tick(elapsed);
}
if (this.state.get('damageList').size > 0) {
this.state = this.state.set('damageList', I.Map());
}
@ -113,9 +134,6 @@ export class Vulnerable extends Trait {
this.isDirty = true;
this.damageList = {};
}
if (hasGraphics) {
this.emitter.tick(elapsed);
}
const keys = Array.from(this.locks.keys());
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];

View File

@ -127,6 +127,7 @@ function kittyJSON(position) {
}
return {
traits: {
alive: {},
animated: {
params: {
animations: {