mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
重构黑板数据结构
This commit is contained in:
@@ -4,67 +4,70 @@
|
||||
* @Description: 抽象节点基类
|
||||
*/
|
||||
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { IBlackboard } from "../Blackboard";
|
||||
import { BTNode, IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 可以包含多个节点的集合装饰器基类
|
||||
* 叶子节点 基类
|
||||
* 没有子节点
|
||||
*/
|
||||
export abstract class Composite extends BaseNode {
|
||||
constructor(...children: BaseNode[]) {
|
||||
super(children);
|
||||
export abstract class LeafNode extends BTNode {
|
||||
constructor() {
|
||||
super([]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修饰节点基类
|
||||
* 只能包含一个子节点
|
||||
* 修饰节点 基类
|
||||
* 有且仅有一个子节点
|
||||
*/
|
||||
export abstract class Decorator extends BaseNode {
|
||||
constructor(child: BaseNode) {
|
||||
export abstract class Decorator extends BTNode {
|
||||
constructor(child: IBTNode) {
|
||||
super([child]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值型装饰节点基类
|
||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的装饰节点
|
||||
* 组合节点 基类
|
||||
* 多个子节点
|
||||
*/
|
||||
export abstract class Composite extends BTNode {
|
||||
constructor(...children: IBTNode[]) {
|
||||
super(children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值型修饰节点 基类
|
||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的修饰节点
|
||||
*/
|
||||
export abstract class NumericDecorator extends Decorator {
|
||||
protected readonly _max: number;
|
||||
protected _value: number = 0;
|
||||
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆装饰节点基类
|
||||
* 记忆修饰节点基类
|
||||
* 只有记忆节点才需要设置局部数据
|
||||
*/
|
||||
export abstract class MemoryComposite extends Composite {
|
||||
protected runningIndex = 0;
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
// 检查是否需要重置记忆
|
||||
const shouldReset = tree.blackboard.get(`reset_memory`, this);
|
||||
if (shouldReset) {
|
||||
this.runningIndex = 0;
|
||||
tree.blackboard.delete(`reset_memory`, this);
|
||||
}
|
||||
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
|
||||
super._initialize(global, branch);
|
||||
this._local = branch.createChild();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置记忆状态,下次执行时将从第一个子节点开始
|
||||
*/
|
||||
public resetMemory(): void {
|
||||
this.runningIndex = 0;
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this.set(`__nMemoryRunningIndex`, 0);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { LeafNode } from "./AbstractNodes";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
export class Action extends BaseNode {
|
||||
protected _func: (subject?: any) => Status;
|
||||
constructor(func: (subject?: any) => Status) {
|
||||
export class Action extends LeafNode {
|
||||
protected _func: (node: IBTNode) => Status;
|
||||
constructor(func: (node: IBTNode) => Status) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
||||
public tick(): Status {
|
||||
return this._func?.(this) ?? Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export class Action extends BaseNode {
|
||||
* 次数内,返回RUNNING
|
||||
* 超次,返回SUCCESS
|
||||
*/
|
||||
export class WaitTicks extends BaseNode {
|
||||
export class WaitTicks extends LeafNode {
|
||||
private _max: number;
|
||||
private _value: number;
|
||||
|
||||
@@ -29,12 +29,12 @@ export class WaitTicks extends BaseNode {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
if (++this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ export class WaitTicks extends BaseNode {
|
||||
* 时间等待节点 时间(秒)
|
||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||
*/
|
||||
export class WaitTime extends BaseNode {
|
||||
export class WaitTime extends LeafNode {
|
||||
private _max: number;
|
||||
private _value: number = 0;
|
||||
constructor(duration: number = 0) {
|
||||
@@ -54,12 +54,12 @@ export class WaitTime extends BaseNode {
|
||||
this._max = duration * 1000;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = new Date().getTime();
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
const currTime = new Date().getTime();
|
||||
if (currTime - this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
|
||||
169
src/behaviortree/BTNode/BTNode.ts
Normal file
169
src/behaviortree/BTNode/BTNode.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { globalBlackboard, IBlackboard } from "../Blackboard";
|
||||
import { Status } from "../header";
|
||||
|
||||
export interface IBTNode {
|
||||
readonly children: IBTNode[];
|
||||
/** 本节点的的黑板引用 */
|
||||
blackboard: IBlackboard;
|
||||
/**
|
||||
* 初始化节点
|
||||
* @param root 树根节点的黑板
|
||||
* @param parent 父节点的黑板
|
||||
*/
|
||||
_initialize(root: IBlackboard, parent: IBlackboard): void;
|
||||
|
||||
_execute(): Status;
|
||||
tick(): Status;
|
||||
cleanupAll(): void;
|
||||
|
||||
/**
|
||||
* 优先写入自己的黑板数据, 如果没有则写入父节点的黑板数据
|
||||
*/
|
||||
set<T>(key: string, value: T): void;
|
||||
get<T>(key: string): T;
|
||||
|
||||
/**
|
||||
* 写入树根节点的黑板数据
|
||||
*/
|
||||
setRoot<T>(key: string, value: T): void;
|
||||
getRoot<T>(key: string): T;
|
||||
|
||||
/**
|
||||
* 写入全局黑板数据
|
||||
*/
|
||||
setGlobal<T>(key: string, value: T): void;
|
||||
getGlobal<T>(key: string): T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BTNode implements IBTNode {
|
||||
public readonly children: IBTNode[];
|
||||
|
||||
/** 树根节点的黑板引用 */
|
||||
protected _root!: IBlackboard;
|
||||
/** 本节点的的黑板引用 可能等于 _parent */
|
||||
protected _local!: IBlackboard;
|
||||
|
||||
private _isRunning: boolean;
|
||||
/**
|
||||
* 创建
|
||||
* @param children 子节点列表
|
||||
*/
|
||||
constructor(children?: IBTNode[]) {
|
||||
this.children = children ? [...children] : [];
|
||||
this._isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开节点
|
||||
* @param tree 行为树
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public _initialize(root: IBlackboard, parent: IBlackboard): void {
|
||||
this._root = root;
|
||||
// 在需要的节点中重写,创建新的local
|
||||
this._local = parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点
|
||||
* @internal
|
||||
*/
|
||||
public _execute(): Status {
|
||||
// 首次执行时初始化
|
||||
if (!this._isRunning) {
|
||||
this._isRunning = true;
|
||||
this.open();
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick();
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._isRunning = false;
|
||||
this.close();
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
*/
|
||||
protected open(): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick(): Status;
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
*/
|
||||
protected close(): void { }
|
||||
|
||||
/**
|
||||
* 递归清理节点及其所有子节点的状态
|
||||
* 用于行为树中断时清理所有节点状态
|
||||
*/
|
||||
public cleanupAll(): void {
|
||||
// 清理基础状态
|
||||
this._isRunning = false;
|
||||
|
||||
// 递归清理所有子节点
|
||||
for (const child of this.children) {
|
||||
child.cleanupAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取全局黑板数据
|
||||
*/
|
||||
public set<T>(key: string, value: T): void {
|
||||
this._local.set(key, value);
|
||||
}
|
||||
|
||||
public get<T>(key: string): T {
|
||||
return this._local.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取树根节点的黑板数据
|
||||
*/
|
||||
public setRoot<T>(key: string, value: T): void {
|
||||
this._root.set(key, value);
|
||||
}
|
||||
|
||||
public getRoot<T>(key: string): T {
|
||||
return this._root.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局黑板数据
|
||||
*/
|
||||
public setGlobal<T>(key: string, value: T): void {
|
||||
globalBlackboard.set(key, value);
|
||||
}
|
||||
|
||||
public getGlobal<T>(key: string): T {
|
||||
return globalBlackboard.get(key);
|
||||
}
|
||||
|
||||
public get local(): IBlackboard {
|
||||
return this._local;
|
||||
}
|
||||
|
||||
public get blackboard(): IBlackboard {
|
||||
return this._local;
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BaseNode {
|
||||
public readonly children: BaseNode[];
|
||||
private _id: string;
|
||||
private _isRunning: boolean;
|
||||
|
||||
set id(id: string) { this._id = id; }
|
||||
get id(): string { return this._id }
|
||||
|
||||
/**
|
||||
* 创建
|
||||
* @param children 子节点列表
|
||||
*/
|
||||
constructor(children?: BaseNode[]) {
|
||||
this._id = ""; // 临时值,将在树构造时被正确设置
|
||||
this.children = children ? [...children] : [];
|
||||
this._isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点
|
||||
* @param tree 行为树
|
||||
* @returns 状态
|
||||
*/
|
||||
public _execute<T>(tree: BehaviorTree<T>): Status {
|
||||
// 首次执行时初始化
|
||||
if (!this._isRunning) {
|
||||
this._isRunning = true;
|
||||
this.initialize(tree);
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick(tree);
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._isRunning = false;
|
||||
this.cleanup(tree);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected initialize<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected cleanup<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @param tree 行为树
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick<T>(tree: BehaviorTree<T>): Status;
|
||||
|
||||
/**
|
||||
* 递归清理节点及其所有子节点的状态
|
||||
* 用于行为树中断时清理所有节点状态
|
||||
*/
|
||||
public cleanupAll(): void {
|
||||
// 清理基础状态
|
||||
this._isRunning = false;
|
||||
|
||||
// 递归清理所有子节点
|
||||
for (const child of this.children) {
|
||||
child.cleanupAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
|
||||
@@ -8,12 +7,13 @@ import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
||||
*/
|
||||
export class MemSelector extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
public tick(): Status {
|
||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.FAILURE) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -30,12 +30,13 @@ export class MemSelector extends MemoryComposite {
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class MemSequence extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
public tick(): Status {
|
||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.SUCCESS) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -49,13 +50,13 @@ export class MemSequence extends MemoryComposite {
|
||||
* 从Child Node中随机选择一个执行
|
||||
*/
|
||||
export class RandomSelector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
if (this.children.length === 0) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
const childIndex = Math.floor(Math.random() * this.children.length);
|
||||
const status = this.children[childIndex]!._execute(tree);
|
||||
const status = this.children[childIndex]!._execute();
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -66,9 +67,9 @@ export class RandomSelector extends Composite {
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
||||
*/
|
||||
export class Selector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.FAILURE) {
|
||||
return status;
|
||||
}
|
||||
@@ -84,9 +85,9 @@ export class Selector extends Composite {
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class Sequence extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@@ -96,21 +97,24 @@ export class Sequence extends Composite {
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 并行节点 每次进入全部执行一遍
|
||||
* 它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 任意子节点返回 FAILURE, 返回 FAILURE
|
||||
* 2. 否则 任意子节点返回 RUNNING, 返回 RUNNING
|
||||
* 3. 全部成功, 才返回 SUCCESS
|
||||
*/
|
||||
export class Parallel extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
let result = Status.SUCCESS;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (result === Status.FAILURE || status === Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -119,20 +123,23 @@ export class Parallel extends Composite {
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS
|
||||
* 它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 任意子节点返回 SUCCESS, 返回 SUCCESS
|
||||
* 2. 否则, 任意子节点返回 FAILURE, 返回 FAILURE
|
||||
* 否则返回 RUNNING
|
||||
*/
|
||||
export class ParallelAnySuccess extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
let result = Status.RUNNING;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
||||
result = Status.SUCCESS;
|
||||
continue;
|
||||
}
|
||||
if (status === Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { LeafNode } from "./AbstractNodes";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 条件节点
|
||||
* 根据条件函数返回SUCCESS或FAILURE
|
||||
*/
|
||||
export class Condition extends BaseNode {
|
||||
export class Condition extends LeafNode {
|
||||
/** 执行函数 @internal */
|
||||
private readonly _func: (subject: any) => boolean;
|
||||
constructor(func: (subject: any) => boolean) {
|
||||
private readonly _func: (node: IBTNode) => boolean;
|
||||
constructor(func: (node: IBTNode) => boolean) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ? Status.SUCCESS : Status.FAILURE;
|
||||
public tick(): Status {
|
||||
return this._func?.(this) ? Status.SUCCESS : Status.FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,9 @@
|
||||
* @Description: 装饰节点 装饰节点下必须包含子节点
|
||||
*/
|
||||
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 结果反转节点
|
||||
@@ -16,8 +15,8 @@ import { BaseNode } from "./BaseNode";
|
||||
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
||||
*/
|
||||
export class Inverter extends Decorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
public tick(): Status {
|
||||
const status = this.children[0]!._execute();
|
||||
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.FAILURE;
|
||||
@@ -41,44 +40,35 @@ export class LimitTime extends NumericDecorator {
|
||||
* @param child 子节点
|
||||
* @param max 最大时间 (秒) 默认1秒
|
||||
*/
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child, max * 1000);
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
this._value = Date.now();
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
const currentTime = Date.now();
|
||||
|
||||
if (currentTime - this._value > this._max) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
return this.children[0]!._execute(tree);
|
||||
return this.children[0]!._execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 次数限制节点
|
||||
* 必须且只能包含一个子节点
|
||||
* 次数限制内, 返回子节点的状态, 次数达到后, 直接返回失败
|
||||
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
||||
*/
|
||||
export class LimitTimes extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
if (this._value >= this._max) {
|
||||
export class LimitTicks extends NumericDecorator {
|
||||
public tick(): Status {
|
||||
this._value++;
|
||||
if (this._value > this._max) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
const status = this.children[0]!._execute(tree);
|
||||
if (status !== Status.RUNNING) {
|
||||
this._value++;
|
||||
if (this._value < this._max) {
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
return this.children[0]!._execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +79,9 @@ export class LimitTimes extends NumericDecorator {
|
||||
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
||||
*/
|
||||
export class Repeat extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute();
|
||||
// 如果子节点完成(成功或失败),增加计数
|
||||
if (status === Status.SUCCESS || status === Status.FAILURE) {
|
||||
this._value++;
|
||||
@@ -112,8 +102,8 @@ export class Repeat extends NumericDecorator {
|
||||
* 子节点成功 计数+1
|
||||
*/
|
||||
export class RepeatUntilFailure extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
public tick(): Status {
|
||||
const status = this.children[0]!._execute();
|
||||
if (status === Status.FAILURE) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
@@ -136,9 +126,9 @@ export class RepeatUntilFailure extends NumericDecorator {
|
||||
* 子节点失败, 计数+1
|
||||
*/
|
||||
export class RepeatUntilSuccess extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute();
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user