avocado/packages/entity/test/mobile.js
2024-01-28 11:51:17 -06:00

68 lines
1.9 KiB
JavaScript

/* eslint-disable global-require */
import {Flecks} from '@flecks/core';
import {expect} from 'chai';
let flecks;
let Entity;
beforeEach(async () => {
flecks = await Flecks.from({
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'),
},
});
({Entity} = flecks.avocado.resource.Resources);
});
afterEach(() => {
flecks.destroy();
});
describe('Mobile', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Mobile: {},
Positioned: {},
},
});
});
it('exists', async () => {
expect(entity.is('Mobile')).to.be.true;
});
it('can request movement', async () => {
entity.speed = 100;
entity.requestMovement([1, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
entity.isMobile = false;
entity.requestMovement([1, 0]);
entity.tick(1);
expect(entity.position).to.deep.equal([100, 0]);
});
it('can force movement', async () => {
expect(entity.speed).to.equal(0);
entity.forceMovement([10, 0]);
expect(entity.position).to.deep.equal([10, 0]);
});
it('can move for a time', async () => {
entity.speed = 10;
const tickingPromise = entity.moveFor([1, 0], 1);
entity.addTickingPromise(tickingPromise);
expect(entity.position).to.deep.equal([0, 0]);
entity.tick(0.25);
expect(entity.position).to.deep.equal([2.5, 0]);
entity.tick(0.25);
expect(entity.position).to.deep.equal([5, 0]);
entity.tick(0.5);
expect(entity.position).to.deep.equal([10, 0]);
return tickingPromise;
});
});