From 25ff3f270d19aef3010acd265e6d233fed4b0889 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 2 Oct 2019 00:10:14 -0500 Subject: [PATCH] feat: died packet --- packages/entity/packets/died.packet.js | 3 +++ packages/entity/traits/alive.trait.js | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 packages/entity/packets/died.packet.js diff --git a/packages/entity/packets/died.packet.js b/packages/entity/packets/died.packet.js new file mode 100644 index 0000000..09bdae4 --- /dev/null +++ b/packages/entity/packets/died.packet.js @@ -0,0 +1,3 @@ +import {Packet} from '@avocado/net'; + +export class DiedPacket extends Packet {} diff --git a/packages/entity/traits/alive.trait.js b/packages/entity/traits/alive.trait.js index 221957e..0fe2732 100644 --- a/packages/entity/traits/alive.trait.js +++ b/packages/entity/traits/alive.trait.js @@ -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,7 +156,9 @@ export class Alive extends decorate(Trait) { this._dyingTickingPromise.tick(elapsed); } else { - this.entity.dieIfPossible(); + if (AVOCADO_SERVER) { + this.entity.dieIfPossible(); + } } }