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