blueshell
Version:
A Behavior Tree implementation in modern Javascript
106 lines • 4.4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Created by josh on 1/15/16.
*/
const chai_1 = require("chai");
const RobotActions_1 = require("./test/RobotActions");
const lib_1 = require("../../lib");
const Behavior = __importStar(require("../../lib"));
class StopMotors extends Behavior.Action {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEvent(state, event) {
state.commands.push('motorsStopped');
return lib_1.rc.SUCCESS;
}
}
class StopLasers extends Behavior.Action {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEvent(state, event) {
const storage = this.getNodeStorage(state);
storage.cooldown = storage.cooldown ? --storage.cooldown : state.laserCooldownTime;
let result = lib_1.rc.SUCCESS;
// console.log('Storage cooldown is ', storage.cooldown);
if (storage.cooldown > 0) {
result = lib_1.rc.RUNNING;
}
else {
state.commands.push('lasersCooled');
}
return result;
}
}
class Shutdown extends Behavior.Action {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEvent(state, event) {
state.commands.push('powerOff');
return lib_1.rc.SUCCESS;
}
}
const shutdownSequence = new Behavior.LatchedSequence('shutdownWithWaitAi', [
new StopMotors(),
new StopLasers(),
new Shutdown(),
]);
describe('LatchedSelector', function () {
it('should run correctly', function () {
// With a happy bot
const botState = new RobotActions_1.RobotState();
botState.laserCooldownTime = 0;
const res = shutdownSequence.handleEvent(botState, 'lowBattery');
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree success');
chai_1.assert.equal(botState.commands.length, 3, 'Need Three Commands');
chai_1.assert.equal(botState.commands[0], 'motorsStopped');
chai_1.assert.equal(botState.commands[1], 'lasersCooled');
chai_1.assert.equal(botState.commands[2], 'powerOff');
});
it('should loop correctly', function () {
// With a happy bot
const botState = new RobotActions_1.RobotState();
botState.laserCooldownTime = 1;
let res = shutdownSequence.handleEvent(botState, 'lowBattery 1');
chai_1.assert.equal(res, lib_1.rc.RUNNING, 'Behavior Tree Running');
chai_1.assert.equal(botState.commands.length, 1);
chai_1.assert.equal(botState.commands[0], 'motorsStopped');
res = shutdownSequence.handleEvent(botState, 'lowBattery 2');
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree Success');
chai_1.assert.equal(botState.commands.length, 3, 'Need Three Commands');
chai_1.assert.equal(botState.commands[0], 'motorsStopped');
chai_1.assert.equal(botState.commands[1], 'lasersCooled');
chai_1.assert.equal(botState.commands[2], 'powerOff');
});
});
//# sourceMappingURL=LatchedSequence.test.js.map