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 {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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user