mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-26 16:48:56 +00:00
添加装饰器给蓝图编辑器做准备;修改随机节点添加权重支持
This commit is contained in:
157
src/behaviortree/BT.ts
Normal file
157
src/behaviortree/BT.ts
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
/**
|
||||||
|
* 行为树装饰器和元数据管理
|
||||||
|
* 用于编辑器显示和配置节点信息
|
||||||
|
*/
|
||||||
|
|
||||||
|
export namespace BT {
|
||||||
|
/**
|
||||||
|
* 参数类型枚举
|
||||||
|
*/
|
||||||
|
export enum ParamType {
|
||||||
|
int = "number",
|
||||||
|
float = "float",
|
||||||
|
string = "string",
|
||||||
|
bool = "boolean"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 节点类型枚举
|
||||||
|
*/
|
||||||
|
export enum Type {
|
||||||
|
/** 行为节点 */
|
||||||
|
Action = "action",
|
||||||
|
/** 条件节点 */
|
||||||
|
Condition = "condition",
|
||||||
|
/** 组合节点 */
|
||||||
|
Composite = "composite",
|
||||||
|
/** 装饰节点 */
|
||||||
|
Decorator = "decorator"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 参数描述接口
|
||||||
|
*/
|
||||||
|
export interface ParameterInfo {
|
||||||
|
/** 参数类型 */
|
||||||
|
type: ParamType;
|
||||||
|
/** 参数名称 */
|
||||||
|
name: string;
|
||||||
|
/** 参数描述 */
|
||||||
|
description?: string;
|
||||||
|
/** 默认值 */
|
||||||
|
defaultValue?: any;
|
||||||
|
/** 是否必需 */
|
||||||
|
required?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 节点元数据接口
|
||||||
|
*/
|
||||||
|
export interface NodeMetadata {
|
||||||
|
/** 节点名称 */
|
||||||
|
name: string;
|
||||||
|
/** 节点分组 */
|
||||||
|
group: string;
|
||||||
|
/** 节点类型 */
|
||||||
|
type: Type;
|
||||||
|
/** 节点描述 */
|
||||||
|
description: string;
|
||||||
|
/** 参数列表 */
|
||||||
|
parameters: ParameterInfo[];
|
||||||
|
/** 最大子节点数量:0=不允许子节点,1=最多一个子节点,-1=无限制 */
|
||||||
|
maxChildren: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 节点元数据存储
|
||||||
|
*/
|
||||||
|
const NODE_METADATA_MAP = new Map<string, NodeMetadata>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 行为节点装饰器
|
||||||
|
*/
|
||||||
|
export function ActionNode(metadata: Omit<NodeMetadata, "type" | "maxChildren">) {
|
||||||
|
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||||
|
const fullMetadata: NodeMetadata = {
|
||||||
|
...metadata,
|
||||||
|
type: Type.Action,
|
||||||
|
maxChildren: 0,
|
||||||
|
};
|
||||||
|
NODE_METADATA_MAP.set(constructor.name, fullMetadata);
|
||||||
|
return constructor;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件节点装饰器
|
||||||
|
*/
|
||||||
|
export function ConditionNode(metadata: Omit<NodeMetadata, "type" | "maxChildren">) {
|
||||||
|
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||||
|
const fullMetadata: NodeMetadata = {
|
||||||
|
...metadata,
|
||||||
|
type: Type.Condition,
|
||||||
|
maxChildren: 0,
|
||||||
|
};
|
||||||
|
NODE_METADATA_MAP.set(constructor.name, fullMetadata);
|
||||||
|
return constructor;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组合节点装饰器
|
||||||
|
*/
|
||||||
|
export function CompositeNode(metadata: Omit<NodeMetadata, "type" | "maxChildren">) {
|
||||||
|
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||||
|
const fullMetadata: NodeMetadata = {
|
||||||
|
...metadata,
|
||||||
|
type: Type.Composite,
|
||||||
|
maxChildren: -1
|
||||||
|
};
|
||||||
|
NODE_METADATA_MAP.set(constructor.name, fullMetadata);
|
||||||
|
return constructor;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 装饰节点装饰器
|
||||||
|
*/
|
||||||
|
export function DecoratorNode(metadata: Omit<NodeMetadata, "type" | "maxChildren">) {
|
||||||
|
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||||
|
const fullMetadata: NodeMetadata = {
|
||||||
|
...metadata,
|
||||||
|
type: Type.Decorator,
|
||||||
|
maxChildren: 1
|
||||||
|
};
|
||||||
|
NODE_METADATA_MAP.set(constructor.name, fullMetadata);
|
||||||
|
return constructor;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取节点元数据
|
||||||
|
*/
|
||||||
|
export function getNodeMetadata(nodeName: string): NodeMetadata | undefined {
|
||||||
|
return NODE_METADATA_MAP.get(nodeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有节点元数据
|
||||||
|
*/
|
||||||
|
export function getAllNodeMetadata(): Map<string, NodeMetadata> {
|
||||||
|
return new Map(NODE_METADATA_MAP);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断节点是否允许子节点
|
||||||
|
*/
|
||||||
|
export function canHaveChildren(metadata: NodeMetadata): boolean {
|
||||||
|
return metadata.maxChildren !== 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断节点是否可以添加更多子节点
|
||||||
|
*/
|
||||||
|
export function canAddMoreChildren(metadata: NodeMetadata, currentChildCount: number): boolean {
|
||||||
|
return metadata.maxChildren === -1 || currentChildCount < metadata.maxChildren;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +1,20 @@
|
|||||||
|
import { BT } from "../BT";
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { LeafNode } from "./AbstractNodes";
|
import { LeafNode } from "./AbstractNodes";
|
||||||
import { IBTNode } from "./BTNode";
|
|
||||||
|
|
||||||
export class Action extends LeafNode {
|
|
||||||
protected _func: (node: IBTNode) => Status;
|
|
||||||
constructor(func: (node: IBTNode) => Status) {
|
|
||||||
super();
|
|
||||||
this._func = func;
|
|
||||||
}
|
|
||||||
|
|
||||||
public tick(): Status {
|
|
||||||
return this._func?.(this) ?? Status.SUCCESS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 次数等待节点(无子节点)
|
* 次数等待节点(无子节点)
|
||||||
* 次数内,返回RUNNING
|
* 次数内,返回RUNNING
|
||||||
* 超次,返回SUCCESS
|
* 超次,返回SUCCESS
|
||||||
*/
|
*/
|
||||||
|
@BT.ActionNode({
|
||||||
|
name: "等待次数",
|
||||||
|
group: "等待行为",
|
||||||
|
description: "等待指定次数后返回成功",
|
||||||
|
parameters: [
|
||||||
|
{ name: "maxTicks", type: BT.ParamType.int, description: "最大等待次数", defaultValue: 0, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class WaitTicks extends LeafNode {
|
export class WaitTicks extends LeafNode {
|
||||||
private _max: number;
|
private _max: number;
|
||||||
private _value: number;
|
private _value: number;
|
||||||
@@ -46,6 +42,14 @@ export class WaitTicks extends LeafNode {
|
|||||||
* 时间等待节点 时间(秒)
|
* 时间等待节点 时间(秒)
|
||||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||||
*/
|
*/
|
||||||
|
@BT.ActionNode({
|
||||||
|
name: "等待时间",
|
||||||
|
group: "等待行为",
|
||||||
|
description: "等待指定时间(秒)后返回成功",
|
||||||
|
parameters: [
|
||||||
|
{ name: "duration", type: BT.ParamType.float, description: "等待时间(秒)", defaultValue: 0, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class WaitTime extends LeafNode {
|
export class WaitTime extends LeafNode {
|
||||||
private _max: number;
|
private _max: number;
|
||||||
private _value: number = 0;
|
private _value: number = 0;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import { BT } from "../BT";
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||||
|
import { IBTNode } from "./BTNode";
|
||||||
|
import { WeightDecorator } from "./Decorator";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记忆选择节点 从上到下执行
|
* 记忆选择节点 从上到下执行
|
||||||
@@ -8,6 +11,12 @@ import { Composite, MemoryComposite } from "./AbstractNodes";
|
|||||||
*
|
*
|
||||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "记忆选择节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "记住上次运行位置的选择节点,从记忆位置开始执行",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class MemSelector extends MemoryComposite {
|
export class MemSelector extends MemoryComposite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
@@ -33,6 +42,12 @@ export class MemSelector extends MemoryComposite {
|
|||||||
*
|
*
|
||||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "记忆顺序节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "记住上次运行位置的序列节点,从记忆位置开始执行",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class MemSequence extends MemoryComposite {
|
export class MemSequence extends MemoryComposite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
@@ -51,28 +66,17 @@ export class MemSequence extends MemoryComposite {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 随机选择节点
|
|
||||||
* 随机选择一个子节点执行
|
|
||||||
* 返回子节点状态
|
|
||||||
*/
|
|
||||||
export class RandomSelector extends Composite {
|
|
||||||
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();
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 选择节点 从上到下执行
|
* 选择节点 从上到下执行
|
||||||
* 返回第一个不为 FAILURE 的子节点状态
|
* 返回第一个不为 FAILURE 的子节点状态
|
||||||
* 否则返回 FAILURE
|
* 否则返回 FAILURE
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "选择节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "依次执行子节点,直到找到成功或运行中的节点",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class Selector extends Composite {
|
export class Selector extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
for (let i = 0; i < this.children.length; i++) {
|
for (let i = 0; i < this.children.length; i++) {
|
||||||
@@ -85,11 +89,76 @@ export class Selector extends Composite {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 随机选择节点
|
||||||
|
* 随机选择一个子节点执行
|
||||||
|
* 返回子节点状态
|
||||||
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "随机选择节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "随机选择一个子节点执行",
|
||||||
|
parameters: [],
|
||||||
|
})
|
||||||
|
export class RandomSelector extends Composite {
|
||||||
|
private _totalWeight: number = 0;
|
||||||
|
private _weights: number[] = [];
|
||||||
|
|
||||||
|
constructor(...children: IBTNode[]) {
|
||||||
|
super(...children);
|
||||||
|
this._totalWeight = 0;
|
||||||
|
this._weights = [];
|
||||||
|
|
||||||
|
for (const child of children) {
|
||||||
|
const weight = this.getChildWeight(child);
|
||||||
|
this._totalWeight += weight;
|
||||||
|
this._weights.push(this._totalWeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getChildWeight(child: IBTNode): number {
|
||||||
|
return (child instanceof WeightDecorator) ? (child.weight) : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public tick(): Status {
|
||||||
|
if (this.children.length === 0) {
|
||||||
|
return Status.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基于权重的随机选择
|
||||||
|
const randomValue = Math.random() * this._totalWeight;
|
||||||
|
|
||||||
|
// 使用二分查找找到对应的子节点索引(O(log n)复杂度)
|
||||||
|
let left = 0;
|
||||||
|
let right = this._weights.length - 1;
|
||||||
|
let childIndex = 0;
|
||||||
|
|
||||||
|
while (left <= right) {
|
||||||
|
const mid = Math.floor((left + right) / 2);
|
||||||
|
if (this._weights[mid]! > randomValue) {
|
||||||
|
childIndex = mid;
|
||||||
|
right = mid - 1;
|
||||||
|
} else {
|
||||||
|
left = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const status = this.children[childIndex]!._execute();
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 顺序节点 从上到下执行
|
* 顺序节点 从上到下执行
|
||||||
* 遇到 SUCCESS 继续下一个
|
* 遇到 SUCCESS 继续下一个
|
||||||
* 否则返回子节点状态
|
* 否则返回子节点状态
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "顺序节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "依次执行所有子节点,全部成功才返回成功",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class Sequence extends Composite {
|
export class Sequence extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
for (let i = 0; i < this.children.length; i++) {
|
for (let i = 0; i < this.children.length; i++) {
|
||||||
@@ -107,6 +176,12 @@ export class Sequence extends Composite {
|
|||||||
* 并行节点 从上到下执行 全部执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "并行节点",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "同时执行所有子节点,全部成功才返回成功",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class Parallel extends Composite {
|
export class Parallel extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let result = Status.SUCCESS;
|
let result = Status.SUCCESS;
|
||||||
@@ -126,6 +201,12 @@ export class Parallel extends Composite {
|
|||||||
* 并行节点 从上到下执行 全部执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
||||||
*/
|
*/
|
||||||
|
@BT.CompositeNode({
|
||||||
|
name: "并行任意成功",
|
||||||
|
group: "基础组合节点",
|
||||||
|
description: "同时执行所有子节点,任意一个成功即返回成功",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class ParallelAnySuccess extends Composite {
|
export class ParallelAnySuccess extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let result = Status.FAILURE;
|
let result = Status.FAILURE;
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
import { Status } from "../header";
|
|
||||||
import { LeafNode } from "./AbstractNodes";
|
|
||||||
import { IBTNode } from "./BTNode";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件节点
|
|
||||||
* 根据条件函数返回SUCCESS或FAILURE
|
|
||||||
*/
|
|
||||||
export class Condition extends LeafNode {
|
|
||||||
/** 执行函数 @internal */
|
|
||||||
private readonly _func: (node: IBTNode) => boolean;
|
|
||||||
constructor(func: (node: IBTNode) => boolean) {
|
|
||||||
super();
|
|
||||||
this._func = func;
|
|
||||||
}
|
|
||||||
|
|
||||||
public tick(): Status {
|
|
||||||
return this._func?.(this) ? Status.SUCCESS : Status.FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
* @Description: 装饰节点 装饰节点下必须包含子节点
|
* @Description: 装饰节点 装饰节点下必须包含子节点
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { BT } from "../BT";
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
||||||
import { IBTNode } from "./BTNode";
|
import { IBTNode } from "./BTNode";
|
||||||
@@ -14,6 +15,12 @@ import { IBTNode } from "./BTNode";
|
|||||||
* 第一个Child Node节点, 返回 FAILURE, 本Node向自己的Parent Node也返回 SUCCESS
|
* 第一个Child Node节点, 返回 FAILURE, 本Node向自己的Parent Node也返回 SUCCESS
|
||||||
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "反转器",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "反转子节点的执行结果,成功变失败,失败变成功",
|
||||||
|
parameters: []
|
||||||
|
})
|
||||||
export class Inverter extends Decorator {
|
export class Inverter extends Decorator {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
const status = this.children[0]!._execute();
|
const status = this.children[0]!._execute();
|
||||||
@@ -34,6 +41,14 @@ export class Inverter extends Decorator {
|
|||||||
* 规定时间内, 根据Child Node的结果, 本节点向自己的父节点也返回相同的结果
|
* 规定时间内, 根据Child Node的结果, 本节点向自己的父节点也返回相同的结果
|
||||||
* 超时后, 直接返回 FAILURE
|
* 超时后, 直接返回 FAILURE
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "时间限制器",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "限制子节点执行时间,超时返回失败",
|
||||||
|
parameters: [
|
||||||
|
{ name: "max", type: BT.ParamType.float, description: "最大时间(秒)", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class LimitTime extends NumericDecorator {
|
export class LimitTime extends NumericDecorator {
|
||||||
/**
|
/**
|
||||||
* 时间限制节点
|
* 时间限制节点
|
||||||
@@ -62,6 +77,14 @@ export class LimitTime extends NumericDecorator {
|
|||||||
* 必须且只能包含一个子节点
|
* 必须且只能包含一个子节点
|
||||||
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "次数限制器",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "限制子节点执行次数,超过次数返回失败",
|
||||||
|
parameters: [
|
||||||
|
{ name: "max", type: BT.ParamType.int, description: "最大次数", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class LimitTicks extends NumericDecorator {
|
export class LimitTicks extends NumericDecorator {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
this._value++;
|
this._value++;
|
||||||
@@ -78,6 +101,14 @@ export class LimitTicks extends NumericDecorator {
|
|||||||
* 子节点是成功或失败,累加计数
|
* 子节点是成功或失败,累加计数
|
||||||
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "重复器",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "重复执行子节点指定次数",
|
||||||
|
parameters: [
|
||||||
|
{ name: "max", type: BT.ParamType.int, description: "重复次数", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class Repeat extends NumericDecorator {
|
export class Repeat extends NumericDecorator {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
// 执行子节点
|
// 执行子节点
|
||||||
@@ -101,6 +132,14 @@ export class Repeat extends NumericDecorator {
|
|||||||
*
|
*
|
||||||
* 子节点成功 计数+1
|
* 子节点成功 计数+1
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "重复直到失败",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "重复执行子节点直到失败或达到最大次数",
|
||||||
|
parameters: [
|
||||||
|
{ name: "max", type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class RepeatUntilFailure extends NumericDecorator {
|
export class RepeatUntilFailure extends NumericDecorator {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
const status = this.children[0]!._execute();
|
const status = this.children[0]!._execute();
|
||||||
@@ -125,6 +164,14 @@ export class RepeatUntilFailure extends NumericDecorator {
|
|||||||
*
|
*
|
||||||
* 子节点失败, 计数+1
|
* 子节点失败, 计数+1
|
||||||
*/
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "重复直到成功",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "重复执行子节点直到成功或达到最大次数",
|
||||||
|
parameters: [
|
||||||
|
{ name: "max", type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
export class RepeatUntilSuccess extends NumericDecorator {
|
export class RepeatUntilSuccess extends NumericDecorator {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
// 执行子节点
|
// 执行子节点
|
||||||
@@ -141,4 +188,33 @@ export class RepeatUntilSuccess extends NumericDecorator {
|
|||||||
}
|
}
|
||||||
return Status.RUNNING;
|
return Status.RUNNING;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 权重装饰节点
|
||||||
|
*/
|
||||||
|
@BT.DecoratorNode({
|
||||||
|
name: "权重装饰器",
|
||||||
|
group: "基础装饰节点",
|
||||||
|
description: "权重装饰节点",
|
||||||
|
parameters: [
|
||||||
|
{ name: "weight", type: BT.ParamType.float, description: "权重", defaultValue: 1, required: true }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
export class WeightDecorator extends Decorator {
|
||||||
|
private _weight: number;
|
||||||
|
|
||||||
|
constructor(child: IBTNode, weight?: number) {
|
||||||
|
super(child);
|
||||||
|
// 优先使用构造函数参数,否则使用装饰器默认参数
|
||||||
|
this._weight = weight || 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public tick(): Status {
|
||||||
|
return this.children[0]!._execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public get weight(): number {
|
||||||
|
return this._weight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@ export * from "./behaviortree/BTNode/AbstractNodes";
|
|||||||
export * from "./behaviortree/BTNode/Action";
|
export * from "./behaviortree/BTNode/Action";
|
||||||
export { IBTNode } from "./behaviortree/BTNode/BTNode";
|
export { IBTNode } from "./behaviortree/BTNode/BTNode";
|
||||||
export * from "./behaviortree/BTNode/Composite";
|
export * from "./behaviortree/BTNode/Composite";
|
||||||
export { Condition } from "./behaviortree/BTNode/Condition";
|
|
||||||
export * from "./behaviortree/BTNode/Decorator";
|
export * from "./behaviortree/BTNode/Decorator";
|
||||||
export { Status } from "./behaviortree/header";
|
export { Status } from "./behaviortree/header";
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
"lib": ["es6", "dom"],
|
"lib": ["es6", "dom"],
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"experimentalDecorators": true, // 启用ES装饰器
|
"experimentalDecorators": true, // 启用ES装饰器
|
||||||
|
"emitDecoratorMetadata": true, // 启用装饰器元数据
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"strictNullChecks": true,
|
"strictNullChecks": true,
|
||||||
@@ -20,7 +21,6 @@
|
|||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"./src/**/*"
|
"./src/**/*"
|
||||||
// "libs"
|
|
||||||
],
|
],
|
||||||
// 排除
|
// 排除
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
|||||||
Reference in New Issue
Block a user