优化行为树代码

This commit is contained in:
dgflash
2025-09-15 09:57:14 +08:00
parent a47498dcc1
commit 32a11ae3b0
6 changed files with 31 additions and 31 deletions

View File

@@ -10,35 +10,35 @@ import { IControl } from './IControl';
export abstract class BTreeNode implements IControl {
protected _control!: IControl;
public title: string;
title: string;
public constructor() {
constructor() {
this.title = this.constructor.name;
}
public start(blackboard?: any) {
start(blackboard?: any) {
}
public end(blackboard?: any) {
end(blackboard?: any) {
}
public abstract run(blackboard?: any): void;
abstract run(blackboard?: any): void;
public setControl(control: IControl) {
setControl(control: IControl) {
this._control = control;
}
public running(blackboard?: any) {
running(blackboard?: any) {
this._control.running(this);
}
public success() {
success() {
this._control.success();
}
public fail() {
fail() {
this._control.fail();
}
}

View File

@@ -17,7 +17,7 @@ export class BehaviorTree implements IControl {
private _blackboard: any;
/** 是否已开始执行 */
public get started(): boolean {
get started(): boolean {
return this._started;
}
@@ -26,7 +26,7 @@ export class BehaviorTree implements IControl {
* @param node 根节点
* @param blackboard 外部参数对象
*/
public constructor(node: BTreeNode, blackboard?: any) {
constructor(node: BTreeNode, blackboard?: any) {
countUnnamed += 1;
this.title = node.constructor.name + '(btree_' + (countUnnamed) + ')';
this._root = node;
@@ -34,12 +34,12 @@ export class BehaviorTree implements IControl {
}
/** 设置行为逻辑中的共享数据 */
public setObject(blackboard: any) {
setObject(blackboard: any) {
this._blackboard = blackboard;
}
/** 执行行为树逻辑 */
public run() {
run() {
if (this._started) {
console.error(`行为树【${this.title}】未调用步骤,在最后一次调用步骤时有一个任务未完成`);
}
@@ -52,16 +52,16 @@ export class BehaviorTree implements IControl {
node.run(this._blackboard);
}
public running(node: BTreeNode) {
running(node: BTreeNode) {
this._started = false;
}
public success() {
success() {
this._current.end(this._blackboard);
this._started = false;
}
public fail() {
fail() {
this._current.end(this._blackboard);
this._started = false;
}

View File

@@ -10,7 +10,7 @@ import { BTreeNode } from './BTreeNode';
/** 复合节点 */
export abstract class BranchNode extends BTreeNode {
/** 子节点数组 */
public children: Array<BTreeNode>;
children: Array<BTreeNode>;
/** 当前任务索引 */
protected _actualTask!: number;
/** 正在运行的节点 */
@@ -19,17 +19,17 @@ export abstract class BranchNode extends BTreeNode {
/** 外部参数对象 */
protected _blackboard: any;
public constructor(nodes: Array<BTreeNode>) {
constructor(nodes: Array<BTreeNode>) {
super();
this.children = nodes || [];
}
public start() {
start() {
this._actualTask = 0;
super.start();
}
public run(blackboard?: any) {
run(blackboard?: any) {
if (this.children.length == 0) { // 没有子任务直接视为执行失败
this._control.fail();
}
@@ -53,17 +53,17 @@ export abstract class BranchNode extends BTreeNode {
node.run(this._blackboard);
}
public running(node: BTreeNode) {
running(node: BTreeNode) {
this._nodeRunning = node;
this._control.running(node);
}
public success() {
success() {
this._nodeRunning = null;
this._runningNode.end(this._blackboard);
}
public fail() {
fail() {
this._nodeRunning = null;
this._runningNode.end(this._blackboard);
}

View File

@@ -12,7 +12,7 @@ import { BTreeNode } from './BTreeNode';
* 如果装饰器是true 它所在的子树会被执行如果是false 所在的子树不会被执行
*/
export class Decorator extends BTreeNode {
public node!: BTreeNode;
node!: BTreeNode;
constructor(node?: string | BTreeNode) {
super()
@@ -25,17 +25,17 @@ export class Decorator extends BTreeNode {
this.node = BehaviorTree.getNode(node);
}
public start() {
start() {
this.node.setControl(this);
this.node.start();
super.start();
}
public end() {
end() {
this.node.end();
}
public run(blackboard: any) {
run(blackboard: any) {
this.node.run(blackboard);
}
}

View File

@@ -11,12 +11,12 @@ import { BranchNode } from './BranchNode';
* 只要子节点有一个返回true则停止执行其它子节点并且Selector返回true。如果所有子节点都返回false则Selector返回false。
*/
export class Selector extends BranchNode {
public success() {
success() {
super.success()
this._control.success();
}
public fail() {
fail() {
super.fail()
this._actualTask += 1;

View File

@@ -16,7 +16,7 @@ export class Sequence extends BranchNode {
super(nodes);
}
public success() {
success() {
super.success();
this._actualTask += 1;
@@ -28,7 +28,7 @@ export class Sequence extends BranchNode {
}
}
public fail() {
fail() {
super.fail();
this._control.fail();
}