fix: directional packet

This commit is contained in:
cha0s 2019-09-30 15:35:57 -05:00
parent 909cc388f2
commit 40a9731147
10 changed files with 79 additions and 47 deletions

View File

@ -60,7 +60,9 @@ export function EventEmitterMixin(Superclass) {
} }
off(typesOrType, fn) { off(typesOrType, fn) {
typesOrType = Array.isArray(typesOrType) ? typesOrType : [typesOrType]; if (!Array.isArray(typesOrType)) {
typesOrType = [typesOrType];
}
for (let i = 0; i < typesOrType.length; i++) { for (let i = 0; i < typesOrType.length; i++) {
const type = typesOrType[i]; const type = typesOrType[i];
this.offSingleEvent(type, fn); this.offSingleEvent(type, fn);
@ -91,7 +93,9 @@ export function EventEmitterMixin(Superclass) {
} }
_on(typesOrType, fn, that, once) { _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++) { for (let i = 0; i < typesOrType.length; i++) {
const type = typesOrType[i]; const type = typesOrType[i];
this.onSingleEvent(type, fn, that, once); this.onSingleEvent(type, fn, that, once);

View File

@ -280,7 +280,13 @@ export class Entity extends decorate(Resource) {
const packets = []; const packets = [];
const updates = []; const updates = [];
for (const type in this._traits) { 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) { if (traitPackets.length > 0) {
updates.push({ updates.push({
type, type,

View File

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

View File

@ -1,9 +0,0 @@
// export class TraitDirectionalPacket extends EntityPacket {
// static get schema() {
// const schema = super.schema;
// schema.data.direction = 'uint8';
// return schema;
// }
// }

View File

@ -0,0 +1,12 @@
import {Packet} from '@avocado/net';
export class TraitUpdateDirectionalDirectionPacket extends Packet {
static get schema() {
return {
...super.schema,
data: 'uint8',
};
}
}

View File

@ -2,7 +2,9 @@ import {compose} from '@avocado/core';
import {Vector} from '@avocado/math'; import {Vector} from '@avocado/math';
import {StateProperty, Trait} from '../trait'; import {StateProperty, Trait} from '../trait';
import {TraitDirectionalPacket} from '../packets/trait-directional.packet'; import {
TraitUpdateDirectionalDirectionPacket,
} from '../packets/trait-update-directional-direction.packet';
const decorate = compose( const decorate = compose(
StateProperty('direction', { StateProperty('direction', {
@ -31,21 +33,32 @@ export class Directional extends decorate(Trait) {
constructor(entity, params, state) { constructor(entity, params, state) {
super(entity, params, state); super(entity, params, state);
this._directionChanged = false;
this.directionCount = this.params.directionCount; this.directionCount = this.params.directionCount;
} }
acceptPacket(packet) { acceptPacket(packet) {
if (packet instanceof TraitDirectionalPacket) { if (packet instanceof TraitUpdateDirectionalDirectionPacket) {
this.entity.direction = packet.data.direction; this.entity.direction = packet.data;
} }
} }
packetsForUpdate() { cleanPackets() {
return this.createTraitPacketUpdates(TraitDirectionalPacket); this._directionChanged = false;
}
packets(informed) {
if (this._directionChanged) {
return new TraitUpdateDirectionalDirectionPacket(this.entity.direction);
}
} }
listeners() { listeners() {
const listeners = {}; const listeners = {
directionChanged: () => {
this._directionChanged = true;
},
};
if (this.params.trackMovement) { if (this.params.trackMovement) {
listeners.movementRequest = (vector) => { listeners.movementRequest = (vector) => {
if (Vector.isZero(vector)) { if (Vector.isZero(vector)) {

View File

@ -78,18 +78,16 @@ export class Positioned extends decorate(Trait) {
} }
packets(informed) { packets(informed) {
const packets = [];
if (this._positionChanged) { if (this._positionChanged) {
// Physics slop can end us up with negatives. Don't allow them in the // 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. // packed representation, even though it means a slight loss in accuracy.
// The world bounds will (should!) keep things *eventually* correct. // The world bounds will (should!) keep things *eventually* correct.
const x = Math.max(0, this.state.x); const x = Math.max(0, this.state.x);
const y = Math.max(0, this.state.y); const y = Math.max(0, this.state.y);
packets.push(new TraitUpdatePositionedPositionPacket({ return new TraitUpdatePositionedPositionPacket({
position: [x, y], position: [x, y],
}, this.entity)); });
} }
return packets;
} }
set relaxServerPositionConstraintIfNearerThan(nearerThan) { set relaxServerPositionConstraintIfNearerThan(nearerThan) {

View File

@ -1,10 +1,12 @@
// export class TraitVisiblePacket extends EntityPacket { import {Packet} from '@avocado/net';
// static get schema() { export class TraitVisiblePacket extends Packet {
// const schema = super.schema;
// schema.data.isVisible = 'bool';
// schema.data.opacity = 'uint8';
// return schema;
// }
// } static get schema() {
const schema = super.schema;
schema.data.isVisible = 'bool';
schema.data.opacity = 'uint8';
return schema;
}
}

View File

@ -33,7 +33,9 @@ export function SynchronizedMixin(Superclass) {
if (!packets) { if (!packets) {
return []; return [];
} }
packets = Array.isArray(packets) ? packets : [packets]; if (!Array.isArray(packets)) {
packets = [packets];
}
if (this.packetsAreIdempotent()) { if (this.packetsAreIdempotent()) {
this._idempotentPackets = packets; this._idempotentPackets = packets;
} }

View File

@ -1,10 +1,12 @@
// export class TraitAnimatedPacket extends EntityPacket { import {Packet} from '@avocado/net';
// static get schema() { export class TraitAnimatedPacket extends Packet {
// const schema = super.schema;
// schema.data.currentAnimation = 'string';
// schema.data.isAnimating = 'bool';
// return schema;
// }
// } static get schema() {
const schema = super.schema;
schema.data.currentAnimation = 'string';
schema.data.isAnimating = 'bool';
return schema;
}
}