diff --git a/packages/core/event-emitter.js b/packages/core/event-emitter.js index f65a230..6693a75 100644 --- a/packages/core/event-emitter.js +++ b/packages/core/event-emitter.js @@ -60,7 +60,9 @@ export function EventEmitterMixin(Superclass) { } off(typesOrType, fn) { - typesOrType = Array.isArray(typesOrType) ? typesOrType : [typesOrType]; + if (!Array.isArray(typesOrType)) { + typesOrType = [typesOrType]; + } for (let i = 0; i < typesOrType.length; i++) { const type = typesOrType[i]; this.offSingleEvent(type, fn); @@ -91,7 +93,9 @@ export function EventEmitterMixin(Superclass) { } _on(typesOrType, fn, that, once) { - typesOrType = Array.isArray(typesOrType) ? typesOrType : [typesOrType]; + if (!Array.isArray(typesOrType)) { + typesOrType = [typesOrType]; + } for (let i = 0; i < typesOrType.length; i++) { const type = typesOrType[i]; this.onSingleEvent(type, fn, that, once); diff --git a/packages/entity/entity.synchronized.js b/packages/entity/entity.synchronized.js index 5d976e5..5e7102c 100644 --- a/packages/entity/entity.synchronized.js +++ b/packages/entity/entity.synchronized.js @@ -280,7 +280,13 @@ export class Entity extends decorate(Resource) { const packets = []; const updates = []; for (const type in this._traits) { - const traitPackets = this._traits[type].packets(informed); + let traitPackets = this._traits[type].packets(informed); + if (!traitPackets) { + continue; + } + if (!Array.isArray(traitPackets)) { + traitPackets = [traitPackets]; + } if (traitPackets.length > 0) { updates.push({ type, diff --git a/packages/entity/packets/trait-alive.packet.js b/packages/entity/packets/trait-alive.packet.js index ad278f4..9134acd 100644 --- a/packages/entity/packets/trait-alive.packet.js +++ b/packages/entity/packets/trait-alive.packet.js @@ -1,10 +1,12 @@ -// export class TraitAlivePacket extends EntityPacket { +import {Packet} from '@avocado/net'; -// static get schema() { -// const schema = super.schema; -// schema.data.life = 'uint16'; -// schema.data.maxLife = 'uint16'; -// return schema; -// } +export class TraitAlivePacket extends Packet { -// } + static get schema() { + const schema = super.schema; + schema.data.life = 'uint16'; + schema.data.maxLife = 'uint16'; + return schema; + } + +} diff --git a/packages/entity/packets/trait-directional.packet.js b/packages/entity/packets/trait-directional.packet.js deleted file mode 100644 index 0bf9d7a..0000000 --- a/packages/entity/packets/trait-directional.packet.js +++ /dev/null @@ -1,9 +0,0 @@ -// export class TraitDirectionalPacket extends EntityPacket { - -// static get schema() { -// const schema = super.schema; -// schema.data.direction = 'uint8'; -// return schema; -// } - -// } diff --git a/packages/entity/packets/trait-update-directional-direction.packet.js b/packages/entity/packets/trait-update-directional-direction.packet.js new file mode 100644 index 0000000..779959f --- /dev/null +++ b/packages/entity/packets/trait-update-directional-direction.packet.js @@ -0,0 +1,12 @@ +import {Packet} from '@avocado/net'; + +export class TraitUpdateDirectionalDirectionPacket extends Packet { + + static get schema() { + return { + ...super.schema, + data: 'uint8', + }; + } + +} diff --git a/packages/entity/traits/directional.trait.js b/packages/entity/traits/directional.trait.js index 2b9f33a..76362c8 100644 --- a/packages/entity/traits/directional.trait.js +++ b/packages/entity/traits/directional.trait.js @@ -2,7 +2,9 @@ import {compose} from '@avocado/core'; import {Vector} from '@avocado/math'; import {StateProperty, Trait} from '../trait'; -import {TraitDirectionalPacket} from '../packets/trait-directional.packet'; +import { + TraitUpdateDirectionalDirectionPacket, +} from '../packets/trait-update-directional-direction.packet'; const decorate = compose( StateProperty('direction', { @@ -31,21 +33,32 @@ export class Directional extends decorate(Trait) { constructor(entity, params, state) { super(entity, params, state); + this._directionChanged = false; this.directionCount = this.params.directionCount; } acceptPacket(packet) { - if (packet instanceof TraitDirectionalPacket) { - this.entity.direction = packet.data.direction; + if (packet instanceof TraitUpdateDirectionalDirectionPacket) { + this.entity.direction = packet.data; } } - packetsForUpdate() { - return this.createTraitPacketUpdates(TraitDirectionalPacket); + cleanPackets() { + this._directionChanged = false; + } + + packets(informed) { + if (this._directionChanged) { + return new TraitUpdateDirectionalDirectionPacket(this.entity.direction); + } } listeners() { - const listeners = {}; + const listeners = { + directionChanged: () => { + this._directionChanged = true; + }, + }; if (this.params.trackMovement) { listeners.movementRequest = (vector) => { if (Vector.isZero(vector)) { diff --git a/packages/entity/traits/positioned.trait.js b/packages/entity/traits/positioned.trait.js index f65cb58..fe32429 100644 --- a/packages/entity/traits/positioned.trait.js +++ b/packages/entity/traits/positioned.trait.js @@ -78,18 +78,16 @@ export class Positioned extends decorate(Trait) { } packets(informed) { - const packets = []; if (this._positionChanged) { // Physics slop can end us up with negatives. Don't allow them in the // packed representation, even though it means a slight loss in accuracy. // The world bounds will (should!) keep things *eventually* correct. const x = Math.max(0, this.state.x); const y = Math.max(0, this.state.y); - packets.push(new TraitUpdatePositionedPositionPacket({ + return new TraitUpdatePositionedPositionPacket({ position: [x, y], - }, this.entity)); + }); } - return packets; } set relaxServerPositionConstraintIfNearerThan(nearerThan) { diff --git a/packages/graphics/packets/trait-visible.packet.js b/packages/graphics/packets/trait-visible.packet.js index c652bd5..a4d8b03 100644 --- a/packages/graphics/packets/trait-visible.packet.js +++ b/packages/graphics/packets/trait-visible.packet.js @@ -1,10 +1,12 @@ -// export class TraitVisiblePacket extends EntityPacket { +import {Packet} from '@avocado/net'; -// static get schema() { -// const schema = super.schema; -// schema.data.isVisible = 'bool'; -// schema.data.opacity = 'uint8'; -// return schema; -// } +export class TraitVisiblePacket extends Packet { -// } + static get schema() { + const schema = super.schema; + schema.data.isVisible = 'bool'; + schema.data.opacity = 'uint8'; + return schema; + } + +} diff --git a/packages/net/s13n/synchronized.js b/packages/net/s13n/synchronized.js index 804ca8a..1b081e1 100644 --- a/packages/net/s13n/synchronized.js +++ b/packages/net/s13n/synchronized.js @@ -33,7 +33,9 @@ export function SynchronizedMixin(Superclass) { if (!packets) { return []; } - packets = Array.isArray(packets) ? packets : [packets]; + if (!Array.isArray(packets)) { + packets = [packets]; + } if (this.packetsAreIdempotent()) { this._idempotentPackets = packets; } diff --git a/packages/timing/packets/trait-animated.packet.js b/packages/timing/packets/trait-animated.packet.js index a32d150..8f8ea47 100644 --- a/packages/timing/packets/trait-animated.packet.js +++ b/packages/timing/packets/trait-animated.packet.js @@ -1,10 +1,12 @@ -// export class TraitAnimatedPacket extends EntityPacket { +import {Packet} from '@avocado/net'; -// static get schema() { -// const schema = super.schema; -// schema.data.currentAnimation = 'string'; -// schema.data.isAnimating = 'bool'; -// return schema; -// } +export class TraitAnimatedPacket extends Packet { -// } + static get schema() { + const schema = super.schema; + schema.data.currentAnimation = 'string'; + schema.data.isAnimating = 'bool'; + return schema; + } + +}