Files
kunpocc-behaviortree/src/behaviortree/BTNode/Composite.ts

140 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-09-02 17:05:46 +08:00
import type { BehaviorTree } from "../BehaviorTree";
2025-06-04 23:10:59 +08:00
import { Status } from "../header";
2025-09-02 17:05:46 +08:00
import { Composite, MemoryComposite } from "./AbstractNodes";
2025-06-04 23:10:59 +08:00
/**
*
2025-09-02 17:05:46 +08:00
* FAILURE
2025-06-04 23:10:59 +08:00
* Child Node返回不为 FAILURE, Node向自己的Parent Node也返回Child Node状态
*/
2025-09-02 17:05:46 +08:00
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);
2025-06-04 23:10:59 +08:00
if (status !== Status.FAILURE) {
if (status === Status.RUNNING) {
2025-09-02 17:05:46 +08:00
this.runningIndex = i;
2025-06-04 23:10:59 +08:00
}
return status;
}
}
return Status.FAILURE;
}
}
/**
*
2025-09-02 17:05:46 +08:00
* RUNNING , , RUNNING
* SUCCESS FAILURE
2025-06-04 23:10:59 +08:00
* Child Node返回不为 SUCCESS, Node向自己的Parent Node也返回Child Node状态
* SUCCESS, SUCCESS
*/
2025-09-02 17:05:46 +08:00
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);
2025-06-04 23:10:59 +08:00
if (status !== Status.SUCCESS) {
if (status === Status.RUNNING) {
2025-09-02 17:05:46 +08:00
this.runningIndex = i;
2025-06-04 23:10:59 +08:00
}
return status;
}
}
return Status.SUCCESS;
}
}
/**
*
* Child Node中随机选择一个执行
*/
export class RandomSelector extends Composite {
2025-09-02 17:05:46 +08:00
public tick<T>(tree: BehaviorTree<T>): Status {
if (this.children.length === 0) {
return Status.FAILURE;
}
2025-06-04 23:10:59 +08:00
2025-09-02 17:05:46 +08:00
const childIndex = Math.floor(Math.random() * this.children.length);
const status = this.children[childIndex]!._execute(tree);
2025-06-04 23:10:59 +08:00
return status;
}
}
/**
* FAILURE
2025-09-02 17:05:46 +08:00
* Node时begin到end迭代执行自己的Child Node
* Child Node执行后返回 SUCCESS RUNNINGNode向自己的Parent Node也返回 SUCCESS RUNNING
2025-06-04 23:10:59 +08:00
*/
export class Selector extends Composite {
2025-09-02 17:05:46 +08:00
public tick<T>(tree: BehaviorTree<T>): Status {
2025-06-04 23:10:59 +08:00
for (let i = 0; i < this.children.length; i++) {
2025-09-02 17:05:46 +08:00
let status = this.children[i]!._execute(tree);
2025-06-04 23:10:59 +08:00
if (status !== Status.FAILURE) {
return status;
}
}
return Status.FAILURE;
}
}
/**
*
* Node时begin到end迭代执行自己的Child Node
2025-09-02 17:05:46 +08:00
* FAILURE RUNNING, FAILURE RUNNING
2025-06-04 23:10:59 +08:00
* SUCCESS, SUCCESS
*/
export class Sequence extends Composite {
2025-09-02 17:05:46 +08:00
public tick<T>(tree: BehaviorTree<T>): Status {
2025-06-04 23:10:59 +08:00
for (let i = 0; i < this.children.length; i++) {
2025-09-02 17:05:46 +08:00
let status = this.children[i]!._execute(tree);
2025-06-04 23:10:59 +08:00
if (status !== Status.SUCCESS) {
return status;
}
}
return Status.SUCCESS;
}
}
/**
*
* Node时begin到end迭代执行自己的Child Node
* 1. Child Node执行后返回 FAILURE, FAILURE
2025-09-02 17:05:46 +08:00
* 2. Child Node执行后返回 RUNNING, RUNNING
2025-06-04 23:10:59 +08:00
* SUCCESS, SUCCESS
*/
export class Parallel extends Composite {
2025-09-02 17:05:46 +08:00
public tick<T>(tree: BehaviorTree<T>): Status {
2025-06-04 23:10:59 +08:00
let result = Status.SUCCESS;
for (let i = 0; i < this.children.length; i++) {
2025-09-02 17:05:46 +08:00
let status = this.children[i]!._execute(tree);
2025-06-04 23:10:59 +08:00
if (status == Status.FAILURE) {
result = Status.FAILURE;
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
result = Status.RUNNING;
}
}
return result;
}
}
/**
*
* Node时begin到end迭代执行自己的Child Node
* 1. Child Node执行后返回 FAILURE, FAILURE
* 2. Child Node SUCCESS, SUCCESS
* RUNNING
*/
export class ParallelAnySuccess extends Composite {
2025-09-02 17:05:46 +08:00
public tick<T>(tree: BehaviorTree<T>): Status {
2025-06-04 23:10:59 +08:00
let result = Status.RUNNING;
for (let i = 0; i < this.children.length; i++) {
2025-09-02 17:05:46 +08:00
let status = this.children[i]!._execute(tree);
2025-06-04 23:10:59 +08:00
if (status == Status.FAILURE) {
result = Status.FAILURE;
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
result = Status.SUCCESS;
}
}
return result;
}
}