avocado/packages/entity/test/spawner.js

102 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-01-03 00:57:51 -06:00
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
const {name} = require('../package.json');
describe(name, () => {
let latus;
let Entity;
2021-01-03 16:37:03 -06:00
let EntityList;
2021-01-03 00:57:51 -06:00
beforeEach(async () => {
latus = Latus.mock([
['@avocado/entity', `${__dirname}/../src`],
'@avocado/resource',
'@avocado/traits',
]);
await Promise.all(latus.invokeFlat('@latus/core/starting'));
2021-01-03 16:37:03 -06:00
({fromResourceType: {Entity, EntityList}} = resource(latus));
2021-01-03 00:57:51 -06:00
});
describe('Traits', () => {
describe('Spawner', () => {
2021-01-03 16:37:03 -06:00
let entity;
2021-01-03 18:02:05 -06:00
let list;
2021-01-03 16:37:03 -06:00
beforeEach(async () => {
entity = await Entity.load({
2021-01-03 00:57:51 -06:00
traits: {
2021-01-03 18:02:05 -06:00
Listed: {},
Spawner: {
params: {
spawns: {
testy: {
traits: {
Alive: {},
Listed: {},
Positioned: {},
},
},
},
},
},
2021-01-03 00:57:51 -06:00
},
});
2021-01-03 18:02:05 -06:00
list = new EntityList();
list.addEntity(entity);
2021-01-03 16:37:03 -06:00
});
it('exists', async () => {
2021-01-03 18:02:05 -06:00
expect(entity.is('Spawner')).to.be.true;
2021-01-03 00:57:51 -06:00
});
2021-01-03 18:02:05 -06:00
it('can spawn from key', async () => {
const spawned = await entity.spawn('testy');
expect(spawned.is('Alive')).to.be.true;
const spawned2 = await entity.spawn('testy', {
traits: {
Alive: {
state: {
life: 50,
},
},
},
});
expect(spawned2.life).to.equal(50);
const spawned3 = await entity.spawnAt('testy', [69, 420]);
expect(spawned3.position).to.deep.equal([69, 420]);
2021-01-03 16:37:03 -06:00
});
it('can spawn from arbitrary JSON', async () => {
2021-01-03 18:02:05 -06:00
const spawned = await entity.spawnRaw({
traits: {
Listed: {},
Mobile: {},
},
});
expect(spawned.is('Mobile')).to.be.true;
const spawned2 = await entity.spawnRawAt(
{
traits: {
Listed: {},
Mobile: {},
},
},
[311, 200],
);
expect(spawned2.position).to.deep.equal([311, 200]);
2021-01-03 16:37:03 -06:00
});
it('can kill all children', async () => {
2021-01-03 18:02:05 -06:00
const COUNT = 15;
for (let i = 0; i < COUNT; ++i) {
await entity.spawnRaw({
traits: {
Existent: {},
Listed: {},
Mobile: {},
},
});
}
2021-01-04 07:23:40 -06:00
expect(Object.keys(list.entities)).to.have.lengthOf(COUNT + 1);
2021-01-03 18:02:05 -06:00
entity.killAllChildren();
2021-01-04 07:23:40 -06:00
expect(Object.keys(list.entities)).to.have.lengthOf(1);
2021-01-03 16:37:03 -06:00
});
2021-01-03 00:57:51 -06:00
});
});
});