blueshell
Version:
A Behavior Tree implementation in modern Javascript
83 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parent = void 0;
exports.setEventCounter = setEventCounter;
exports.clearEventSeenRecursive = clearEventSeenRecursive;
const Base_1 = require("./Base");
const models_1 = require("../models");
/**
* Sets the event counter of the nocde and any child nodes to indicate
* the node has been visited for this event
* @param pStorage Private node storage containing current eventCounter
* @param state The state holding the node storage
* @param node The node to set
*/
function setEventCounter(pStorage, state, node) {
const nodeStorage = node.getNodeStorage(state);
if (nodeStorage.lastEventSeen !== undefined) {
nodeStorage.lastEventSeen = pStorage.eventCounter;
if ((0, models_1.isParentNode)(node)) {
node.getChildren().forEach((child) => {
setEventCounter(pStorage, state, child);
});
}
}
}
/**
* Clears the last event seen property of node and all of node's children
* @param node The node to clear
* @param state The state holding the node storage
*/
function clearEventSeenRecursive(node, state) {
if ((0, models_1.isParentNode)(node)) {
node.getChildren().forEach((child) => {
clearEventSeenRecursive(child, state);
});
}
const nodeStorage = node.getNodeStorage(state);
nodeStorage.lastEventSeen = undefined;
}
/**
* Base class for all nodes that expose a list of children.
* Primarily used to support the visualization of the behavior tree
* using dotTree or archyTree
*
* @author Mark Asdoorian
*/
class Parent extends Base_1.Action {
/**
* @constructor
* @param name
*/
constructor(name) {
super(name);
}
/**
* Initializes the parent of our list of children. Only required to be called if you know your children
* at construction time. If your children are dynamic, then you are responsible for setting their parent
* properly when they are created.
* Note: this repeats code from set parent() because at construction time, this.children will not return
* anything (this isn't this yet), which is why we have to pass children in as a parameter
* @param children list of children to init (can't call getChildren() yet because this is called from the constructor)
*/
initChildren(children) {
for (const child of children) {
child.parent = this.name;
}
}
/**
* Sets the parent of this Node, and all children Nodes.
* @override
*/
set parent(parent) {
super.parent = parent;
for (const child of this.getChildren()) {
child.parent = this.path;
}
}
get parent() {
return super.parent;
}
}
exports.Parent = Parent;
//# sourceMappingURL=Parent.js.map