mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-26 16:48:56 +00:00
项目重构,破坏性更新
This commit is contained in:
@@ -1,84 +1,41 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Ticker } from "../Ticker";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
|
||||
/**
|
||||
* 可以包含多个节点的集合装饰器基类
|
||||
*
|
||||
*/
|
||||
export abstract class Composite extends BaseNode {
|
||||
constructor(...children: BaseNode[]) {
|
||||
super(children);
|
||||
}
|
||||
}
|
||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
|
||||
/**
|
||||
* 记忆选择节点
|
||||
* 选择不为 FAILURE 的节点
|
||||
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
|
||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
||||
*/
|
||||
export class MemSelector extends Composite {
|
||||
/**
|
||||
* 打开
|
||||
* @param {Ticker} ticker
|
||||
*/
|
||||
public open(ticker: Ticker): void {
|
||||
super.open(ticker);
|
||||
ticker.blackboard.set("runningChild", 0, ticker.tree.id, this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
let childIndex = ticker.blackboard.get("runningChild", ticker.tree.id, this.id) as number;
|
||||
|
||||
for (let i = childIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
|
||||
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);
|
||||
if (status !== Status.FAILURE) {
|
||||
if (status === Status.RUNNING) {
|
||||
ticker.blackboard.set("runningChild", i, ticker.tree.id, this.id);
|
||||
this.runningIndex = i;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return Status.FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆顺序节点
|
||||
* 如果上次执行到 RUNING 的节点, 下次进入节点后, 直接从 RUNING 节点开始
|
||||
* 遇到 RUNING 或者 FAILURE 停止迭代
|
||||
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
|
||||
* 遇到 SUCCESS 或者 FAILURE 停止迭代
|
||||
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class MemSequence extends Composite {
|
||||
/**
|
||||
* 打开
|
||||
* @param {Ticker} ticker
|
||||
*/
|
||||
public open(ticker: Ticker): void {
|
||||
super.open(ticker);
|
||||
ticker.blackboard.set("runningChild", 0, ticker.tree.id, this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
let childIndex = ticker.blackboard.get("runningChild", ticker.tree.id, this.id) as number;
|
||||
for (let i = childIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
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);
|
||||
if (status !== Status.SUCCESS) {
|
||||
if (status === Status.RUNNING) {
|
||||
ticker.blackboard.set("runningChild", i, ticker.tree.id, this.id);
|
||||
this.runningIndex = i;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -92,34 +49,26 @@ export class MemSequence extends Composite {
|
||||
* 从Child Node中随机选择一个执行
|
||||
*/
|
||||
export class RandomSelector extends Composite {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
let childIndex = (Math.random() * this.children.length) | 0;
|
||||
let child = this.children[childIndex];
|
||||
let status = child._execute(ticker);
|
||||
public tick<T>(tree: BehaviorTree<T>): 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);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择节点,选择不为 FAILURE 的节点
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNING
|
||||
* 当执行本Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
||||
*/
|
||||
export class Selector extends Composite {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.FAILURE) {
|
||||
return status;
|
||||
}
|
||||
@@ -131,18 +80,13 @@ export class Selector extends Composite {
|
||||
/**
|
||||
* 顺序节点
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 遇到 FAILURE 或 RUNING, 那停止迭代,返回FAILURE 或 RUNING
|
||||
* 遇到 FAILURE 或 RUNNING, 那停止迭代,返回FAILURE 或 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class Sequence extends Composite {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@@ -155,19 +99,14 @@ export class Sequence extends Composite {
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 当存在Child Node执行后返回 RUNING, 本节点返回 RUNING
|
||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class Parallel extends Composite {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
let result = Status.SUCCESS;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
|
||||
@@ -186,15 +125,10 @@ export class Parallel extends Composite {
|
||||
* 否则返回 RUNNING
|
||||
*/
|
||||
export class ParallelAnySuccess extends Composite {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
let result = Status.RUNNING;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]._execute(ticker);
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
|
||||
|
||||
Reference in New Issue
Block a user