From 50e29feeb824d92090401ec2636db229b89ec36f Mon Sep 17 00:00:00 2001 From: gongxh Date: Thu, 18 Sep 2025 22:51:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E6=A0=91=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=B7=BB=E5=8A=A0=E5=A4=8D=E5=90=88=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=8F=82=E6=95=B0=EF=BC=8C=E6=9B=B4=E6=96=B0demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/resources/config/bt_config.json | 145 ++---------------- bt-demo/assets/script/BTNode.ts | 29 +++- .../extensions-config/bt-editor/bt-tree1.json | 78 +++++++--- .../extensions-config/bt-editor/bt-tree2.json | 46 ++++-- bt-demo/package.json | 2 +- package.json | 2 +- src/behaviortree/BT.ts | 10 +- 7 files changed, 141 insertions(+), 171 deletions(-) diff --git a/bt-demo/assets/resources/config/bt_config.json b/bt-demo/assets/resources/config/bt_config.json index b86dca0..1cc138e 100644 --- a/bt-demo/assets/resources/config/bt_config.json +++ b/bt-demo/assets/resources/config/bt_config.json @@ -1,138 +1,8 @@ { - "bt-tree1": [ - { - "id": "1759488688188_qejfcso50", - "className": "Selector", - "parameters": {}, - "children": [ - "1759488707759_2bmdm1fqt", - "1759488725107_v8u160t95", - "1759488737637_axpz9aqaz", - "1759482034741_cf3mqaqdj" - ] - }, - { - "id": "1759479318405_bptb8ltcp", - "className": "LimitTime", - "parameters": { - "_max": 2 - }, - "children": [ - "1758089736854_t55n54hkh" - ] - }, - { - "id": "1759479295671_jflit2ek8", - "className": "LimitTime", - "parameters": { - "_max": 2 - }, - "children": [ - "1758089659917_vjumiu9hy" - ] - }, - { - "id": "1758089659917_vjumiu9hy", - "className": "BTAnimation", - "parameters": { - "_name": "walk", - "_loop": true - }, - "children": [] - }, - { - "id": "1758089736854_t55n54hkh", - "className": "BTAnimation", - "parameters": { - "_name": "run", - "_loop": true - }, - "children": [] - }, - { - "id": "1758089757615_dp9tw9ka1", - "className": "BTAnimation", - "parameters": { - "_name": "jump", - "_loop": false - }, - "children": [] - }, - { - "id": "1759478407706_w30m4btux", - "className": "BTAnimation", - "parameters": { - "_name": "idle", - "_loop": true - }, - "children": [] - }, - { - "id": "1759481172259_xou25wj2n", - "className": "BTConditionRandom", - "parameters": { - "_value": 0.3 - }, - "children": [] - }, - { - "id": "1759481282875_5orqavi5y", - "className": "BTConditionRandom", - "parameters": { - "_value": 0.4 - }, - "children": [] - }, - { - "id": "1759481307863_ja6q4q9bz", - "className": "BTConditionRandom", - "parameters": { - "_value": 0.3 - }, - "children": [] - }, - { - "id": "1759482034741_cf3mqaqdj", - "className": "LimitTime", - "parameters": { - "_max": 3 - }, - "children": [ - "1759478407706_w30m4btux" - ] - }, - { - "id": "1759488707759_2bmdm1fqt", - "className": "Sequence", - "parameters": {}, - "children": [ - "1759481172259_xou25wj2n", - "1759479295671_jflit2ek8" - ] - }, - { - "id": "1759488725107_v8u160t95", - "className": "Sequence", - "parameters": {}, - "children": [ - "1759481282875_5orqavi5y", - "1759479318405_bptb8ltcp" - ] - }, - { - "id": "1759488737637_axpz9aqaz", - "className": "Sequence", - "parameters": {}, - "children": [ - "1759481307863_ja6q4q9bz", - "1758089757615_dp9tw9ka1" - ] - } - ], "bt-tree2": [ { - "id": "1757930589538_qisfksbwz", - "className": "MemSequence", + "id": "1758206972710_bhxebhy7o", + "className": "Sequence", "parameters": {}, "children": [ "1758090634327_mf36nwkdt" @@ -142,6 +12,17 @@ "id": "1758090634327_mf36nwkdt", "className": "Selector", "parameters": {}, + "children": [ + "1758206988178_55b7kk5va" + ] + }, + { + "id": "1758206988178_55b7kk5va", + "className": "BTAnimation", + "parameters": { + "_name": "", + "_loop": false + }, "children": [] } ] diff --git a/bt-demo/assets/script/BTNode.ts b/bt-demo/assets/script/BTNode.ts index 0bd510c..a3d6358 100644 --- a/bt-demo/assets/script/BTNode.ts +++ b/bt-demo/assets/script/BTNode.ts @@ -7,6 +7,33 @@ import { sp } from "cc"; import { BT } from "./Header"; +@BT.ClassAction("BTTestNode", { name: "嵌套数据测试节点", group: "测试", desc: "测试节点" }) +export class BTTestNode extends BT.LeafNode { + @BT.prop({ + type: BT.ParamType.object, + properties: { + x: { type: BT.ParamType.int, min: 0 }, + y: { type: BT.ParamType.int, min: 0 } + } + }) + position: { x: number, y: number }; + + // 对象数组参数 + @BT.prop({ + type: BT.ParamType.array, + itemType: BT.ParamType.object, + itemProperties: { + name: { type: BT.ParamType.string }, + value: { type: BT.ParamType.int } + } + }) + configs: Array<{ name: string, value: number }>; + + public tick(): BT.Status { + return BT.Status.SUCCESS; + } +} + @BT.ClassAction("BTAnimation", { name: "播放动画", group: "动画", desc: "通过动画名播放动画,播放完成后返回成功" }) export class BTAnimation extends BT.LeafNode { @BT.prop({ type: BT.ParamType.string, description: "动画名" }) @@ -54,7 +81,7 @@ export class BTConditionRandom extends BT.Condition { private _value: number = 0.5; public isEligible(): boolean { - return Math.random() > this._value; + return Math.random() < this._value; } } diff --git a/bt-demo/extensions-config/bt-editor/bt-tree1.json b/bt-demo/extensions-config/bt-editor/bt-tree1.json index a429844..c7d61ca 100644 --- a/bt-demo/extensions-config/bt-editor/bt-tree1.json +++ b/bt-demo/extensions-config/bt-editor/bt-tree1.json @@ -1,6 +1,6 @@ { - "name": "bt-tree1", - "description": "", + "name": "bt-tree2", + "description": "这是一个描述", "nodes": [ { "id": "1759488688188_qejfcso50", @@ -8,23 +8,46 @@ "name": "选择节点", "position": { "x": -60, - "y": -240 + "y": -200 }, "parameters": {}, "children": [ "1759488707759_2bmdm1fqt", "1759488725107_v8u160t95", "1759488737637_axpz9aqaz", - "1759482034741_cf3mqaqdj" + "1759482034741_cf3mqaqdj", + "1758190139303_t5o7vv3ak" ], "alias": "根选择节点" }, + { + "id": "1758190139303_t5o7vv3ak", + "className": "BTTestNode", + "name": "嵌套数据测试节点", + "position": { + "x": 440, + "y": -80 + }, + "parameters": { + "position": { + "x": 10, + "y": 20 + }, + "configs": [ + { + "name": "hahaa", + "value": 1 + } + ] + }, + "children": [] + }, { "id": "1759479318405_bptb8ltcp", "className": "LimitTime", "name": "时间限制器", "position": { - "x": -60, + "x": -120, "y": 40 }, "parameters": { @@ -39,7 +62,7 @@ "className": "LimitTime", "name": "时间限制器", "position": { - "x": -340, + "x": -400, "y": 40 }, "parameters": { @@ -54,7 +77,7 @@ "className": "BTAnimation", "name": "播放动画", "position": { - "x": -340, + "x": -400, "y": 160 }, "parameters": { @@ -68,8 +91,8 @@ "className": "BTAnimation", "name": "播放动画", "position": { - "x": -60, - "y": 200 + "x": -120, + "y": 160 }, "parameters": { "_name": "run", @@ -82,8 +105,8 @@ "className": "BTAnimation", "name": "播放动画", "position": { - "x": 220, - "y": 60 + "x": 160, + "y": 40 }, "parameters": { "_name": "jump", @@ -96,7 +119,7 @@ "className": "BTAnimation", "name": "播放动画", "position": { - "x": 360, + "x": 300, "y": 40 }, "parameters": { @@ -110,7 +133,7 @@ "className": "BTConditionRandom", "name": "随机条件节点", "position": { - "x": -480, + "x": -540, "y": 40 }, "parameters": { @@ -123,7 +146,7 @@ "className": "BTConditionRandom", "name": "随机条件节点", "position": { - "x": -200, + "x": -260, "y": 40 }, "parameters": { @@ -136,7 +159,7 @@ "className": "BTConditionRandom", "name": "随机条件节点", "position": { - "x": 80, + "x": 20, "y": 40 }, "parameters": { @@ -149,11 +172,11 @@ "className": "LimitTime", "name": "时间限制器", "position": { - "x": 360, + "x": 300, "y": -80 }, "parameters": { - "_max": 3 + "_max": 2 }, "children": [ "1759478407706_w30m4btux" @@ -165,7 +188,7 @@ "className": "Sequence", "name": "顺序节点", "position": { - "x": -400, + "x": -480, "y": -80 }, "parameters": {}, @@ -180,7 +203,7 @@ "className": "Sequence", "name": "顺序节点", "position": { - "x": -120, + "x": -200, "y": -80 }, "parameters": {}, @@ -188,14 +211,14 @@ "1759481282875_5orqavi5y", "1759479318405_bptb8ltcp" ], - "alias": "奔跑动画分支" + "alias": "奔跑动画" }, { "id": "1759488737637_axpz9aqaz", "className": "Sequence", "name": "顺序节点", "position": { - "x": 160, + "x": 80, "y": -80 }, "parameters": {}, @@ -297,11 +320,18 @@ "targetNodeId": "1758089757615_dp9tw9ka1", "sourcePointType": "child", "targetPointType": "parent" + }, + { + "id": "conn_1758204108181_90iaioyvg", + "sourceNodeId": "1759488688188_qejfcso50", + "targetNodeId": "1758190139303_t5o7vv3ak", + "sourcePointType": "child", + "targetPointType": "parent" } ], - "canvasScale": 1.0252718826976899, + "canvasScale": 1.0006385665653545, "canvasOffset": { - "x": 540.227707847, - "y": 428.7616100108487 + "x": 584.9936143343465, + "y": 498.99074078480237 } } \ No newline at end of file diff --git a/bt-demo/extensions-config/bt-editor/bt-tree2.json b/bt-demo/extensions-config/bt-editor/bt-tree2.json index d797409..0ca65f9 100644 --- a/bt-demo/extensions-config/bt-editor/bt-tree2.json +++ b/bt-demo/extensions-config/bt-editor/bt-tree2.json @@ -3,12 +3,12 @@ "description": "", "nodes": [ { - "id": "1757930589538_qisfksbwz", - "className": "MemSequence", - "name": "记忆顺序节点", + "id": "1758206972710_bhxebhy7o", + "className": "Sequence", + "name": "顺序节点", "position": { - "x": -60, - "y": -280 + "x": 80, + "y": -320 }, "parameters": {}, "children": [ @@ -20,25 +20,49 @@ "className": "Selector", "name": "选择节点", "position": { - "x": 20, - "y": -80 + "x": -80, + "y": -220 }, "parameters": {}, + "children": [ + "1758206988178_55b7kk5va" + ], + "alias": "是的发放是的发放" + }, + { + "id": "1758206988178_55b7kk5va", + "className": "BTAnimation", + "name": "播放动画", + "position": { + "x": -20, + "y": -40 + }, + "parameters": { + "_name": "", + "_loop": false + }, "children": [] } ], "connections": [ { - "id": "conn_1758090635620_zajj5r8g0", - "sourceNodeId": "1757930589538_qisfksbwz", + "id": "conn_1758206976733_208tneycs", + "sourceNodeId": "1758206972710_bhxebhy7o", "targetNodeId": "1758090634327_mf36nwkdt", "sourcePointType": "child", "targetPointType": "parent" + }, + { + "id": "conn_1758206989897_46hw88z7h", + "sourceNodeId": "1758090634327_mf36nwkdt", + "targetNodeId": "1758206988178_55b7kk5va", + "sourcePointType": "child", + "targetPointType": "parent" } ], "canvasScale": 1.139190980775211, "canvasOffset": { - "x": 266.1539154489914, - "y": 608.1811503312816 + "x": 549.4323607689915, + "y": 698.6185343759718 } } \ No newline at end of file diff --git a/bt-demo/package.json b/bt-demo/package.json index c62fb91..cd8be4d 100755 --- a/bt-demo/package.json +++ b/bt-demo/package.json @@ -5,7 +5,7 @@ "version": "3.8.6" }, "dependencies": { - "kunpocc-behaviortree": "^0.1.0", + "kunpocc-behaviortree": "^0.1.1", "ts-node": "^10.9.2" } } diff --git a/package.json b/package.json index 265d96c..8ba9135 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kunpocc-behaviortree", - "version": "0.1.0", + "version": "0.1.1", "description": "行为树", "main": "./dist/kunpocc-behaviortree.cjs", "module": "./dist/kunpocc-behaviortree.mjs", diff --git a/src/behaviortree/BT.ts b/src/behaviortree/BT.ts index 5e6f323..6bfa71f 100644 --- a/src/behaviortree/BT.ts +++ b/src/behaviortree/BT.ts @@ -11,7 +11,9 @@ export namespace BT { int = "number", float = "float", string = "string", - bool = "boolean" + bool = "boolean", + object = "object", + array = "array" } /** @@ -46,6 +48,12 @@ export namespace BT { min?: number, /** 最大值 */ max?: number, + /** 对象属性定义 - 仅当type为object时使用 */ + properties?: { [key: string]: Omit }; + /** 数组元素类型 - 仅当type为array时使用 */ + itemType?: ParamType; + /** 数组元素对象定义 - 仅当type为array且itemType为object时使用 */ + itemProperties?: { [key: string]: Omit }; } /**