avocado/packages/entity/test/directional.js
2022-03-22 01:08:24 -05:00

68 lines
2.0 KiB
JavaScript

/* eslint-disable global-require */
import {Flecks} from '@flecks/core';
import {normalize} from '@flecks/socket';
import {expect} from 'chai';
let flecks;
let Entity;
beforeEach(async () => {
flecks = new Flecks({
flecks: {
'@avocado/entity': require('@avocado/entity'),
'@avocado/graphics': require('@avocado/graphics'),
'@avocado/resource': require('@avocado/resource'),
'@avocado/traits': require('@avocado/traits'),
'@flecks/core': require('@flecks/core'),
'@flecks/react': require('@flecks/react'),
'@flecks/socket': require('@flecks/socket'),
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
});
afterEach(() => {
flecks.destroy();
});
describe('Directional', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Directional: {
params: {
directionCount: 4,
},
},
},
});
});
it('exists', async () => {
expect(entity.is('Directional')).to.be.true;
});
it('tracks movement', async () => {
entity.emit('movementRequest', [1, 0]);
expect(entity.direction).to.equal(1);
entity.emit('movementRequest', [0, 1]);
expect(entity.direction).to.equal(2);
entity.emit('movementRequest', [-1, 0]);
expect(entity.direction).to.equal(3);
entity.emit('movementRequest', [0, -1]);
expect(entity.direction).to.equal(0);
});
it('generates and accepts direction packets', async () => {
entity.direction = 2;
const packets = entity.trait('Directional').packetsFor();
expect(packets).to.have.lengthOf(1);
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(normalize(flecks, packets[0]));
expect(entity2.direction).to.equal(2);
});
});