avocado/packages/entity/test/alive.js

138 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-01-03 00:57:51 -06:00
import {
buildCondition,
buildExpression,
buildInvoke,
} from '@avocado/behavior';
import {resource} from '@avocado/resource';
2021-01-03 01:20:07 -06:00
import {normalize} from '@avocado/s13n';
2021-01-03 00:57:51 -06:00
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 () => {
2021-01-03 18:02:05 -06:00
expect(entity.is('Alive')).to.be.true;
2021-01-03 00:57:51 -06:00
});
it('is alive', async () => {
2021-01-03 18:02:05 -06:00
expect(entity.isDying).to.be.false;
2021-01-03 00:57:51 -06:00
});
it('can die', async () => {
entity.life = 0;
entity.tick(0);
2021-01-03 18:02:05 -06:00
expect(entity.isDying).to.be.true;
2021-01-03 00:57:51 -06:00
});
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);
2021-01-03 18:02:05 -06:00
expect(entity.isDying).to.be.false;
2021-01-03 00:57:51 -06:00
entity.life = 10;
entity.tick(0);
2021-01-03 18:02:05 -06:00
expect(entity.isDying).to.be.true;
2021-01-03 00:57:51 -06:00
});
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);
2021-01-03 18:02:05 -06:00
expect(didActions).to.be.true;
2021-01-03 00:57:51 -06:00
});
describe('Packets', () => {
2021-01-03 01:20:07 -06:00
let entity2;
beforeEach(async () => {
entity2 = await Entity.load({
traits: {
Alive: {},
},
});
});
it('generates and accepts life packets', async () => {
2021-01-03 00:57:51 -06:00
entity.life = 80;
entity.maxLife = 90;
2021-01-04 06:47:52 -06:00
const packets = normalize(latus, entity.trait('Alive').packets());
2021-01-03 00:57:51 -06:00
expect(packets).to.have.lengthOf(1);
2021-01-03 16:37:03 -06:00
expect(packets[0].constructor.type).to.equal('TraitUpdateAlive');
expect(packets[0].data).to.deep.equal({life: 80, maxLife: 90});
2021-01-04 06:47:52 -06:00
entity2.trait('Alive').acceptPacket(packets[0]);
2021-01-03 01:20:07 -06:00
expect(entity2.life).to.equal(80);
expect(entity2.maxLife).to.equal(90);
2021-01-03 00:57:51 -06:00
});
2021-01-03 01:20:07 -06:00
it('generates and accepts death packets', async () => {
2021-01-03 00:57:51 -06:00
entity.life = 0;
entity.tick();
2021-01-04 06:47:52 -06:00
const packets = normalize(latus, entity.trait('Alive').packets());
2021-01-03 00:57:51 -06:00
expect(packets).to.have.lengthOf(2);
2021-01-03 16:37:03 -06:00
expect(packets[0].constructor.type).to.equal('TraitUpdateAlive');
expect(packets[0].data).to.deep.equal({life: 0, maxLife: 100});
expect(packets[1].constructor.type).to.equal('Died');
2021-01-03 18:02:05 -06:00
expect(entity2.isDying).to.be.false;
2021-01-03 01:20:07 -06:00
const promise = new Promise((resolve) => {
entity2.on('isDyingChanged', resolve);
});
2021-01-04 06:47:52 -06:00
entity2.trait('Alive').acceptPacket(packets[1]);
2021-01-03 01:20:07 -06:00
return promise;
2021-01-03 00:57:51 -06:00
});
});
});
});
});