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) {
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);

View File

@ -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,

View File

@ -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;
}
}

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 {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)) {

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}
}