feat: died packet

This commit is contained in:
cha0s 2019-10-02 00:10:14 -05:00
parent 4340be5e2d
commit 25ff3f270d
2 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,3 @@
import {Packet} from '@avocado/net';
export class DiedPacket extends Packet {}

View File

@ -8,6 +8,7 @@ import {
import {compose} from '@avocado/core';
import {StateProperty, Trait} from '../trait';
import {DiedPacket} from '../packets/died.packet';
import {TraitUpdateAlivePacket} from '../packets/trait-update-alive.packet';
const decorate = compose(
@ -52,6 +53,7 @@ export class Alive extends decorate(Trait) {
static defaultState() {
return {
isDying: false,
life: 100,
maxLife: 100,
};
@ -83,6 +85,9 @@ export class Alive extends decorate(Trait) {
this.entity.life = packet.data.life;
this.entity.maxLife = packet.data.maxLife;
}
if (packet instanceof DiedPacket) {
this.entity.forceDeath();
}
}
get deathSound() {
@ -90,10 +95,15 @@ export class Alive extends decorate(Trait) {
}
packets(informed) {
const {life, maxLife} = this.stateDifferences();
const packets = [];
const {isDying, life, maxLife} = this.stateDifferences();
if (life || maxLife) {
return new TraitUpdateAlivePacket(this.state);
packets.push(new TraitUpdateAlivePacket(this.state));
}
if (isDying) {
packets.push(new DiedPacket());
}
return packets;
}
listeners() {
@ -129,6 +139,7 @@ export class Alive extends decorate(Trait) {
if (this._dyingTickingPromise) {
return;
}
this.state.isDying = true;
this._dyingTickingPromise = this._deathActions.tickingPromise(
this._context
)
@ -145,9 +156,11 @@ export class Alive extends decorate(Trait) {
this._dyingTickingPromise.tick(elapsed);
}
else {
if (AVOCADO_SERVER) {
this.entity.dieIfPossible();
}
}
}
}