refactor: gather
This commit is contained in:
parent
6eb66403cf
commit
d13a48365f
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -21,7 +20,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity}} = resource(latus));
|
({Entity} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Behaved', () => {
|
describe('Behaved', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import {packets, Packet} from '@latus/socket';
|
import {Packet} from '@latus/socket';
|
||||||
|
|
||||||
export default (latus) => class EntityListUpdateEntityPacket extends Packet {
|
export default (latus) => class EntityListUpdateEntityPacket extends Packet {
|
||||||
|
|
||||||
static pack(data) {
|
static pack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data[i].packets = Bundle.encode(data[i].packets);
|
data[i].packets = Bundle.encode(data[i].packets);
|
||||||
|
@ -21,7 +21,7 @@ export default (latus) => class EntityListUpdateEntityPacket extends Packet {
|
||||||
}
|
}
|
||||||
|
|
||||||
static unpack(data) {
|
static unpack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data[i].packets = Bundle.decode(data[i].packets);
|
data[i].packets = Bundle.decode(data[i].packets);
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
||||||
import {traits} from '@avocado/traits';
|
|
||||||
import {packets} from '@latus/socket';
|
|
||||||
|
|
||||||
export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpdatePacket {
|
export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpdatePacket {
|
||||||
|
|
||||||
static pack(data) {
|
static pack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
const {fromType} = traits(latus);
|
const Traits = latus.get('%traits');
|
||||||
for (let i = 0; i < data.traits.length; i++) {
|
for (let i = 0; i < data.traits.length; i++) {
|
||||||
const Trait = fromType[data.traits[i].type];
|
const {[data.traits[i].type]: Trait} = Traits;
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data.traits[i] = {
|
data.traits[i] = {
|
||||||
type: Trait.id,
|
type: Trait.id,
|
||||||
|
@ -30,10 +28,10 @@ export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpda
|
||||||
}
|
}
|
||||||
|
|
||||||
static unpack(data) {
|
static unpack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
const {fromId} = traits(latus);
|
const Traits = latus.get('%traits');
|
||||||
for (let i = 0; i < data.traits.length; i++) {
|
for (let i = 0; i < data.traits.length; i++) {
|
||||||
const Trait = fromId[data.traits[i].type];
|
const {[data.traits[i].type]: Trait} = Traits;
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data.traits[i] = {
|
data.traits[i] = {
|
||||||
type: Trait.type,
|
type: Trait.type,
|
||||||
|
|
|
@ -45,7 +45,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
|
||||||
switch (s13nType) {
|
switch (s13nType) {
|
||||||
case 'create': {
|
case 'create': {
|
||||||
const uuid = packet.data.synchronized.id;
|
const uuid = packet.data.synchronized.id;
|
||||||
const {Entity} = latus.get('%resources.fromResourceType');
|
const {Entity} = latus.get('%resources');
|
||||||
this.#serializer.create(uuid, Entity.load(packet.data.spec));
|
this.#serializer.create(uuid, Entity.load(packet.data.spec));
|
||||||
this.#serializer.later(uuid, (entity) => {
|
this.#serializer.later(uuid, (entity) => {
|
||||||
this.addEntity(entity);
|
this.addEntity(entity);
|
||||||
|
@ -105,7 +105,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
|
||||||
|
|
||||||
async load(json = []) {
|
async load(json = []) {
|
||||||
await super.load(json);
|
await super.load(json);
|
||||||
const {Entity} = latus.get('%resources.fromResourceType');
|
const {Entity} = latus.get('%resources');
|
||||||
const entityInstances = await Promise.all(json.map((entity) => Entity.load(entity)));
|
const entityInstances = await Promise.all(json.map((entity) => Entity.load(entity)));
|
||||||
for (let i = 0; i < entityInstances.length; i++) {
|
for (let i = 0; i < entityInstances.length; i++) {
|
||||||
this.addEntity(entityInstances[i]);
|
this.addEntity(entityInstances[i]);
|
||||||
|
|
|
@ -4,7 +4,6 @@ import without from 'lodash.without';
|
||||||
import {fastApply, mergeDiff} from '@avocado/core';
|
import {fastApply, mergeDiff} from '@avocado/core';
|
||||||
import {Synchronized} from '@avocado/s13n';
|
import {Synchronized} from '@avocado/s13n';
|
||||||
import {JsonResource} from '@avocado/resource';
|
import {JsonResource} from '@avocado/resource';
|
||||||
import {traits} from '@avocado/traits';
|
|
||||||
import {compose, EventEmitter} from '@latus/core';
|
import {compose, EventEmitter} from '@latus/core';
|
||||||
|
|
||||||
import {defineTraitAccessors, enumerateTraitAccessorKeys} from '../accessors';
|
import {defineTraitAccessors, enumerateTraitAccessorKeys} from '../accessors';
|
||||||
|
@ -76,7 +75,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async addTrait(type, json = {}) {
|
async addTrait(type, json = {}) {
|
||||||
const {fromType: {[type]: Trait}} = traits(latus);
|
const {[type]: Trait} = latus.get('%traits');
|
||||||
if (!Trait) {
|
if (!Trait) {
|
||||||
debug(`Tried to add trait "${type}" which isn't registered!`);
|
debug(`Tried to add trait "${type}" which isn't registered!`);
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -202,14 +201,14 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
||||||
if (!this.uri || !this.#originalJson || 0 === Object.keys(this.#originalJson).length) {
|
if (!this.uri || !this.#originalJson || 0 === Object.keys(this.#originalJson).length) {
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
const {fromType} = latus.get('%traits');
|
const Traits = latus.get('%traits');
|
||||||
const traits = Object.fromEntries(
|
const traits = Object.fromEntries(
|
||||||
Object.entries(this.#originalJson.traits)
|
Object.entries(this.#originalJson.traits)
|
||||||
.map(([type, json]) => fromType[type] && [
|
.map(([type, json]) => Traits[type] && [
|
||||||
type,
|
type,
|
||||||
{
|
{
|
||||||
params: fromType[type].defaultParamsWith(json.params),
|
params: Traits[type].defaultParamsWith(json.params),
|
||||||
state: fromType[type].defaultStateWith(json.state),
|
state: Traits[type].defaultStateWith(json.state),
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
.filter((e) => !!e),
|
.filter((e) => !!e),
|
||||||
|
@ -277,7 +276,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
|
||||||
const implementation = this.#hooks[hook].find(({type: hookType}) => hookType === type);
|
const implementation = this.#hooks[hook].find(({type: hookType}) => hookType === type);
|
||||||
this.#hooks[hook].splice(this.#hooks[hook].indexOf(implementation), 1);
|
this.#hooks[hook].splice(this.#hooks[hook].indexOf(implementation), 1);
|
||||||
}
|
}
|
||||||
const {fromType: {[type]: Trait}} = traits(latus);
|
const {[type]: Trait} = latus.get('%traits');
|
||||||
const properties = enumerateTraitAccessorKeys(Trait.prototype);
|
const properties = enumerateTraitAccessorKeys(Trait.prototype);
|
||||||
for (let i = 0; i < properties.length; ++i) {
|
for (let i = 0; i < properties.length; ++i) {
|
||||||
const property = properties[i];
|
const property = properties[i];
|
||||||
|
|
|
@ -265,7 +265,7 @@ export default (latus) => class Spawner extends decorate(Trait) {
|
||||||
const childIndex = this.#children.length;
|
const childIndex = this.#children.length;
|
||||||
this.#children.push(null);
|
this.#children.push(null);
|
||||||
const list = this.destinationEntityList();
|
const list = this.destinationEntityList();
|
||||||
const {Entity} = latus.get('%resources.fromResourceType');
|
const {Entity} = latus.get('%resources');
|
||||||
const child = await Entity.load(json);
|
const child = await Entity.load(json);
|
||||||
this.#children[childIndex] = child;
|
this.#children[childIndex] = child;
|
||||||
// Listen for destroy event.
|
// Listen for destroy event.
|
||||||
|
|
|
@ -3,7 +3,6 @@ import {
|
||||||
buildExpression,
|
buildExpression,
|
||||||
buildInvoke,
|
buildInvoke,
|
||||||
} from '@avocado/behavior';
|
} from '@avocado/behavior';
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {normalize} from '@latus/socket';
|
import {normalize} from '@latus/socket';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
@ -19,7 +18,7 @@ beforeEach(async () => {
|
||||||
'@latus/socket': require('@latus/socket'),
|
'@latus/socket': require('@latus/socket'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity}} = resource(latus));
|
({Entity} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Alive', () => {
|
describe('Alive', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {normalize} from '@latus/socket';
|
import {normalize} from '@latus/socket';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
@ -13,7 +12,7 @@ beforeEach(async () => {
|
||||||
'@latus/socket': require('@latus/socket'),
|
'@latus/socket': require('@latus/socket'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity}} = resource(latus));
|
({Entity} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Directional', () => {
|
describe('Directional', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Trait, traits} from '@avocado/traits';
|
import {Trait, traits} from '@avocado/traits';
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
@ -12,7 +11,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity}} = resource(latus));
|
({Entity} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
it('has sane defaults', () => {
|
it('has sane defaults', () => {
|
||||||
const entity = new Entity();
|
const entity = new Entity();
|
||||||
|
@ -28,7 +27,7 @@ it('can add and remove traits', async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
traits(latus).fromType['TestTrait'] = TestTrait;
|
latus.set('%traits.TestTrait', TestTrait);
|
||||||
await entity.addTrait('TestTrait');
|
await entity.addTrait('TestTrait');
|
||||||
expect(entity.is('TestTrait')).to.be.true;
|
expect(entity.is('TestTrait')).to.be.true;
|
||||||
entity.removeTrait('TestTrait');
|
entity.removeTrait('TestTrait');
|
||||||
|
@ -45,7 +44,7 @@ it('can add traits asynchronously', async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
traits(latus).fromType['Async'] = AsyncTrait;
|
latus.set('%traits.Async', AsyncTrait);
|
||||||
let start = Date.now();
|
let start = Date.now();
|
||||||
const entity = await Entity.load({
|
const entity = await Entity.load({
|
||||||
traits: {
|
traits: {
|
||||||
|
@ -85,8 +84,8 @@ it('can invoke hooks', async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
latus.set('%traits.fromType.AnotherTrait', AnotherTrait);
|
latus.set('%traits.AnotherTrait', AnotherTrait);
|
||||||
latus.set('%traits.fromType.YetAnotherTrait', YetAnotherTrait);
|
latus.set('%traits.YetAnotherTrait', YetAnotherTrait);
|
||||||
const entity = new Entity();
|
const entity = new Entity();
|
||||||
await entity.addTrait('AnotherTrait');
|
await entity.addTrait('AnotherTrait');
|
||||||
await entity.addTrait('YetAnotherTrait');
|
await entity.addTrait('YetAnotherTrait');
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Trait} from '@avocado/traits';
|
import {Trait} from '@avocado/traits';
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
@ -12,7 +11,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity}} = resource(latus));
|
({Entity} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Existent', () => {
|
describe('Existent', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
@ -74,7 +73,7 @@ describe('Existent', () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
latus.get('%traits.fromType')['Waiter'] = Waiter;
|
latus.set('%traits.Waiter', Waiter);
|
||||||
await entity.addTrait('Waiter');
|
await entity.addTrait('Waiter');
|
||||||
const promise = Promise.all([
|
const promise = Promise.all([
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity, EntityList}} = resource(latus));
|
({Entity, EntityList} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Listed', () => {
|
describe('Listed', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity, EntityList}} = resource(latus));
|
({Entity, EntityList} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Mobile', () => {
|
describe('Mobile', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity, EntityList}} = resource(latus));
|
({Entity, EntityList} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Perishable', () => {
|
describe('Perishable', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {normalize} from '@latus/socket';
|
import {normalize} from '@latus/socket';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
@ -14,7 +13,7 @@ beforeEach(async () => {
|
||||||
'@latus/socket': require('@latus/socket'),
|
'@latus/socket': require('@latus/socket'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity, EntityList}} = resource(latus));
|
({Entity, EntityList} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Positioned', () => {
|
describe('Positioned', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -13,7 +12,7 @@ beforeEach(async () => {
|
||||||
'@avocado/traits': require('@avocado/traits'),
|
'@avocado/traits': require('@avocado/traits'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
({fromResourceType: {Entity, EntityList}} = resource(latus));
|
({Entity, EntityList} = latus.get('%resources'));
|
||||||
});
|
});
|
||||||
describe('Spawner', () => {
|
describe('Spawner', () => {
|
||||||
let entity;
|
let entity;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import {mapValuesAsync} from '@avocado/core';
|
import {mapValuesAsync} from '@avocado/core';
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {StateProperty, Trait} from '@avocado/traits';
|
import {StateProperty, Trait} from '@avocado/traits';
|
||||||
import {Rectangle, Vector} from '@avocado/math';
|
import {Rectangle, Vector} from '@avocado/math';
|
||||||
import {compose} from '@latus/core';
|
import {compose} from '@latus/core';
|
||||||
|
@ -149,7 +148,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
|
||||||
await super.load(json);
|
await super.load(json);
|
||||||
this.#currentImage = this.state.currentImage;
|
this.#currentImage = this.state.currentImage;
|
||||||
if (Object.keys(this.params.images).length > 0) {
|
if (Object.keys(this.params.images).length > 0) {
|
||||||
const {fromResourceType: {Image}} = resource(latus);
|
const {Image} = latus.get('%resources');
|
||||||
const sprites = await mapValuesAsync(
|
const sprites = await mapValuesAsync(
|
||||||
this.params.images,
|
this.params.images,
|
||||||
async (json) => new Sprite(await Image.load(json.uri)),
|
async (json) => new Sprite(await Image.load(json.uri)),
|
||||||
|
|
|
@ -92,7 +92,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async emitParticleJson(json) {
|
async emitParticleJson(json) {
|
||||||
const {Entity} = latus.get('%resources.fromResourceType');
|
const {Entity} = latus.get('%resources');
|
||||||
return this.entity.emitParticleEntity(await Entity.load(json));
|
return this.entity.emitParticleEntity(await Entity.load(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
import {gather} from '@latus/core';
|
import {gather} from '@latus/core';
|
||||||
|
|
||||||
export {default as Resource, resource} from './resource';
|
export {default as Resource} from './resource';
|
||||||
export {default as JsonResource} from './json-resource';
|
export {default as JsonResource} from './json-resource';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
hooks: {
|
||||||
'@latus/core/starting': (latus) => {
|
'@latus/core/starting': (latus) => {
|
||||||
// eslint-disable-next-line no-param-reassign
|
latus.set('%resources', gather(
|
||||||
latus.config['%resources'] = gather(
|
|
||||||
latus,
|
latus,
|
||||||
{
|
{
|
||||||
type: '@avocado/resource/resources',
|
type: '@avocado/resource/resources',
|
||||||
idAttribute: 'resourceId',
|
idAttribute: 'resourceId',
|
||||||
typeAttribute: 'resourceType',
|
typeAttribute: 'resourceType',
|
||||||
},
|
},
|
||||||
);
|
));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,8 +8,6 @@ const decorate = compose(
|
||||||
Property('uri'),
|
Property('uri'),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const resource = ({config}) => config['%resources'];
|
|
||||||
|
|
||||||
export default class Resource extends decorate(Class) {
|
export default class Resource extends decorate(Class) {
|
||||||
|
|
||||||
constructor(...args) {
|
constructor(...args) {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Class, compose, EventEmitter} from '@latus/core';
|
import {Class, compose, EventEmitter} from '@latus/core';
|
||||||
|
|
||||||
import Serializer from './serializer';
|
import Serializer from './serializer';
|
||||||
|
@ -53,7 +52,7 @@ export default class ReceiverSynchronizer extends decorate(Class) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSynchronized(type, id, json) {
|
async createSynchronized(type, id, json) {
|
||||||
const {fromResourceId: {[type]: Resource}} = resource(this.latus);
|
const {[type]: Resource} = this.latus.get('%resources');
|
||||||
if (!(type in this.#synchronized)) {
|
if (!(type in this.#synchronized)) {
|
||||||
this.#synchronized[type] = {};
|
this.#synchronized[type] = {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Trait} from '@avocado/traits';
|
import {Trait} from '@avocado/traits';
|
||||||
|
|
||||||
export default (latus) => class Audible extends Trait {
|
export default (latus) => class Audible extends Trait {
|
||||||
|
@ -53,7 +52,7 @@ export default (latus) => class Audible extends Trait {
|
||||||
async extendJson(json) {
|
async extendJson(json) {
|
||||||
const extended = await super.extendJson(json);
|
const extended = await super.extendJson(json);
|
||||||
if (this.params.sounds && 'client' === process.env.SIDE) {
|
if (this.params.sounds && 'client' === process.env.SIDE) {
|
||||||
const {fromResourceType: {Sound}} = resource(latus);
|
const {Sound} = latus.get('%resources');
|
||||||
extended.sounds = Object.fromEntries(
|
extended.sounds = Object.fromEntries(
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.entries(this.params.sounds)
|
Object.entries(this.params.sounds)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import {mapValuesAsync} from '@avocado/core';
|
import {mapValuesAsync} from '@avocado/core';
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {StateProperty, Trait} from '@avocado/traits';
|
import {StateProperty, Trait} from '@avocado/traits';
|
||||||
import {Rectangle, Vector} from '@avocado/math';
|
import {Rectangle, Vector} from '@avocado/math';
|
||||||
import {compose} from '@latus/core';
|
import {compose} from '@latus/core';
|
||||||
|
@ -202,7 +201,7 @@ export default (latus) => class Animated extends decorate(Trait) {
|
||||||
async load(json) {
|
async load(json) {
|
||||||
await super.load(json);
|
await super.load(json);
|
||||||
if (Object.keys(json.params.animations).length > 0) {
|
if (Object.keys(json.params.animations).length > 0) {
|
||||||
const {fromResourceType: {Animation}} = resource(latus);
|
const {Animation} = latus.get('%resources');
|
||||||
const animations = await mapValuesAsync(
|
const animations = await mapValuesAsync(
|
||||||
json.params.animations,
|
json.params.animations,
|
||||||
(json) => Animation.load(json),
|
(json) => Animation.load(json),
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import {packets, Packet} from '@latus/socket';
|
import {Packet} from '@latus/socket';
|
||||||
|
|
||||||
export default (latus) => class LayersUpdateLayerPacket extends Packet {
|
export default (latus) => class LayersUpdateLayerPacket extends Packet {
|
||||||
|
|
||||||
static pack(data) {
|
static pack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data[i].layerPackets = Bundle.encode(data[i].layerPackets);
|
data[i].layerPackets = Bundle.encode(data[i].layerPackets);
|
||||||
|
@ -21,7 +21,7 @@ export default (latus) => class LayersUpdateLayerPacket extends Packet {
|
||||||
}
|
}
|
||||||
|
|
||||||
static unpack(data) {
|
static unpack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data[i].layerPackets = Bundle.decode(data[i].layerPackets);
|
data[i].layerPackets = Bundle.decode(data[i].layerPackets);
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import {packets} from '@latus/socket';
|
|
||||||
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
import {SynchronizedUpdatePacket} from '@avocado/s13n';
|
||||||
|
|
||||||
export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacket {
|
export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacket {
|
||||||
|
|
||||||
static pack(data) {
|
static pack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data.layersPackets = Bundle.encode(data.layersPackets);
|
data.layersPackets = Bundle.encode(data.layersPackets);
|
||||||
return data;
|
return data;
|
||||||
|
@ -17,7 +16,7 @@ export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacke
|
||||||
}
|
}
|
||||||
|
|
||||||
static unpack(data) {
|
static unpack(data) {
|
||||||
const {fromType: {Bundle}} = packets(latus);
|
const {Bundle} = latus.get('%packets');
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
data.layersPackets = Bundle.decode(data.layersPackets);
|
data.layersPackets = Bundle.decode(data.layersPackets);
|
||||||
return data;
|
return data;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import {Property} from '@avocado/core';
|
import {Property} from '@avocado/core';
|
||||||
import {JsonResource, resource} from '@avocado/resource';
|
import {JsonResource} from '@avocado/resource';
|
||||||
import {compose, EventEmitter} from '@latus/core';
|
import {compose, EventEmitter} from '@latus/core';
|
||||||
|
|
||||||
const decorate = compose(
|
const decorate = compose(
|
||||||
|
@ -15,7 +15,7 @@ export default (latus) => class Layer extends decorate(JsonResource) {
|
||||||
super();
|
super();
|
||||||
this.tileEntities = {};
|
this.tileEntities = {};
|
||||||
this.tileGeometry = [];
|
this.tileGeometry = [];
|
||||||
const {fromResourceType: {EntityList, Tiles}} = resource(latus);
|
const {EntityList, Tiles} = latus.get('%resources');
|
||||||
this.setEntityList(new EntityList());
|
this.setEntityList(new EntityList());
|
||||||
this.setTiles(new Tiles());
|
this.setTiles(new Tiles());
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ export default (latus) => class Layer extends decorate(JsonResource) {
|
||||||
tilesetUri,
|
tilesetUri,
|
||||||
world,
|
world,
|
||||||
} = json;
|
} = json;
|
||||||
const {fromResourceType: {EntityList, Tiles, Tileset}} = resource(latus);
|
const {EntityList, Tiles, Tileset} = latus.get('%resources');
|
||||||
this.setTiles(new Tiles(tiles));
|
this.setTiles(new Tiles(tiles));
|
||||||
this.tileset = tilesetUri
|
this.tileset = tilesetUri
|
||||||
? await Tileset.load({extends: tilesetUri})
|
? await Tileset.load({extends: tilesetUri})
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import {compose, EventEmitter} from '@latus/core';
|
import {compose, EventEmitter} from '@latus/core';
|
||||||
import {
|
import {JsonResource} from '@avocado/resource';
|
||||||
JsonResource,
|
|
||||||
resource,
|
|
||||||
} from '@avocado/resource';
|
|
||||||
|
|
||||||
const decorate = compose(
|
const decorate = compose(
|
||||||
EventEmitter,
|
EventEmitter,
|
||||||
|
@ -84,7 +81,7 @@ export default (latus) => class Layers extends decorate(JsonResource) {
|
||||||
async load(json = []) {
|
async load(json = []) {
|
||||||
await super.load(json);
|
await super.load(json);
|
||||||
this.removeAllLayers();
|
this.removeAllLayers();
|
||||||
const {fromResourceType: {Layer}} = resource(latus);
|
const {Layer} = latus.get('%resources');
|
||||||
const layers = await Promise.all(json.map((layer) => Layer.load(layer)));
|
const layers = await Promise.all(json.map((layer) => Layer.load(layer)));
|
||||||
for (let i = 0; i < layers.length; i++) {
|
for (let i = 0; i < layers.length; i++) {
|
||||||
this.addLayer(layers[i]);
|
this.addLayer(layers[i]);
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import {Vector} from '@avocado/math';
|
import {Vector} from '@avocado/math';
|
||||||
import {
|
import {JsonResource} from '@avocado/resource';
|
||||||
JsonResource,
|
|
||||||
resource,
|
|
||||||
} from '@avocado/resource';
|
|
||||||
import {Synchronized} from '@avocado/s13n';
|
import {Synchronized} from '@avocado/s13n';
|
||||||
import {compose, EventEmitter} from '@latus/core';
|
import {compose, EventEmitter} from '@latus/core';
|
||||||
|
|
||||||
|
@ -21,7 +18,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this._s13nId = s13nId++;
|
this._s13nId = s13nId++;
|
||||||
const {fromResourceType: {Layers}} = resource(latus);
|
const {Layers} = latus.get('%resources');
|
||||||
this.setLayers(new Layers());
|
this.setLayers(new Layers());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +66,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
|
||||||
if (size) {
|
if (size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
const {fromResourceType: {Layers}} = resource(latus);
|
const {Layers} = latus.get('%resources');
|
||||||
this.setLayers(
|
this.setLayers(
|
||||||
layers
|
layers
|
||||||
? await Layers.load(layers)
|
? await Layers.load(layers)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ describe('Layer', () => {
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
});
|
});
|
||||||
it('has sane defaults', async () => {
|
it('has sane defaults', async () => {
|
||||||
const {fromResourceType: {Layer}} = resource(latus);
|
const {Layer} = latus.get('%resources');
|
||||||
const layer = new Layer();
|
const layer = new Layer();
|
||||||
expect(Object.keys(layer.entities).length).to.equal(0);
|
expect(Object.keys(layer.entities).length).to.equal(0);
|
||||||
expect(layer.tiles.size).to.deep.equal([0, 0]);
|
expect(layer.tiles.size).to.deep.equal([0, 0]);
|
||||||
|
@ -23,7 +22,7 @@ describe('Layer', () => {
|
||||||
let entity;
|
let entity;
|
||||||
let layer;
|
let layer;
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const {fromResourceType: {Entity, EntityList, Layer}} = resource(latus);
|
const {Entity, EntityList, Layer} = latus.get('%resources');
|
||||||
entity = await Entity.load({
|
entity = await Entity.load({
|
||||||
traits: {
|
traits: {
|
||||||
Existent: {},
|
Existent: {},
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -12,13 +11,13 @@ describe('Tiles', () => {
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
});
|
});
|
||||||
it("has sane defaults", async () => {
|
it("has sane defaults", async () => {
|
||||||
const {fromResourceType: {Tiles}} = resource(latus);
|
const {Tiles} = latus.get('%resources');
|
||||||
const tiles = new Tiles();
|
const tiles = new Tiles();
|
||||||
expect(tiles.size).to.deep.equal([0, 0]);
|
expect(tiles.size).to.deep.equal([0, 0]);
|
||||||
expect(tiles.data.length).to.equal(0);
|
expect(tiles.data.length).to.equal(0);
|
||||||
});
|
});
|
||||||
it("gets and sets tile data", async () => {
|
it("gets and sets tile data", async () => {
|
||||||
const {fromResourceType: {Tiles}} = resource(latus);
|
const {Tiles} = latus.get('%resources');
|
||||||
const tiles = new Tiles({data: [0, 1, 2, 3], size: [2, 2]});
|
const tiles = new Tiles({data: [0, 1, 2, 3], size: [2, 2]});
|
||||||
expect(tiles.tileAt([0, 0])).to.equal(0);
|
expect(tiles.tileAt([0, 0])).to.equal(0);
|
||||||
expect(tiles.tileAt([1, 0])).to.equal(1);
|
expect(tiles.tileAt([1, 0])).to.equal(1);
|
||||||
|
@ -31,7 +30,7 @@ describe('Tiles', () => {
|
||||||
expect(tiles.data).to.deep.equal([69, 420, 311, 42]);
|
expect(tiles.data).to.deep.equal([69, 420, 311, 42]);
|
||||||
});
|
});
|
||||||
it("can get a data slice", async () => {
|
it("can get a data slice", async () => {
|
||||||
const {fromResourceType: {Tiles}} = resource(latus);
|
const {Tiles} = latus.get('%resources');
|
||||||
const tiles = new Tiles(
|
const tiles = new Tiles(
|
||||||
{
|
{
|
||||||
data: [
|
data: [
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import {Image} from '@avocado/graphics';
|
import {Image} from '@avocado/graphics';
|
||||||
import {resource} from '@avocado/resource';
|
|
||||||
import {Latus} from '@latus/core';
|
import {Latus} from '@latus/core';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
|
||||||
|
@ -13,17 +12,17 @@ describe('Tileset', () => {
|
||||||
'@avocado/topdown': require('../src'),
|
'@avocado/topdown': require('../src'),
|
||||||
});
|
});
|
||||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||||
const {fromResourceType: {Tileset}} = resource(latus);
|
const {Tileset} = latus.get('%resources');
|
||||||
Tileset.root = 'test/fixtures';
|
Tileset.root = 'test/fixtures';
|
||||||
});
|
});
|
||||||
it("has sane defaults", async () => {
|
it("has sane defaults", async () => {
|
||||||
const {fromResourceType: {Tileset}} = resource(latus);
|
const {Tileset} = latus.get('%resources');
|
||||||
const tileset = new Tileset();
|
const tileset = new Tileset();
|
||||||
expect(tileset.tileSize).to.deep.equal([0, 0]);
|
expect(tileset.tileSize).to.deep.equal([0, 0]);
|
||||||
expect(tileset.image).to.equal(undefined);
|
expect(tileset.image).to.equal(undefined);
|
||||||
});
|
});
|
||||||
it("can load", async () => {
|
it("can load", async () => {
|
||||||
const {fromResourceType: {Tileset}} = resource(latus);
|
const {Tileset} = latus.get('%resources');
|
||||||
const tileset = await Tileset.load({extends: '/test.tileset.json'});
|
const tileset = await Tileset.load({extends: '/test.tileset.json'});
|
||||||
expect(tileset.tileSize).to.deep.equal([4, 4]);
|
expect(tileset.tileSize).to.deep.equal([4, 4]);
|
||||||
expect(tileset.image.size).to.deep.equal([16, 16]);
|
expect(tileset.image.size).to.deep.equal([16, 16]);
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
import {gather} from '@latus/core';
|
import {gather} from '@latus/core';
|
||||||
|
|
||||||
export {default as Trait, traits} from './trait';
|
export {default as Trait} from './trait';
|
||||||
export {default as StateProperty} from './state-property';
|
export {default as StateProperty} from './state-property';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
hooks: {
|
||||||
'@latus/core/starting': (latus) => {
|
'@latus/core/starting': (latus) => {
|
||||||
// eslint-disable-next-line no-param-reassign
|
latus.set('%traits', gather(
|
||||||
latus.config['%traits'] = gather(
|
|
||||||
latus,
|
latus,
|
||||||
{
|
{
|
||||||
type: '@avocado/traits',
|
type: '@avocado/traits',
|
||||||
},
|
},
|
||||||
);
|
));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,8 +6,6 @@ const decorate = compose(
|
||||||
Synchronized,
|
Synchronized,
|
||||||
);
|
);
|
||||||
|
|
||||||
export const traits = (latus) => latus.config['%traits'];
|
|
||||||
|
|
||||||
export default class Trait extends decorate(JsonResource) {
|
export default class Trait extends decorate(JsonResource) {
|
||||||
|
|
||||||
#markedAsDirty = true;
|
#markedAsDirty = true;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user