refactor: API

This commit is contained in:
cha0s 2021-01-04 06:47:52 -06:00
parent 65474994e0
commit 79b5353cab
9 changed files with 14 additions and 14 deletions

View File

@ -86,7 +86,7 @@ export default (latus) => class Behaved extends decorate(Trait) {
type: 'string',
label: 'Current routine',
options: (entity) => (
Object.keys(entity.traitInstance('behaved').params.routines)
Object.keys(entity.trait('Behaved').params.routines)
.reduce((r, key) => ({...r, [key]: key}), {})
),
},

View File

@ -354,7 +354,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
// };
// }
traitInstance(type) {
trait(type) {
return this._traits[type];
}

View File

@ -136,7 +136,7 @@ export default (latus) => class Spawner extends decorate(Trait) {
if (!entity) {
return undefined;
}
return Object.keys(entity.traitInstance('spawner').params.spawns)
return Object.keys(entity.trait('Spawner').params.spawns)
.reduce((r, key) => ({...r, [key]: key}), {});
}

View File

@ -108,18 +108,18 @@ describe(name, () => {
it('generates and accepts life packets', async () => {
entity.life = 80;
entity.maxLife = 90;
const packets = normalize(latus, entity.traitInstance('Alive').packets());
const packets = normalize(latus, 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.traitInstance('Alive').acceptPacket(packets[0]);
entity2.trait('Alive').acceptPacket(packets[0]);
expect(entity2.life).to.equal(80);
expect(entity2.maxLife).to.equal(90);
});
it('generates and accepts death packets', async () => {
entity.life = 0;
entity.tick();
const packets = normalize(latus, entity.traitInstance('Alive').packets());
const packets = normalize(latus, 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});
@ -128,7 +128,7 @@ describe(name, () => {
const promise = new Promise((resolve) => {
entity2.on('isDyingChanged', resolve);
});
entity2.traitInstance('Alive').acceptPacket(packets[1]);
entity2.trait('Alive').acceptPacket(packets[1]);
return promise;
});
});

View File

@ -47,7 +47,7 @@ describe(name, () => {
});
it('generates and accepts direction packets', async () => {
entity.direction = 2;
const packets = normalize(latus, entity.traitInstance('Directional').packets());
const packets = normalize(latus, 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);
@ -57,7 +57,7 @@ describe(name, () => {
},
});
expect(entity2.direction).to.equal(0);
entity2.traitInstance('Directional').acceptPacket(packets[0]);
entity2.trait('Directional').acceptPacket(packets[0]);
expect(entity2.direction).to.equal(2);
});
});

View File

@ -34,7 +34,7 @@ describe(name, () => {
});
it('generates and accepts movement packets', async () => {
entity.setPosition([1, 1]);
const packets = normalize(latus, entity.traitInstance('Positioned').packets());
const packets = normalize(latus, 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]);
@ -44,7 +44,7 @@ describe(name, () => {
},
});
expect(entity2.position).to.deep.equal([0, 0]);
const trait = entity2.traitInstance('Positioned');
const trait = entity2.trait('Positioned');
trait.acceptPacket(packets[0]);
expect(trait.serverPosition).to.deep.equal([1, 1]);
});

View File

@ -67,7 +67,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
['key', {
type: 'string',
options: (entity) => (
Object.keys(entity.traitInstance('emitter').particles)
Object.keys(entity.trait('Emitter').particles)
.reduce((r, key) => ({...r, [key]: key}), {})
),
}],

View File

@ -97,7 +97,7 @@ export default class Audible extends Trait {
}
static optionsForSounds(entity) {
return Object.keys(entity.traitInstance('audible').params.sounds)
return Object.keys(entity.trait('Audible').params.sounds)
.reduce((r, key) => ({...r, [key]: key}), {});
}

View File

@ -48,7 +48,7 @@ export default class Animated extends decorate(Trait) {
type: 'string',
label: 'Current animation',
options: (entity) => (
Object.keys(entity.traitInstance('animated').params.animations)
Object.keys(entity.trait('Animated').params.animations)
.reduce((r, key) => ({...r, [key]: key}), {})
),
},