UNPKG

behaviortree

Version:

A JavaScript implementation of Behavior Trees. They are useful for implementing AIs. For Browsers and NodeJS.

42 lines (41 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Introspector { constructor() { this.currentResult = []; this.results = []; } start(tree) { this.tree = tree; this.currentResult = []; } end() { const result = this.currentResult.pop(); if (result) { this.results.push(result); } delete this.tree; this.currentResult = []; } push(node, result, blackboard) { this.currentResult.push(this._toResult(node, result, blackboard)); } wrapLast(numResults, node, result, blackboard) { const children = this.currentResult.splice(this.currentResult.length - numResults, numResults); this.currentResult.push({ ...this._toResult(node, result, blackboard), children }); } // eslint-disable-next-line @typescript-eslint/no-unused-vars _toResult(node, result, _blackboard) { return { ...(node.name ? { name: node.name } : {}), result }; } reset() { this.results = []; } get lastResult() { if (this.results.length === 0) { return null; } return this.results[this.results.length - 1]; } } exports.default = Introspector;