refactor: alive moved to core
This commit is contained in:
parent
798075b685
commit
25136fe1e9
|
@ -1,150 +0,0 @@
|
|||
import {
|
||||
behaviorItemFromJSON,
|
||||
buildCondition,
|
||||
buildInvoke,
|
||||
buildTraversal,
|
||||
createContext,
|
||||
} from '@avocado/behavior';
|
||||
import {compose} from '@avocado/core';
|
||||
import {StateProperty, Trait} from '@avocado/entity';
|
||||
|
||||
import {TraitAlivePacket} from './trait-alive.packet';
|
||||
|
||||
const decorate = compose(
|
||||
StateProperty('life', {
|
||||
track: true,
|
||||
}),
|
||||
StateProperty('maxLife', {
|
||||
track: true,
|
||||
}),
|
||||
);
|
||||
|
||||
export class Alive extends decorate(Trait) {
|
||||
|
||||
static defaultParams() {
|
||||
const playDeathSound = buildInvoke(['entity', 'playSound'], [
|
||||
buildTraversal(['entity', 'deathSound']),
|
||||
]);
|
||||
const squeeze = buildInvoke(['entity', 'transition'], [
|
||||
{
|
||||
opacity: 0,
|
||||
visibleScaleX: .3,
|
||||
visibleScaleY: 3,
|
||||
},
|
||||
0.2,
|
||||
]);
|
||||
const isLifeGone = buildCondition('<=', [
|
||||
buildTraversal(['entity', 'life']),
|
||||
0,
|
||||
]);
|
||||
return {
|
||||
deathActions: {
|
||||
type: 'actions',
|
||||
traversals: [
|
||||
playDeathSound,
|
||||
squeeze,
|
||||
],
|
||||
},
|
||||
deathCondition: isLifeGone,
|
||||
deathSound: 'deathSound',
|
||||
};
|
||||
}
|
||||
|
||||
static defaultState() {
|
||||
return {
|
||||
life: 100,
|
||||
maxLife: 100,
|
||||
};
|
||||
}
|
||||
|
||||
static type() {
|
||||
return 'alive';
|
||||
}
|
||||
|
||||
constructor(entity, params, state) {
|
||||
super(entity, params, state);
|
||||
this._context = createContext();
|
||||
this._context.add('entity', this.entity);
|
||||
const actionsJSON = this.params.deathActions;
|
||||
this._deathActions = behaviorItemFromJSON(actionsJSON);
|
||||
this._deathSound = this.params.deathSound;
|
||||
const conditionJSON = this.params.deathCondition;
|
||||
this._deathCondition = behaviorItemFromJSON(conditionJSON);
|
||||
this._isDying = false;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._context.clear();
|
||||
}
|
||||
|
||||
acceptPacket(packet) {
|
||||
if (packet instanceof TraitAlivePacket) {
|
||||
this.entity.life = packet.data.life;
|
||||
this.entity.maxLife = packet.data.maxLife;
|
||||
}
|
||||
}
|
||||
|
||||
get deathSound() {
|
||||
return this._deathSound;
|
||||
}
|
||||
|
||||
packetsForUpdate() {
|
||||
return this.createTraitPacketUpdates(TraitAlivePacket);
|
||||
}
|
||||
|
||||
listeners() {
|
||||
return {
|
||||
|
||||
tookDamage: (damage, source) => {
|
||||
if (damage.damageSpec.power > 0) {
|
||||
this.entity.life -= damage.amount;
|
||||
}
|
||||
else {
|
||||
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
|
||||
);
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
methods() {
|
||||
return {
|
||||
|
||||
dieIfPossible: () => {
|
||||
if (this._deathCondition.check(this._context)) {
|
||||
this.entity.forceDeath();
|
||||
}
|
||||
},
|
||||
|
||||
forceDeath: () => {
|
||||
if (this._isDying) {
|
||||
return;
|
||||
}
|
||||
this._isDying = true;
|
||||
this.entity.emit('dying');
|
||||
this._deathActions.once('actionsFinished', () => {
|
||||
if (this.entity.is('existent')) {
|
||||
this.entity.destroy();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
tick(elapsed) {
|
||||
if (this._isDying) {
|
||||
this._deathActions.tick(this._context, elapsed);
|
||||
}
|
||||
else {
|
||||
this.entity.dieIfPossible();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
import {EntityPacket} from '@avocado/entity';
|
||||
|
||||
export class TraitAlivePacket extends EntityPacket {
|
||||
|
||||
static get schema() {
|
||||
const schema = super.schema;
|
||||
schema.data.life = 'uint16';
|
||||
schema.data.maxLife = 'uint16';
|
||||
return schema;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user