blueshell
Version:
A Behavior Tree implementation in modern Javascript
54 lines • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeArchyTree = serializeArchyTree;
const archy = require("archy");
const models_1 = require("../models");
function buildArchyTree(node, contextDepth, state) {
let label = node.name;
if (label !== node.constructor.name) {
label += ' (' + node.constructor.name + ')';
}
let onPath = false;
if (state) {
const eventCounter = node.getTreeEventCounter(state);
const lastEventSeen = node.getLastEventSeen(state);
const lastResult = node.getLastResult(state);
if (lastEventSeen === eventCounter && lastResult) {
label += ' => ' + lastResult;
onPath = true;
}
}
if (!onPath) {
if (contextDepth < 0) {
return undefined;
}
if (contextDepth === 0) {
return {
label: '...',
nodes: [],
};
}
}
const nodes = [];
if ((0, models_1.isParentNode)(node)) {
for (const child of node.getChildren()) {
const childDepth = contextDepth - (onPath ? 0 : 1);
const subTree = buildArchyTree(child, childDepth, state);
if (subTree) {
nodes.push(subTree);
}
}
}
return {
label,
nodes,
};
}
function serializeArchyTree(tree, state, contextDepth = Number.MAX_SAFE_INTEGER) {
const archyTree = buildArchyTree(tree, contextDepth, state);
if (archyTree) {
return archy(archyTree);
}
return '';
}
//# sourceMappingURL=archyTree.js.map