avocado/packages/entity/test/alive.js
2021-01-03 01:20:07 -06:00

138 lines
4.1 KiB
JavaScript

import {
buildCondition,
buildExpression,
buildInvoke,
} from '@avocado/behavior';
import {resource} from '@avocado/resource';
import {normalize} from '@avocado/s13n';
import {Latus} from '@latus/core';
import {expect} from 'chai';
const {name} = require('../package.json');
describe(name, () => {
let latus;
let Entity;
beforeEach(async () => {
latus = Latus.mock([
'@avocado/behavior',
['@avocado/entity', `${__dirname}/../src`],
'@avocado/resource',
'@avocado/traits',
'@latus/socket',
]);
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
});
describe('Traits', () => {
describe('Alive', () => {
let entity;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Alive: {},
},
});
});
it('exists', async () => {
expect(entity.is('Alive')).to.equal(true);
});
it('is alive', async () => {
expect(entity.isDying).to.equal(false);
});
it('can die', async () => {
entity.life = 0;
entity.tick(0);
expect(entity.isDying).to.equal(true);
});
it('clamps life', async () => {
entity.life = 120;
expect(entity.life).to.equal(100);
entity.maxLife = 50;
expect(entity.life).to.equal(50);
});
it('can have a custom death condition', async () => {
const entity = await Entity.load({
traits: {
Alive: {
params: {
deathCondition: buildCondition('<=', [
buildExpression(['entity', 'life']),
10,
]),
},
},
},
});
entity.tick(0);
expect(entity.isDying).to.equal(false);
entity.life = 10;
entity.tick(0);
expect(entity.isDying).to.equal(true);
});
it('runs actions on death', async () => {
let didActions;
const entity = await Entity.load({
traits: {
Alive: {
params: {
deathActions: {
type: 'expressions',
expressions: [
buildInvoke(['entity', 'ded']),
],
},
},
},
},
});
entity.ded = () => {
didActions = true;
};
const handle = setInterval(() => {
entity.tick();
}, 16.66);
await entity.forceDeath();
clearInterval(handle);
expect(didActions).to.equal(true);
});
describe('Packets', () => {
let entity2;
beforeEach(async () => {
entity2 = await Entity.load({
traits: {
Alive: {},
},
});
});
it('generates and accepts life packets', async () => {
entity.life = 80;
entity.maxLife = 90;
const packets = entity.traitInstance('Alive').packets();
expect(packets).to.have.lengthOf(1);
expect(packets[0][0]).to.equal('TraitUpdateAlive');
expect(packets[0][1]).to.deep.equal({life: 80, maxLife: 90});
entity2.traitInstance('Alive').acceptPacket(normalize(latus, packets[0]).pop());
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 = entity.traitInstance('Alive').packets();
expect(packets).to.have.lengthOf(2);
expect(packets[0][0]).to.equal('TraitUpdateAlive');
expect(packets[0][1]).to.deep.equal({life: 0, maxLife: 100});
expect(packets[1][0]).to.equal('Died');
expect(entity2.isDying).to.equal(false);
const promise = new Promise((resolve) => {
entity2.on('isDyingChanged', resolve);
});
entity2.traitInstance('Alive').acceptPacket(normalize(latus, packets[1]).pop());
return promise;
});
});
});
});
});