fix: alive trait sync

This commit is contained in:
cha0s 2019-09-30 20:57:04 -05:00
parent 204907c27f
commit 0305d24c73
3 changed files with 22 additions and 16 deletions

View File

@ -1,12 +0,0 @@
import {Packet} from '@avocado/net';
export class TraitAlivePacket extends Packet {
static get schema() {
const schema = super.schema;
schema.data.life = 'uint16';
schema.data.maxLife = 'uint16';
return schema;
}
}

View File

@ -0,0 +1,15 @@
import {Packet} from '@avocado/net';
export class TraitUpdateAlivePacket extends Packet {
static get schema() {
return {
...super.schema,
data: {
life: 'uint16',
maxLife: 'uint16',
}
};
}
}

View File

@ -8,7 +8,7 @@ import {
import {compose} from '@avocado/core'; import {compose} from '@avocado/core';
import {StateProperty, Trait} from '../trait'; import {StateProperty, Trait} from '../trait';
import {TraitAlivePacket} from '../packets/trait-alive.packet'; import {TraitUpdateAlivePacket} from '../packets/trait-update-alive.packet';
const decorate = compose( const decorate = compose(
StateProperty('life', { StateProperty('life', {
@ -79,7 +79,7 @@ export class Alive extends decorate(Trait) {
} }
acceptPacket(packet) { acceptPacket(packet) {
if (packet instanceof TraitAlivePacket) { if (packet instanceof TraitUpdateAlivePacket) {
this.entity.life = packet.data.life; this.entity.life = packet.data.life;
this.entity.maxLife = packet.data.maxLife; this.entity.maxLife = packet.data.maxLife;
} }
@ -89,8 +89,11 @@ export class Alive extends decorate(Trait) {
return this._deathSound; return this._deathSound;
} }
packetsForUpdate() { packets(informed) {
return this.createTraitPacketUpdates(TraitAlivePacket); const {life, maxLife} = this.stateDifferences();
if (life || maxLife) {
return new TraitUpdateAlivePacket(this.state);
}
} }
listeners() { listeners() {