import {Latus} from '@latus/core'; import {expect} from 'chai'; import { buildExpression, buildExpressions, buildInvoke, buildValue, } from '../src/builders'; const asyncWait = () => new Promise((resolve) => setTimeout(resolve, 0)); let latus; let Entity; beforeEach(async () => { latus = Latus.mock({ '@avocado/behavior': require('../src'), '@avocado/entity': require('@avocado/entity'), '@avocado/resource': require('@avocado/resource'), '@avocado/traits': require('@avocado/traits'), }); await Promise.all(latus.invokeFlat('@latus/core/starting')); ({Entity} = latus.get('%resources')); }); describe('Behaved', () => { let entity; beforeEach(async () => { entity = await Entity.load({ traits: { Behaved: { params: { routines: { initial: buildExpressions([ buildInvoke( ['entity', 'forceMovement'], [ buildValue([10, 0]), ], ), buildInvoke( ['Timing', 'wait'], [ buildValue(1), ], ), ]), another: buildExpressions([ buildInvoke( ['entity', 'forceMovement'], [ buildValue([20, 0]), ], ), ]), one: buildExpressions([ buildInvoke( ['entity', 'forceMovement'], [ buildValue([30, 0]), ], ), buildInvoke( ['Timing', 'wait'], [ buildValue(0), ], ), buildInvoke( ['entity', 'jumpToRoutine'], [ buildValue('two'), ], ), ]), two: buildExpressions([ buildInvoke( ['entity', 'forceMovement'], [ buildValue([40, 0]), ], ), buildInvoke( ['entity', 'jumpToRoutine'], [ buildValue('one'), ], ), ]), }, }, }, Mobile: {}, Positioned: {}, }, }); }); it('exists', async () => { expect(entity.is('Behaved')).to.be.true; }); if ('client' !== process.env.SIDE) { it('runs routines', async () => { expect(entity.position).to.deep.equal([0, 0]); entity.tick(0); await asyncWait(); expect(entity.position).to.deep.equal([10, 0]); entity.tick(1); await asyncWait(); expect(entity.position).to.deep.equal([10, 0]); entity.isBehaving = false; entity.tick(1); await asyncWait(); expect(entity.position).to.deep.equal([10, 0]); entity.isBehaving = true; entity.tick(1); await asyncWait(); expect(entity.position).to.deep.equal([20, 0]); }); it('can change routines', async () => { entity.currentRoutine = 'another'; expect(entity.position).to.deep.equal([0, 0]); entity.tick(0); await asyncWait(); expect(entity.position).to.deep.equal([20, 0]); }); it('allows routines to set routine', async () => { entity.currentRoutine = 'one'; expect(entity.position).to.deep.equal([0, 0]); entity.tick(0); await asyncWait(); expect(entity.currentRoutine).to.equal('one'); expect(entity.position).to.deep.equal([30, 0]); entity.tick(0); await asyncWait(); expect(entity.currentRoutine).to.equal('one'); expect(entity.position).to.deep.equal([30, 0]); entity.tick(0); await asyncWait(); expect(entity.currentRoutine).to.equal('two'); expect(entity.position).to.deep.equal([30, 0]); entity.tick(0); await asyncWait(); expect(entity.currentRoutine).to.equal('one'); expect(entity.position).to.deep.equal([70, 0]); }); } });