mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-26 16:48:56 +00:00
重构黑板数据结构
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user