mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
demo逻辑调整
This commit is contained in:
72
bt-demo/assets/script/BTNode.ts
Normal file
72
bt-demo/assets/script/BTNode.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-17
|
||||
* @Description: 定义一些行为节点
|
||||
*/
|
||||
|
||||
import { sp } from "cc";
|
||||
import { BT } from "./Header";
|
||||
|
||||
@BT.ActionNode("BTAnimation", { name: "播放动画", group: "动画", desc: "通过动画名播放动画,播放完成后返回成功" })
|
||||
export class BTAnimation extends BT.LeafNode {
|
||||
@BT.prop({ type: BT.ParamType.string, description: "动画名" })
|
||||
private _name: string = "";
|
||||
|
||||
@BT.prop({ type: BT.ParamType.bool, description: "是否循环" })
|
||||
private _loop: boolean = false;
|
||||
|
||||
private _complete: boolean = false;
|
||||
|
||||
protected open(): void {
|
||||
super.open();
|
||||
this._complete = false;
|
||||
|
||||
console.log("open", this._name, this._loop);
|
||||
|
||||
let skeleton = this.getEntity<sp.Skeleton>();
|
||||
skeleton.setAnimation(0, this._name, this._loop);
|
||||
|
||||
if (!this._loop) {
|
||||
skeleton.setCompleteListener(() => {
|
||||
this._complete = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public tick(): BT.Status {
|
||||
if (!this._loop && this._complete) {
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
return BT.Status.RUNNING;
|
||||
}
|
||||
|
||||
protected close(): void {
|
||||
super.close();
|
||||
console.log("close", this._name, this._loop);
|
||||
}
|
||||
}
|
||||
|
||||
/** 条件节点 */
|
||||
@BT.ConditionNode("BTConditionRandom", { name: "随机条件节点", group: "基础条件节点", desc: "随机0-1的值,大于设置值返回成功,否则返回失败" })
|
||||
export class BTConditionRandom extends BT.Condition {
|
||||
|
||||
@BT.prop({ type: BT.ParamType.float, description: "值", defaultValue: 0.5 })
|
||||
private _value: number = 0.5;
|
||||
|
||||
public isEligible(): boolean {
|
||||
return Math.random() > this._value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** 条件装饰节点 */
|
||||
@BT.DecoratorNode("BTCondition", { name: "条件装饰节点", group: "基础装饰节点", desc: "随机0-1的值,大于设置值返回成功,否则返回失败" })
|
||||
export class BTCondition extends BT.ConditionDecorator {
|
||||
|
||||
@BT.prop({ type: BT.ParamType.float, description: "值" })
|
||||
private _value: number = 0.5;
|
||||
|
||||
public isEligible(): boolean {
|
||||
return Math.random() > this._value;
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/script/BTNode.ts.meta
Normal file
9
bt-demo/assets/script/BTNode.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6c8cc47c-1976-432a-aa59-932cb74f41a2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,20 +1,23 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
import * as BT from "kunpocc-behaviortree";
|
||||
import { _decorator, Component, JsonAsset, sp } from 'cc';
|
||||
import { BT } from './Header';
|
||||
const { ccclass, property, menu } = _decorator;
|
||||
@ccclass("GameEntry")
|
||||
@menu("kunpo/GameEntry")
|
||||
export class GameEntry extends Component {
|
||||
@property(Node)
|
||||
private stage: Node = null;
|
||||
@property(sp.Skeleton)
|
||||
private skeleton: sp.Skeleton = null;
|
||||
|
||||
@property(Node)
|
||||
private touchNode: Node = null;
|
||||
@property(JsonAsset)
|
||||
private btConfig: JsonAsset = null;
|
||||
|
||||
private _tree: BT.BehaviorTree<sp.Skeleton> = null;
|
||||
start(): void {
|
||||
BT
|
||||
console.log("btConfig", this.btConfig);
|
||||
let btTree1: BT.INodeConfig[] = this.btConfig.json["bt-tree1"]
|
||||
this._tree = BT.createBehaviorTree(btTree1, this.skeleton);
|
||||
}
|
||||
|
||||
protected update(dt: number): void {
|
||||
|
||||
this._tree.tick(dt);
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/script/Header.ts
Normal file
9
bt-demo/assets/script/Header.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-17
|
||||
* @Description: 头文件
|
||||
*/
|
||||
|
||||
import * as BT from "kunpocc-behaviortree";
|
||||
export { BT };
|
||||
|
||||
9
bt-demo/assets/script/Header.ts.meta
Normal file
9
bt-demo/assets/script/Header.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "17dd8d23-3e47-454a-9e47-69e371273e3b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user