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

142 lines
4.1 KiB
TypeScript
Raw Normal View History

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-04 14:08:19 +08:00
*
* FAILURE
* SUCCESS SUCCESS
*
* RUNNING RUNNING
2025-06-04 23:10:59 +08:00
*/
2025-09-02 17:05:46 +08:00
export class MemSelector extends MemoryComposite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
let index = this.get<number>(`__nMemoryRunningIndex`);
for (let i = index; i < this.children.length; i++) {
let status = this.children[i]!._execute();
2025-09-04 14:08:19 +08:00
if (status === Status.FAILURE) {
continue;
}
if (status === Status.SUCCESS) {
2025-06-04 23:10:59 +08:00
return status;
}
2025-09-04 14:08:19 +08:00
this.set(`__nMemoryRunningIndex`, i);
return Status.RUNNING;
2025-06-04 23:10:59 +08:00
}
return Status.FAILURE;
}
}
/**
2025-09-04 14:08:19 +08:00
*
* SUCCESS
* FAILURE FAILURE
*
* RUNNING RUNNING
2025-06-04 23:10:59 +08:00
*/
2025-09-02 17:05:46 +08:00
export class MemSequence extends MemoryComposite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
let index = this.get<number>(`__nMemoryRunningIndex`);
for (let i = index; i < this.children.length; i++) {
let status = this.children[i]!._execute();
2025-09-04 14:08:19 +08:00
if (status === Status.SUCCESS) {
continue;
}
if (status === Status.FAILURE) {
return Status.FAILURE;
2025-06-04 23:10:59 +08:00
}
2025-09-04 14:08:19 +08:00
this.set(`__nMemoryRunningIndex`, i);
return Status.RUNNING;
2025-06-04 23:10:59 +08:00
}
return Status.SUCCESS;
}
}
/**
*
2025-09-04 14:08:19 +08:00
*
*
2025-06-04 23:10:59 +08:00
*/
export class RandomSelector extends Composite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
2025-09-02 17:05:46 +08:00
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);
2025-09-03 10:54:07 +08:00
const status = this.children[childIndex]!._execute();
2025-06-04 23:10:59 +08:00
return status;
}
}
/**
2025-09-04 14:08:19 +08:00
*
* FAILURE
* FAILURE
2025-06-04 23:10:59 +08:00
*/
export class Selector extends Composite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
2025-06-04 23:10:59 +08:00
for (let i = 0; i < this.children.length; i++) {
2025-09-03 10:54:07 +08:00
let status = this.children[i]!._execute();
2025-06-04 23:10:59 +08:00
if (status !== Status.FAILURE) {
return status;
}
}
return Status.FAILURE;
}
}
/**
2025-09-04 14:08:19 +08:00
*
* SUCCESS
*
2025-06-04 23:10:59 +08:00
*/
export class Sequence extends Composite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
2025-06-04 23:10:59 +08:00
for (let i = 0; i < this.children.length; i++) {
2025-09-03 10:54:07 +08:00
let status = this.children[i]!._execute();
2025-09-04 14:08:19 +08:00
if (status === Status.SUCCESS) {
continue;
2025-06-04 23:10:59 +08:00
}
2025-09-04 14:08:19 +08:00
return status;
2025-06-04 23:10:59 +08:00
}
return Status.SUCCESS;
}
}
/**
2025-09-04 14:08:19 +08:00
*
* FAILURE > RUNNING > SUCCESS
2025-06-04 23:10:59 +08:00
*/
export class Parallel extends Composite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
2025-06-04 23:10:59 +08:00
let result = Status.SUCCESS;
for (let i = 0; i < this.children.length; i++) {
2025-09-03 10:54:07 +08:00
let status = this.children[i]!._execute();
if (result === Status.FAILURE || status === Status.FAILURE) {
2025-06-04 23:10:59 +08:00
result = Status.FAILURE;
2025-09-04 14:08:19 +08:00
} else if (status === Status.RUNNING) {
2025-06-04 23:10:59 +08:00
result = Status.RUNNING;
}
}
return result;
}
}
/**
2025-09-04 14:08:19 +08:00
*
* SUCCESS > RUNNING > FAILURE
2025-06-04 23:10:59 +08:00
*/
export class ParallelAnySuccess extends Composite {
2025-09-03 10:54:07 +08:00
public tick(): Status {
2025-09-04 14:08:19 +08:00
let result = Status.FAILURE;
2025-06-04 23:10:59 +08:00
for (let i = 0; i < this.children.length; i++) {
2025-09-03 10:54:07 +08:00
let status = this.children[i]!._execute();
if (result === Status.SUCCESS || status === Status.SUCCESS) {
2025-06-04 23:10:59 +08:00
result = Status.SUCCESS;
2025-09-04 14:08:19 +08:00
} else if (status === Status.RUNNING) {
result = Status.RUNNING;
2025-06-04 23:10:59 +08:00
}
}
return result;
}
}