mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
项目重构,破坏性更新
This commit is contained in:
@@ -1,117 +1,41 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Ticker } from "../Ticker";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
|
||||
/**
|
||||
* 动作节点
|
||||
* 没有子节点
|
||||
*/
|
||||
export abstract class Action extends BaseNode {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败节点(无子节点)
|
||||
* 直接返回FAILURE
|
||||
*/
|
||||
export class Failure extends Action {
|
||||
/** 执行函数 @internal */
|
||||
private _func: () => void;
|
||||
constructor(func: () => void) {
|
||||
export class Action extends BaseNode {
|
||||
protected _func: (subject?: any) => Status;
|
||||
constructor(func: (subject?: any) => Status) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
this._func();
|
||||
return Status.FAILURE;
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 逻辑节点,一直执行 (无子节点)
|
||||
* 直接返回RUNING
|
||||
*/
|
||||
export class Running extends Action {
|
||||
/** 执行函数 @internal */
|
||||
private _func: () => void;
|
||||
constructor(func: () => void) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
this._func();
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功节点 无子节点
|
||||
* 直接返回SUCCESS
|
||||
*/
|
||||
export class Success extends Action {
|
||||
/** 执行函数 @internal */
|
||||
private _func: () => void;
|
||||
constructor(func: () => void) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
this._func();
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 次数等待节点(无子节点)
|
||||
* 次数内,返回RUNING
|
||||
* 次数内,返回RUNNING
|
||||
* 超次,返回SUCCESS
|
||||
*/
|
||||
export class WaitTicks extends Action {
|
||||
/** 最大次数 @internal */
|
||||
private _maxTicks: number;
|
||||
/** 经过的次数 @internal */
|
||||
private _elapsedTicks: number;
|
||||
export class WaitTicks extends BaseNode {
|
||||
private _max: number;
|
||||
private _value: number;
|
||||
|
||||
constructor(maxTicks: number = 0) {
|
||||
super();
|
||||
this._maxTicks = maxTicks;
|
||||
this._elapsedTicks = 0;
|
||||
this._max = maxTicks;
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开
|
||||
* @param {Ticker} ticker
|
||||
*/
|
||||
public open(ticker: Ticker): void {
|
||||
this._elapsedTicks = 0;
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
if (++this._elapsedTicks >= this._maxTicks) {
|
||||
this._elapsedTicks = 0;
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
if (++this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
return Status.RUNNING;
|
||||
@@ -119,71 +43,27 @@ export class WaitTicks extends Action {
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间等待节点(无子节点)
|
||||
* 时间到后返回SUCCESS,否则返回RUNING
|
||||
* 时间等待节点 时间(秒)
|
||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||
*/
|
||||
export class WaitTime extends Action {
|
||||
/** 等待时间(秒 s) @internal */
|
||||
private _duration: number;
|
||||
export class WaitTime extends BaseNode {
|
||||
private _max: number;
|
||||
private _value: number = 0;
|
||||
constructor(duration: number = 0) {
|
||||
super();
|
||||
this._duration = duration * 1000;
|
||||
this._max = duration * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开
|
||||
* @param {Ticker} ticker
|
||||
*/
|
||||
public open(ticker: Ticker): void {
|
||||
let startTime = new Date().getTime();
|
||||
ticker.blackboard.set("startTime", startTime, ticker.tree.id, this.id);
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
this._value = new Date().getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
let currTime = new Date().getTime();
|
||||
let startTime = ticker.blackboard.get("startTime", ticker.tree.id, this.id);
|
||||
if (currTime - startTime >= this._duration) {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const currTime = new Date().getTime();
|
||||
if (currTime - this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 行为树防止被打断节点
|
||||
* 直接返回 SUCCESS
|
||||
* 和 InterruptDefendCancel 必须成对出现
|
||||
*/
|
||||
export class InterruptDefend extends Action {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
ticker.blackboard.interruptDefend = true;
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 行为树被打断取消节点
|
||||
* 直接返回 SUCCESS
|
||||
* 和 InterruptDefend 必须成对出现
|
||||
*/
|
||||
export class InterruptDefendCancel extends Action {
|
||||
/**
|
||||
* 执行
|
||||
* @param {Ticker} ticker
|
||||
* @returns {Status}
|
||||
*/
|
||||
public tick(ticker: Ticker): Status {
|
||||
ticker.blackboard.interruptDefend = false;
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user