feat: died packet
This commit is contained in:
parent
4340be5e2d
commit
25ff3f270d
3
packages/entity/packets/died.packet.js
Normal file
3
packages/entity/packets/died.packet.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
import {Packet} from '@avocado/net';
|
||||
|
||||
export class DiedPacket extends Packet {}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user