2021-01-03 00:57:51 -06:00
|
|
|
import {
|
|
|
|
buildCondition,
|
|
|
|
buildExpression,
|
|
|
|
buildInvoke,
|
|
|
|
} from '@avocado/behavior';
|
|
|
|
import {Latus} from '@latus/core';
|
2021-01-14 01:22:47 -06:00
|
|
|
import {normalize} from '@latus/socket';
|
2021-01-03 00:57:51 -06:00
|
|
|
import {expect} from 'chai';
|
|
|
|
|
2021-01-05 11:25:37 -06:00
|
|
|
let latus;
|
|
|
|
let Entity;
|
|
|
|
beforeEach(async () => {
|
|
|
|
latus = Latus.mock({
|
|
|
|
'@avocado/behavior': require('@avocado/behavior'),
|
|
|
|
'@avocado/entity': require('../src'),
|
|
|
|
'@avocado/resource': require('@avocado/resource'),
|
|
|
|
'@avocado/traits': require('@avocado/traits'),
|
|
|
|
'@latus/socket': require('@latus/socket'),
|
|
|
|
});
|
|
|
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
2021-01-22 17:58:45 -06:00
|
|
|
({Entity} = latus.get('%resources'));
|
2021-01-05 11:25:37 -06:00
|
|
|
});
|
|
|
|
describe('Alive', () => {
|
|
|
|
let entity;
|
2021-01-03 00:57:51 -06:00
|
|
|
beforeEach(async () => {
|
2021-01-05 11:25:37 -06:00
|
|
|
entity = await Entity.load({
|
|
|
|
traits: {
|
|
|
|
Alive: {},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('exists', async () => {
|
|
|
|
expect(entity.is('Alive')).to.be.true;
|
|
|
|
});
|
|
|
|
it('is alive', async () => {
|
|
|
|
expect(entity.isDying).to.be.false;
|
|
|
|
});
|
|
|
|
if ('client' !== process.env.SIDE) {
|
|
|
|
it('can die', async () => {
|
|
|
|
entity.life = 0;
|
|
|
|
entity.tick(0);
|
|
|
|
expect(entity.isDying).to.be.true;
|
2021-01-05 02:47:30 -06:00
|
|
|
});
|
2021-01-05 11:25:37 -06:00
|
|
|
}
|
|
|
|
it('clamps life', async () => {
|
|
|
|
entity.life = 120;
|
|
|
|
expect(entity.life).to.equal(100);
|
|
|
|
entity.maxLife = 50;
|
|
|
|
expect(entity.life).to.equal(50);
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
2021-01-05 11:25:37 -06:00
|
|
|
if ('client' !== process.env.SIDE) {
|
|
|
|
it('can have a custom death condition', async () => {
|
|
|
|
const entity = await Entity.load({
|
|
|
|
traits: {
|
|
|
|
Alive: {
|
|
|
|
params: {
|
|
|
|
deathCondition: buildCondition('<=', [
|
|
|
|
buildExpression(['entity', 'life']),
|
|
|
|
10,
|
|
|
|
]),
|
|
|
|
},
|
2021-01-03 00:57:51 -06:00
|
|
|
},
|
2021-01-05 11:25:37 -06:00
|
|
|
},
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
2021-01-05 11:25:37 -06:00
|
|
|
entity.tick(0);
|
|
|
|
expect(entity.isDying).to.be.false;
|
|
|
|
entity.life = 10;
|
|
|
|
entity.tick(0);
|
|
|
|
expect(entity.isDying).to.be.true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
it('runs actions on death', async () => {
|
|
|
|
let didActions;
|
|
|
|
const entity = await Entity.load({
|
|
|
|
traits: {
|
|
|
|
Alive: {
|
|
|
|
params: {
|
|
|
|
deathActions: {
|
|
|
|
type: 'expressions',
|
|
|
|
expressions: [
|
|
|
|
buildInvoke(['entity', 'ded']),
|
|
|
|
],
|
2021-01-03 00:57:51 -06:00
|
|
|
},
|
|
|
|
},
|
2021-01-05 11:25:37 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
entity.ded = () => {
|
|
|
|
didActions = true;
|
|
|
|
};
|
|
|
|
const handle = setInterval(() => {
|
|
|
|
entity.tick();
|
|
|
|
}, 16.66);
|
2021-01-22 10:47:16 -06:00
|
|
|
await entity.die();
|
2021-01-05 11:25:37 -06:00
|
|
|
clearInterval(handle);
|
|
|
|
expect(didActions).to.be.true;
|
|
|
|
});
|
|
|
|
describe('Packets', () => {
|
|
|
|
let entity2;
|
|
|
|
beforeEach(async () => {
|
|
|
|
entity2 = await Entity.load({
|
|
|
|
traits: {
|
|
|
|
Alive: {},
|
|
|
|
},
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
2021-01-05 11:25:37 -06:00
|
|
|
});
|
|
|
|
it('generates and accepts life packets', async () => {
|
|
|
|
entity.life = 80;
|
|
|
|
entity.maxLife = 90;
|
2021-01-14 01:22:47 -06:00
|
|
|
const packets = entity.trait('Alive').packets();
|
2021-01-05 11:25:37 -06:00
|
|
|
expect(packets).to.have.lengthOf(1);
|
2021-01-14 01:22:47 -06:00
|
|
|
expect(packets[0][0]).to.equal('TraitUpdateAlive');
|
|
|
|
expect(packets[0][1]).to.deep.equal({life: 80, maxLife: 90});
|
|
|
|
entity2.trait('Alive').acceptPacket(normalize(latus, packets[0]));
|
2021-01-05 11:25:37 -06:00
|
|
|
expect(entity2.life).to.equal(80);
|
|
|
|
expect(entity2.maxLife).to.equal(90);
|
|
|
|
});
|
|
|
|
if ('client' !== process.env.SIDE) {
|
|
|
|
it('generates and accepts death packets', async () => {
|
|
|
|
entity.life = 0;
|
|
|
|
entity.tick();
|
2021-01-14 01:22:47 -06:00
|
|
|
const packets = entity.trait('Alive').packets();
|
2021-01-05 11:25:37 -06:00
|
|
|
expect(packets).to.have.lengthOf(2);
|
2021-01-14 01:22:47 -06:00
|
|
|
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');
|
2021-01-05 11:25:37 -06:00
|
|
|
expect(entity2.isDying).to.be.false;
|
|
|
|
const promise = new Promise((resolve) => {
|
|
|
|
entity2.on('isDyingChanged', resolve);
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
2021-01-14 01:22:47 -06:00
|
|
|
entity2.trait('Alive').acceptPacket(normalize(latus, packets[1]));
|
2021-01-05 11:25:37 -06:00
|
|
|
return promise;
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
2021-01-05 11:25:37 -06:00
|
|
|
}
|
2021-01-03 00:57:51 -06:00
|
|
|
});
|
|
|
|
});
|