flow
This commit is contained in:
parent
fae708aa24
commit
5e77b81b44
|
@ -1,12 +1,12 @@
|
|||
import {packetFromName, Packet} from '@latus/socket';
|
||||
import {packets, Packet} from '@latus/socket';
|
||||
|
||||
export default (latus) => class EntityListUpdateEntityPacket extends Packet {
|
||||
|
||||
static packData(data) {
|
||||
const {BundlePacket} = packetFromName(latus);
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data[i].packets = BundlePacket.bundle(data[i].packets);
|
||||
data[i].packets = Bundle.bundle(data[i].packets);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ export default (latus) => class EntityListUpdateEntityPacket extends Packet {
|
|||
}
|
||||
|
||||
static unpackData(data) {
|
||||
const {BundlePacket} = packetFromName(latus);
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data[i].packets = BundlePacket.unbundle(data[i].packets);
|
||||
data[i].packets = Bundle.unbundle(data[i].packets);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
||||
import {traits} from '@avocado/traits';
|
||||
import {packetFromName} from '@latus/socket';
|
||||
import {packets} from '@latus/socket';
|
||||
|
||||
export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpdatePacket {
|
||||
|
||||
static packData(data) {
|
||||
const {BundlePacket} = packetFromName(latus);
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
const {fromType} = traits(latus);
|
||||
for (let i = 0; i < data.traits.length; i++) {
|
||||
const Trait = fromType[data.traits[i].type];
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data.traits[i] = {
|
||||
type: Trait.id,
|
||||
packets: BundlePacket.bundle(data.traits[i].packets),
|
||||
packets: Bundle.bundle(data.traits[i].packets),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -29,14 +29,14 @@ export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpda
|
|||
}
|
||||
|
||||
static unpack(data) {
|
||||
const {BundlePacket} = packetFromName(latus);
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
const {fromId} = traits(latus);
|
||||
for (let i = 0; i < data.traits.length; i++) {
|
||||
const {default: Trait} = fromId[data.traits[i].type];
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data.traits[i] = {
|
||||
type: Trait.type(),
|
||||
packets: BundlePacket.unbundle(data.traits[i].packets),
|
||||
packets: Bundle.unbundle(data.traits[i].packets),
|
||||
};
|
||||
}
|
||||
return data;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import {compose, EventEmitter} from '@latus/core';
|
||||
import {QuadTree, Rectangle} from '@avocado/math';
|
||||
import {JsonResource, resource} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
|
@ -130,7 +129,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
|
|||
}
|
||||
// Still visible entity.
|
||||
else {
|
||||
const entityPackets = normalize(entity.packets(informed));
|
||||
const entityPackets = entity.packets(informed);
|
||||
if (entityPackets.length > 0) {
|
||||
updates.push({
|
||||
uuid: entity.instanceUuid,
|
||||
|
@ -144,7 +143,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
|
|||
this.#informedEntities.set(informed, visibleEntities);
|
||||
if (updates.length > 0) {
|
||||
packets.push([
|
||||
'EntityListUpdateEntityPacket',
|
||||
'EntityListUpdateEntity',
|
||||
updates,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import D from 'debug';
|
|||
import without from 'lodash.without';
|
||||
|
||||
import {fastApply, mergeDiff} from '@avocado/core';
|
||||
import {normalize, Synchronized} from '@avocado/s13n';
|
||||
import {Synchronized} from '@avocado/s13n';
|
||||
import {JsonResource} from '@avocado/resource';
|
||||
import {traits} from '@avocado/traits';
|
||||
import {compose, EventEmitter} from '@latus/core';
|
||||
|
@ -40,7 +40,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
|||
|
||||
constructor(json = {}) {
|
||||
super();
|
||||
const {instanceUuid, traits = []} = json;
|
||||
const {instanceUuid, traitInstances = []} = json;
|
||||
this.#originalJson = json;
|
||||
this.once('destroyed', () => {
|
||||
this.removeAllTraits();
|
||||
|
@ -53,7 +53,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
|||
this.position = [0, 0];
|
||||
this.room = null;
|
||||
this.visibleAabb = [0, 0, 0, 0];
|
||||
this.addTraits(traits);
|
||||
this.addTraits(traitInstances);
|
||||
}
|
||||
|
||||
acceptPacket(packet) {
|
||||
|
@ -171,7 +171,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
|||
}
|
||||
return [Trait, await Trait.extendJson(json)];
|
||||
});
|
||||
extended.traits = (await Promise.all(promises)).filter((entry) => !!entry);
|
||||
extended.traitInstances = (await Promise.all(promises)).filter((entry) => !!entry);
|
||||
}
|
||||
return extended;
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
|||
const traits = Object.entries(this.#traits);
|
||||
for (let i = 0; i < traits.length; i++) {
|
||||
const [type, trait] = traits[i];
|
||||
const traitPackets = normalize(latus, trait.packets(informed));
|
||||
const traitPackets = trait.packets(informed);
|
||||
if (traitPackets.length > 0) {
|
||||
updates.push({
|
||||
type,
|
||||
|
|
|
@ -79,10 +79,10 @@ export default () => class Directional extends decorate(Trait) {
|
|||
packets() {
|
||||
const {direction} = this.stateDifferences();
|
||||
if (direction) {
|
||||
return [
|
||||
return [[
|
||||
'TraitUpdateDirectionalDirection',
|
||||
direction.value,
|
||||
];
|
||||
]];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
@ -97,10 +97,10 @@ export default () => class Positioned extends decorate(Trait) {
|
|||
packets() {
|
||||
const {x, y} = this.stateDifferences();
|
||||
if (x || y) {
|
||||
return [
|
||||
return [[
|
||||
'TraitUpdatePositionedPosition',
|
||||
this.entity.position,
|
||||
];
|
||||
]];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import {
|
|||
buildInvoke,
|
||||
} from '@avocado/behavior';
|
||||
import {resource} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
import {Latus} from '@latus/core';
|
||||
import {normalize} from '@latus/socket';
|
||||
import {expect} from 'chai';
|
||||
|
||||
let latus;
|
||||
|
@ -108,11 +108,11 @@ describe('Alive', () => {
|
|||
it('generates and accepts life packets', async () => {
|
||||
entity.life = 80;
|
||||
entity.maxLife = 90;
|
||||
const packets = normalize(latus, entity.trait('Alive').packets());
|
||||
const packets = entity.trait('Alive').packets();
|
||||
expect(packets).to.have.lengthOf(1);
|
||||
expect(packets[0].constructor.type).to.equal('TraitUpdateAlive');
|
||||
expect(packets[0].data).to.deep.equal({life: 80, maxLife: 90});
|
||||
entity2.trait('Alive').acceptPacket(packets[0]);
|
||||
expect(packets[0][0]).to.equal('TraitUpdateAlive');
|
||||
expect(packets[0][1]).to.deep.equal({life: 80, maxLife: 90});
|
||||
entity2.trait('Alive').acceptPacket(normalize(latus, packets[0]));
|
||||
expect(entity2.life).to.equal(80);
|
||||
expect(entity2.maxLife).to.equal(90);
|
||||
});
|
||||
|
@ -120,16 +120,16 @@ describe('Alive', () => {
|
|||
it('generates and accepts death packets', async () => {
|
||||
entity.life = 0;
|
||||
entity.tick();
|
||||
const packets = normalize(latus, entity.trait('Alive').packets());
|
||||
const packets = entity.trait('Alive').packets();
|
||||
expect(packets).to.have.lengthOf(2);
|
||||
expect(packets[0].constructor.type).to.equal('TraitUpdateAlive');
|
||||
expect(packets[0].data).to.deep.equal({life: 0, maxLife: 100});
|
||||
expect(packets[1].constructor.type).to.equal('Died');
|
||||
expect(packets[0][0]).to.equal('TraitUpdateAlive');
|
||||
expect(packets[0][1]).to.deep.equal({life: 0, maxLife: 100});
|
||||
expect(packets[1][0]).to.equal('Died');
|
||||
expect(entity2.isDying).to.be.false;
|
||||
const promise = new Promise((resolve) => {
|
||||
entity2.on('isDyingChanged', resolve);
|
||||
});
|
||||
entity2.trait('Alive').acceptPacket(packets[1]);
|
||||
entity2.trait('Alive').acceptPacket(normalize(latus, packets[1]));
|
||||
return promise;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {resource} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
import {Latus} from '@latus/core';
|
||||
import {normalize} from '@latus/socket';
|
||||
import {expect} from 'chai';
|
||||
|
||||
let latus;
|
||||
|
@ -43,17 +43,17 @@ describe('Directional', () => {
|
|||
});
|
||||
it('generates and accepts direction packets', async () => {
|
||||
entity.direction = 2;
|
||||
const packets = normalize(latus, entity.trait('Directional').packets());
|
||||
const packets = entity.trait('Directional').packets();
|
||||
expect(packets).to.have.lengthOf(1);
|
||||
expect(packets[0].constructor.type).to.equal('TraitUpdateDirectionalDirection');
|
||||
expect(packets[0].data).to.equal(2);
|
||||
expect(packets[0][0]).to.equal('TraitUpdateDirectionalDirection');
|
||||
expect(packets[0][1]).to.equal(2);
|
||||
const entity2 = await Entity.load({
|
||||
traits: {
|
||||
Directional: {},
|
||||
},
|
||||
});
|
||||
expect(entity2.direction).to.equal(0);
|
||||
entity2.trait('Directional').acceptPacket(packets[0]);
|
||||
entity2.trait('Directional').acceptPacket(normalize(latus, packets[0]));
|
||||
expect(entity2.direction).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {resource} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
import {Latus} from '@latus/core';
|
||||
import {normalize} from '@latus/socket';
|
||||
import {expect} from 'chai';
|
||||
|
||||
let latus;
|
||||
|
@ -31,10 +31,10 @@ describe('Positioned', () => {
|
|||
if ('client' !== process.env.SIDE) {
|
||||
it('generates and accepts movement packets', async () => {
|
||||
entity.setPosition([1, 1]);
|
||||
const packets = normalize(latus, entity.trait('Positioned').packets());
|
||||
const packets = entity.trait('Positioned').packets();
|
||||
expect(packets).to.have.lengthOf(1);
|
||||
expect(packets[0].constructor.type).to.equal('TraitUpdatePositionedPosition');
|
||||
expect(packets[0].data).to.deep.equal([1, 1]);
|
||||
expect(packets[0][0]).to.equal('TraitUpdatePositionedPosition');
|
||||
expect(packets[0][1]).to.deep.equal([1, 1]);
|
||||
const entity2 = await Entity.load({
|
||||
traits: {
|
||||
Positioned: {},
|
||||
|
@ -42,7 +42,7 @@ describe('Positioned', () => {
|
|||
});
|
||||
expect(entity2.position).to.deep.equal([0, 0]);
|
||||
const trait = entity2.trait('Positioned');
|
||||
trait.acceptPacket(packets[0]);
|
||||
trait.acceptPacket(normalize(latus, packets[0]));
|
||||
expect(trait.serverPosition).to.deep.equal([1, 1]);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -22,6 +22,10 @@ export default (latus) => class Pictured extends decorate(Trait) {
|
|||
super(json);
|
||||
this._cachedAabbs = {};
|
||||
if (json.sprites) {
|
||||
Object.entries(json.sprites).forEach(([key, sprite]) => {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
sprite.position = this.offsetFor(key);
|
||||
});
|
||||
this.#sprites = json.sprites;
|
||||
}
|
||||
}
|
||||
|
@ -71,17 +75,11 @@ export default (latus) => class Pictured extends decorate(Trait) {
|
|||
|
||||
static async extendJson(json) {
|
||||
const extended = await super.extendJson(json);
|
||||
if (Object.keys(this.params.images).length > 0) {
|
||||
if (Object.keys(json.params.images).length > 0) {
|
||||
const {fromResourceType: {Image}} = resource(latus);
|
||||
extended.sprites = await mapValuesAsync(
|
||||
this.params.images,
|
||||
async (json, key) => {
|
||||
const image = await Image.load(json);
|
||||
const sprite = new Sprite(image);
|
||||
// Calculate any offset.
|
||||
sprite.position = this.offsetFor(key);
|
||||
return sprite;
|
||||
},
|
||||
json.params.images,
|
||||
async (json) => new Sprite(await Image.load(json)),
|
||||
);
|
||||
}
|
||||
return extended;
|
||||
|
@ -133,7 +131,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
|
|||
currentImageChanged: (oldKey, currentImage) => {
|
||||
this.#currentImage = currentImage;
|
||||
// Bounding box update.
|
||||
this.entity.updateVisibleBoundingBox();
|
||||
this.entity.emit('updateVisibleBoundingBox');
|
||||
// Only client/graphics.
|
||||
if (!this.#sprites) {
|
||||
return;
|
||||
|
@ -148,7 +146,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
|
|||
},
|
||||
|
||||
traitAdded: () => {
|
||||
this.entity.updateVisibleBoundingBox();
|
||||
this.entity.emit('updateVisibleBoundingBox');
|
||||
this.setSpriteScale();
|
||||
Object.keys(this.#sprites).forEach((key) => {
|
||||
const method = key === this.#currentImage
|
||||
|
|
|
@ -92,8 +92,8 @@ export default () => class Visible extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
constructor(entity, params, state) {
|
||||
super(entity, params, state);
|
||||
constructor(json) {
|
||||
super(json);
|
||||
if ('client' === process.env.SIDE) {
|
||||
this._container = new Container();
|
||||
const {filter} = this.params;
|
||||
|
@ -139,13 +139,13 @@ export default () => class Visible extends decorate(Trait) {
|
|||
packets() {
|
||||
const {isVisible, opacity} = this.stateDifferences();
|
||||
if (isVisible || opacity) {
|
||||
return [
|
||||
'TraitUpdateVisiblePacket',
|
||||
return [[
|
||||
'TraitUpdateVisible',
|
||||
{
|
||||
isVisible: this.state.isVisible,
|
||||
opacity: this.state.opacity,
|
||||
},
|
||||
];
|
||||
]];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
@ -227,6 +227,10 @@ export default () => class Visible extends decorate(Trait) {
|
|||
this.synchronizePosition();
|
||||
},
|
||||
|
||||
updateVisibleBoundingBox: () => {
|
||||
this.scheduledBoundingBoxUpdate = true;
|
||||
},
|
||||
|
||||
visibleScaleChanged: () => {
|
||||
const scale = this.entity.visibleScale;
|
||||
this._visibleScale = [scale[0], scale[1]];
|
||||
|
@ -242,16 +246,6 @@ export default () => class Visible extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
methods() {
|
||||
return {
|
||||
|
||||
updateVisibleBoundingBox: () => {
|
||||
this.scheduledBoundingBoxUpdate = true;
|
||||
},
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
renderTick() {
|
||||
this.synchronizePosition();
|
||||
}
|
||||
|
@ -271,7 +265,7 @@ export default () => class Visible extends decorate(Trait) {
|
|||
}
|
||||
|
||||
tick() {
|
||||
if (AVOCADO_SERVER) {
|
||||
if ('client' !== process.env.SIDE) {
|
||||
if (this.scheduledBoundingBoxUpdate) {
|
||||
// Collect all bounding boxes.
|
||||
const visibleAabbs = this.entity.invokeHookFlat('visibleAabbs');
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {Actions, compile, Context} from '@avocado/behavior';
|
||||
import {StateProperty, Trait} from '@avocado/entity';
|
||||
import {Rectangle, Vector} from '@avocado/math';
|
||||
import {StateProperty, Trait} from '@avocado/traits';
|
||||
import {compose} from '@latus/core';
|
||||
|
||||
const decorate = compose(
|
||||
|
@ -315,7 +315,7 @@ export default () => class Collider extends decorate(Trait) {
|
|||
}
|
||||
|
||||
tick() {
|
||||
if (AVOCADO_SERVER) {
|
||||
if ('client' !== process.env.SIDE) {
|
||||
this.checkActiveCollision();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {StateProperty} from '@avocado/entity';
|
||||
import {Vector} from '@avocado/math';
|
||||
import {Trait} from '@avocado/traits';
|
||||
import {StateProperty, Trait} from '@avocado/traits';
|
||||
import {compose} from '@latus/core';
|
||||
|
||||
import BodyView from '../body-view';
|
||||
|
@ -28,7 +27,7 @@ export default () => class Physical extends decorate(Trait) {
|
|||
body.setCollisionForEntity(this.entity);
|
||||
world.addBody(body);
|
||||
this._body = body;
|
||||
if (AVOCADO_CLIENT) {
|
||||
if ('client' === process.env.SIDE) {
|
||||
if (this.entity.is('visible') && this.entity.is('debuggable')) {
|
||||
this.bodyView = new BodyView(body);
|
||||
this.bodyView.position = Vector.scale(this.entity.position, -1);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {compose} from '@avocado/core';
|
||||
import {Trait} from '@avocado/entity';
|
||||
import {Trait} from '@avocado/traits';
|
||||
import {compose} from '@latus/core';
|
||||
|
||||
import shapeFromJSON from '../shape/from-json';
|
||||
import ShapeView from '../shape/view';
|
||||
|
@ -46,7 +46,7 @@ export default () => class Shaped extends decorate(Trait) {
|
|||
return {
|
||||
|
||||
traitAdded: () => {
|
||||
if (AVOCADO_CLIENT) {
|
||||
if ('client' === process.env.SIDE) {
|
||||
if (this.entity.is('visible') && this.entity.is('debuggable')) {
|
||||
if (!this.#shapeView) {
|
||||
this.#shapeView = new ShapeView(this.entity.shape);
|
||||
|
|
|
@ -5,7 +5,7 @@ import Resource from './resource';
|
|||
export default class JsonResource extends Resource {
|
||||
|
||||
static async extendJson(json) {
|
||||
if (json.extends) {
|
||||
if ('string' === typeof json.extends) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
json.extends = await this.read(json.extends);
|
||||
}
|
||||
|
|
|
@ -26,21 +26,26 @@ export default class Resource extends decorate(Class) {
|
|||
}
|
||||
|
||||
static async read(uri) {
|
||||
if ('client' === process.env.SIDE) {
|
||||
const response = await fetch(join('/', this.root, uri));
|
||||
return response.arrayBuffer();
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
// eslint-disable-next-line global-require
|
||||
const fs = require('fs');
|
||||
fs.readFile(join(process.cwd(), this.root, uri), (error, buffer) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve(buffer);
|
||||
try {
|
||||
if ('client' === process.env.SIDE) {
|
||||
const response = await fetch(join('/', this.root, uri));
|
||||
return response.arrayBuffer();
|
||||
}
|
||||
return await new Promise((resolve, reject) => {
|
||||
// eslint-disable-next-line global-require
|
||||
const fs = require('fs');
|
||||
fs.readFile(join(process.cwd(), this.root, uri), (error, buffer) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve(buffer);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
throw new Error(`Error reading ${JSON.stringify(uri)}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
static root = 'resource';
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
import {gatherWithLatus} from '@latus/core';
|
||||
import {
|
||||
SynchronizedCreatePacket,
|
||||
SynchronizedDestroyPacket,
|
||||
SynchronizedUpdatePacket,
|
||||
} from './packets';
|
||||
|
||||
export * from './packets';
|
||||
|
||||
export {default as normalize} from './normalize';
|
||||
export {default as ReceiverSynchronizer} from './receiver-synchronizer';
|
||||
export {default as SenderSynchronizer} from './sender-synchronizer';
|
||||
export {default as Synchronized, synchronized} from './synchronized';
|
||||
|
||||
export default {
|
||||
hooks: {
|
||||
'@latus/socket/packets': gatherWithLatus(
|
||||
require.context('./packets', false, /\.js$/),
|
||||
),
|
||||
'@latus/socket/packets': () => ({
|
||||
SynchronizedCreate: SynchronizedCreatePacket,
|
||||
SynchronizedDestroy: SynchronizedDestroyPacket,
|
||||
SynchronizedUpdate: SynchronizedUpdatePacket,
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
import {normalize, Packet} from '@latus/socket';
|
||||
|
||||
export default (latus, packets) => {
|
||||
if (!packets) {
|
||||
return [];
|
||||
}
|
||||
// Packet
|
||||
if (!Array.isArray(packets)) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
packets = [packets];
|
||||
}
|
||||
if (
|
||||
// [Packet, ...]
|
||||
!(packets[0] instanceof Packet)
|
||||
// [name, data]
|
||||
&& !Array.isArray(packets[0])
|
||||
) {
|
||||
// [[name, data...], ...]
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
packets = [packets];
|
||||
}
|
||||
return packets.map((packet) => normalize(latus, packet));
|
||||
};
|
|
@ -12,6 +12,10 @@ export default class SenderSynchronizer {
|
|||
|
||||
#synchronizedFlat = [];
|
||||
|
||||
constructor(latus) {
|
||||
this.latus = latus;
|
||||
}
|
||||
|
||||
addSynchronized(synchronized) {
|
||||
if (this.hasSynchronized(synchronized)) {
|
||||
return;
|
||||
|
@ -46,7 +50,7 @@ export default class SenderSynchronizer {
|
|||
payload.push(synchronized.destroyPacket(informed));
|
||||
}
|
||||
else {
|
||||
const packets = synchronized.packetsFor(informed);
|
||||
const packets = synchronized.packetsFor(this.latus, informed);
|
||||
for (let j = 0; j < packets.length; j++) {
|
||||
payload.push(packets[j]);
|
||||
}
|
||||
|
@ -79,7 +83,7 @@ export default class SenderSynchronizer {
|
|||
}
|
||||
this.#queuedPackets = [];
|
||||
if (socket && this.#nextSyncPackets.length > 0) {
|
||||
socket.send(['BundlePacket', this.#nextSyncPackets]);
|
||||
socket.send(['Bundle', this.#nextSyncPackets]);
|
||||
this.#nextSyncPackets = [];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
import normalize from './normalize';
|
||||
import SynchronizedCreatePacket from './packets/synchronized-create';
|
||||
import SynchronizedDestroyPacket from './packets/synchronized-destroy';
|
||||
|
||||
export const synchronized = ({config}) => config['%synchronized'];
|
||||
|
||||
export default function SynchronizedMixin(Superclass) {
|
||||
|
@ -19,13 +15,16 @@ export default function SynchronizedMixin(Superclass) {
|
|||
|
||||
createPacket(informed) {
|
||||
const id = this.s13Id();
|
||||
return new SynchronizedCreatePacket({
|
||||
synchronized: {
|
||||
id,
|
||||
type: this.constructor.resourceId,
|
||||
return [
|
||||
'SynchronizedCreate',
|
||||
{
|
||||
synchronized: {
|
||||
id,
|
||||
type: this.constructor.resourceId,
|
||||
},
|
||||
spec: this.toNetwork(informed),
|
||||
},
|
||||
spec: this.toNetwork(informed),
|
||||
});
|
||||
];
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
|
@ -33,12 +32,15 @@ export default function SynchronizedMixin(Superclass) {
|
|||
|
||||
destroyPacket() {
|
||||
const id = this.s13Id();
|
||||
return new SynchronizedDestroyPacket({
|
||||
synchronized: {
|
||||
id,
|
||||
type: this.constructor.resourceId,
|
||||
return [
|
||||
'SynchronizedDestroy',
|
||||
{
|
||||
synchronized: {
|
||||
id,
|
||||
type: this.constructor.resourceId,
|
||||
},
|
||||
},
|
||||
});
|
||||
];
|
||||
}
|
||||
|
||||
fromNetwork() {
|
||||
|
@ -61,11 +63,11 @@ export default function SynchronizedMixin(Superclass) {
|
|||
if (this._idempotentPackets.length > 0) {
|
||||
return this._idempotentPackets;
|
||||
}
|
||||
const packets = normalize(latus, this.packets(informed));
|
||||
const packets = this.packets(informed);
|
||||
// Embed synchronization info.
|
||||
const id = this.s13Id();
|
||||
for (let i = 0; i < packets.length; i++) {
|
||||
packets[i].data.synchronized = {
|
||||
packets[i][1].synchronized = {
|
||||
id,
|
||||
type: this.constructor.resourceId,
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {Property} from '@avocado/core';
|
||||
import {Rectangle, Vector} from '@avocado/math';
|
||||
import {Resource} from '@avocado/resource';
|
||||
import {JsonResource} from '@avocado/resource';
|
||||
import {compose, EventEmitter} from '@latus/core';
|
||||
|
||||
import TimedIndex from '../timed-index';
|
||||
|
@ -20,13 +20,25 @@ const decorate = compose(
|
|||
}),
|
||||
);
|
||||
|
||||
export default () => class Animation extends decorate(Resource) {
|
||||
export default () => class Animation extends decorate(JsonResource) {
|
||||
|
||||
constructor(json) {
|
||||
constructor(json = {}) {
|
||||
super();
|
||||
this.imageUri = undefined;
|
||||
if ('undefined' !== typeof json) {
|
||||
this.fromJSON(json);
|
||||
if (json.frameRate) {
|
||||
this.ticker.frequency = json.frameRate;
|
||||
}
|
||||
const keys = [
|
||||
'directionCount',
|
||||
'frameCount',
|
||||
'frameSize',
|
||||
'imageUri',
|
||||
];
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
if (json[key]) {
|
||||
this[key] = json[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,24 +95,4 @@ export default () => class Animation extends decorate(Resource) {
|
|||
this.currentFrame = 0;
|
||||
}
|
||||
|
||||
fromJSON(json) {
|
||||
super.fromJSON(json);
|
||||
if (json.frameRate) {
|
||||
this.ticker.frequency = json.frameRate;
|
||||
}
|
||||
const keys = [
|
||||
'directionCount',
|
||||
'frameCount',
|
||||
'frameSize',
|
||||
'imageUri',
|
||||
];
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
if (json[key]) {
|
||||
this[key] = json[key];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -33,6 +33,10 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
// eslint-disable-next-line no-param-reassign
|
||||
animation.direction = this.entity.direction;
|
||||
});
|
||||
Object.entries(json.animationViews).forEach(([key, animationView]) => {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
animationView.position = this.offsetFor(key);
|
||||
});
|
||||
this.#animations = json.animations;
|
||||
this.#animationViews = json.animationViews;
|
||||
}
|
||||
|
@ -105,19 +109,15 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
|
||||
static async extendJson(json) {
|
||||
const extended = await super.extendJson(json);
|
||||
if (Object.keys(this.params.animations).length > 0) {
|
||||
if (Object.keys(json.params.animations).length > 0) {
|
||||
const {fromResourceType: {Animation}} = resource(latus);
|
||||
extended.animations = await mapValuesAsync(
|
||||
this.params.animations,
|
||||
json.params.animations,
|
||||
(json) => Animation.load(json),
|
||||
);
|
||||
extended.animationViews = mapValues(
|
||||
extended.animations,
|
||||
(animation, key) => {
|
||||
const animationView = new AnimationView(animation);
|
||||
animationView.position = this.offsetFor(key);
|
||||
return animationView;
|
||||
},
|
||||
(animation) => new AnimationView(animation),
|
||||
);
|
||||
}
|
||||
return extended;
|
||||
|
@ -184,7 +184,7 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
oldAnimation.reset();
|
||||
}
|
||||
// Bounding box update.
|
||||
this.entity.updateVisibleBoundingBox();
|
||||
this.entity.emit('updateVisibleBoundingBox');
|
||||
// Only client/graphics.
|
||||
if (!this.#animationViews) {
|
||||
return;
|
||||
|
@ -207,7 +207,7 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
},
|
||||
|
||||
traitAdded: () => {
|
||||
this.entity.updateVisibleBoundingBox();
|
||||
this.entity.emit('updateVisibleBoundingBox');
|
||||
this.setSpriteScale();
|
||||
Object.keys(this.#animations).forEach((key) => {
|
||||
const method = key === this.#currentAnimation
|
||||
|
@ -235,13 +235,13 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
packets() {
|
||||
const {currentAnimation, isAnimating} = this.stateDifferences();
|
||||
if (currentAnimation || isAnimating) {
|
||||
return [
|
||||
return [[
|
||||
'TraitUpdateAnimated',
|
||||
{
|
||||
currentAnimation: this.state.currentAnimation,
|
||||
isAnimating: this.state.isAnimating,
|
||||
},
|
||||
];
|
||||
]];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
@ -250,6 +250,9 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
if (!this.#animationViews) {
|
||||
return;
|
||||
}
|
||||
if (!this.entity.is('Visible')) {
|
||||
return;
|
||||
}
|
||||
const animationViews = Object.values(this.#animationViews);
|
||||
for (let i = 0; i < animationViews.length; i++) {
|
||||
animationViews[i].scale = this.entity.rawVisibleScale;
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import {BundlePacket, Packet} from '@latus/socket';
|
||||
import {packets, Packet} from '@latus/socket';
|
||||
|
||||
export default () => class LayersUpdateLayerPacket extends Packet {
|
||||
export default (latus) => class LayersUpdateLayerPacket extends Packet {
|
||||
|
||||
static packData(data) {
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data[i].layerPackets = BundlePacket.bundle(data[i].layerPackets);
|
||||
data[i].layerPackets = Bundle.bundle(data[i].layerPackets);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -20,9 +21,10 @@ export default () => class LayersUpdateLayerPacket extends Packet {
|
|||
}
|
||||
|
||||
static unpackData(data) {
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data[i].layerPackets = BundlePacket.unbundle(data[i].layerPackets);
|
||||
data[i].layerPackets = Bundle.unbundle(data[i].layerPackets);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import {BundlePacket} from '@latus/socket';
|
||||
import {packets} from '@latus/socket';
|
||||
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
||||
|
||||
export default () => class RoomUpdateLayers extends SynchronizedUpdatePacket {
|
||||
export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacket {
|
||||
|
||||
static packData(data) {
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data.layersPackets = BundlePacket.bundle(data.layerPackets);
|
||||
data.layersPackets = Bundle.bundle(data.layerPackets);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -16,8 +17,9 @@ export default () => class RoomUpdateLayers extends SynchronizedUpdatePacket {
|
|||
}
|
||||
|
||||
static unpackData(data) {
|
||||
const {fromType: {Bundle}} = packets(latus);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
data.layersPackets = BundlePacket.unbundle(data.layersPackets);
|
||||
data.layersPackets = Bundle.unbundle(data.layersPackets);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {Property} from '@avocado/core';
|
||||
import {JsonResource, resource} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
import {compose, EventEmitter} from '@latus/core';
|
||||
|
||||
// import Tiles from './tiles';
|
||||
|
@ -194,11 +193,11 @@ export default (latus) => class Layer extends decorate(JsonResource) {
|
|||
|
||||
packets(informed) {
|
||||
const packets = [];
|
||||
const entityListPackets = normalize(this.entityList.packets(informed));
|
||||
const entityListPackets = this.entityList.packets(informed);
|
||||
for (let i = 0; i < entityListPackets.length; i++) {
|
||||
packets.push(entityListPackets[i]);
|
||||
}
|
||||
const tilesPackets = normalize(this.tiles.packets(informed));
|
||||
const tilesPackets = this.tiles.packets(informed);
|
||||
for (let i = 0; i < tilesPackets.length; i++) {
|
||||
packets.push(tilesPackets[i]);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ import {
|
|||
JsonResource,
|
||||
resource,
|
||||
} from '@avocado/resource';
|
||||
import {normalize} from '@avocado/s13n';
|
||||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
|
@ -107,7 +106,7 @@ export default (latus) => class Layers extends decorate(JsonResource) {
|
|||
const packets = [];
|
||||
const updates = [];
|
||||
for (let i = 0; i < this.layers.length; i++) {
|
||||
const layerPackets = normalize(this.layers[i].packets(informed));
|
||||
const layerPackets = this.layers[i].packets(informed);
|
||||
if (layerPackets.length > 0) {
|
||||
updates.push({
|
||||
layerIndex: i,
|
||||
|
@ -117,7 +116,7 @@ export default (latus) => class Layers extends decorate(JsonResource) {
|
|||
}
|
||||
if (updates.length > 0) {
|
||||
packets.push([
|
||||
'LayersUpdateLayerPacket',
|
||||
'LayersUpdateLayer',
|
||||
updates,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import {
|
|||
JsonResource,
|
||||
resource,
|
||||
} from '@avocado/resource';
|
||||
import {normalize, Synchronized} from '@avocado/s13n';
|
||||
import {Synchronized} from '@avocado/s13n';
|
||||
import {compose, EventEmitter} from '@latus/core';
|
||||
|
||||
// import Layers from './layers';
|
||||
|
@ -119,7 +119,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
|
|||
packets(informed) {
|
||||
const payload = [];
|
||||
// Layer updates.
|
||||
const layersPackets = normalize(this.layers.packets(informed));
|
||||
const layersPackets = this.layers.packets(informed);
|
||||
if (layersPackets.length > 0) {
|
||||
payload.push([
|
||||
'RoomUpdateLayers',
|
||||
|
@ -152,6 +152,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
|
|||
}
|
||||
|
||||
toNetwork(informed) {
|
||||
console.log(this);
|
||||
return {
|
||||
layers: this.layers.toNetwork(informed),
|
||||
size: this.size,
|
||||
|
|
|
@ -67,7 +67,7 @@ export default () => class Tiles extends decorate(Class) {
|
|||
}
|
||||
this.data[index] = tile;
|
||||
this._packets.push([
|
||||
'TilesUpdatePacket',
|
||||
'TilesUpdate',
|
||||
{
|
||||
position,
|
||||
tile,
|
||||
|
|
Loading…
Reference in New Issue
Block a user