2025-09-02 17:05:46 +08:00
|
|
|
|
import type { BehaviorTree } from "../BehaviorTree";
|
2025-06-04 23:10:59 +08:00
|
|
|
|
import { Status } from "../header";
|
|
|
|
|
|
import { BaseNode } from "./BaseNode";
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
export class Action extends BaseNode {
|
|
|
|
|
|
protected _func: (subject?: any) => Status;
|
|
|
|
|
|
constructor(func: (subject?: any) => Status) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
super();
|
|
|
|
|
|
this._func = func;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
public tick<T>(tree: BehaviorTree<T>): Status {
|
|
|
|
|
|
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 次数等待节点(无子节点)
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 次数内,返回RUNNING
|
2025-06-04 23:10:59 +08:00
|
|
|
|
* 超次,返回SUCCESS
|
|
|
|
|
|
*/
|
2025-09-02 17:05:46 +08:00
|
|
|
|
export class WaitTicks extends BaseNode {
|
|
|
|
|
|
private _max: number;
|
|
|
|
|
|
private _value: number;
|
|
|
|
|
|
|
2025-06-04 23:10:59 +08:00
|
|
|
|
constructor(maxTicks: number = 0) {
|
|
|
|
|
|
super();
|
2025-09-02 17:05:46 +08:00
|
|
|
|
this._max = maxTicks;
|
|
|
|
|
|
this._value = 0;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
|
|
|
|
|
super.initialize(tree);
|
|
|
|
|
|
this._value = 0;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
public tick<T>(tree: BehaviorTree<T>): Status {
|
|
|
|
|
|
if (++this._value >= this._max) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.RUNNING;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 时间等待节点 时间(秒)
|
|
|
|
|
|
* 时间到后返回SUCCESS,否则返回RUNNING
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-02 17:05:46 +08:00
|
|
|
|
export class WaitTime extends BaseNode {
|
|
|
|
|
|
private _max: number;
|
|
|
|
|
|
private _value: number = 0;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
constructor(duration: number = 0) {
|
|
|
|
|
|
super();
|
2025-09-02 17:05:46 +08:00
|
|
|
|
this._max = duration * 1000;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
|
|
|
|
|
super.initialize(tree);
|
|
|
|
|
|
this._value = new Date().getTime();
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
public tick<T>(tree: BehaviorTree<T>): Status {
|
|
|
|
|
|
const currTime = new Date().getTime();
|
|
|
|
|
|
if (currTime - this._value >= this._max) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.RUNNING;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|