@nice2dev/game-engine 0.1.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +193 -1
- package/dist/cjs/ai/BehaviorTree.js +1215 -0
- package/dist/cjs/ai/BehaviorTree.js.map +1 -0
- package/dist/cjs/ai/StateMachine.js +783 -0
- package/dist/cjs/ai/StateMachine.js.map +1 -0
- package/dist/cjs/audio/AudioBridge.js +454 -0
- package/dist/cjs/audio/AudioBridge.js.map +1 -0
- package/dist/cjs/devtools/GameplayAnalytics.js +651 -0
- package/dist/cjs/devtools/GameplayAnalytics.js.map +1 -0
- package/dist/cjs/dialogue/DialogueSystem.js +1023 -0
- package/dist/cjs/dialogue/DialogueSystem.js.map +1 -0
- package/dist/cjs/editor/NiceGameEditor.js +569 -71
- package/dist/cjs/editor/NiceGameEditor.js.map +1 -1
- package/dist/cjs/editor/ShaderGraph.js +1616 -0
- package/dist/cjs/editor/ShaderGraph.js.map +1 -0
- package/dist/cjs/editor/TimelineEditor.js +819 -0
- package/dist/cjs/editor/TimelineEditor.js.map +1 -0
- package/dist/cjs/engine/SaveSystemV2.js +494 -0
- package/dist/cjs/engine/SaveSystemV2.js.map +1 -0
- package/dist/cjs/export/GodotExporter.js +1102 -0
- package/dist/cjs/export/GodotExporter.js.map +1 -0
- package/dist/cjs/export/PlatformExporter.js +236 -0
- package/dist/cjs/export/PlatformExporter.js.map +1 -0
- package/dist/cjs/export/ThreeJSExporter.js +1116 -0
- package/dist/cjs/export/ThreeJSExporter.js.map +1 -0
- package/dist/cjs/export/UnityExporter.js +1193 -0
- package/dist/cjs/export/UnityExporter.js.map +1 -0
- package/dist/cjs/export/WebExporter.js +1036 -0
- package/dist/cjs/export/WebExporter.js.map +1 -0
- package/dist/cjs/export/index.js +58 -0
- package/dist/cjs/export/index.js.map +1 -0
- package/dist/cjs/i18n/useTranslation.js +11 -11
- package/dist/cjs/import/AsepriteImporter.js +761 -0
- package/dist/cjs/import/AsepriteImporter.js.map +1 -0
- package/dist/cjs/import/DragonBonesImporter.js +499 -0
- package/dist/cjs/import/DragonBonesImporter.js.map +1 -0
- package/dist/cjs/import/GameMakerImporter.js +559 -0
- package/dist/cjs/import/GameMakerImporter.js.map +1 -0
- package/dist/cjs/import/GodotSceneImporter.js +824 -0
- package/dist/cjs/import/GodotSceneImporter.js.map +1 -0
- package/dist/cjs/import/LDtkImporter.js +481 -0
- package/dist/cjs/import/LDtkImporter.js.map +1 -0
- package/dist/cjs/import/Live2DImporter.js +553 -0
- package/dist/cjs/import/Live2DImporter.js.map +1 -0
- package/dist/cjs/import/NdgFormat.js +499 -0
- package/dist/cjs/import/NdgFormat.js.map +1 -0
- package/dist/cjs/import/OgmoImporter.js +529 -0
- package/dist/cjs/import/OgmoImporter.js.map +1 -0
- package/dist/cjs/import/RPGMakerImporter.js +520 -0
- package/dist/cjs/import/RPGMakerImporter.js.map +1 -0
- package/dist/cjs/import/SceneImporter.js +449 -0
- package/dist/cjs/import/SceneImporter.js.map +1 -0
- package/dist/cjs/import/SpineImporter.js +583 -0
- package/dist/cjs/import/SpineImporter.js.map +1 -0
- package/dist/cjs/import/SpriterImporter.js +652 -0
- package/dist/cjs/import/SpriterImporter.js.map +1 -0
- package/dist/cjs/import/TiledMapImporter.js +859 -0
- package/dist/cjs/import/TiledMapImporter.js.map +1 -0
- package/dist/cjs/import/UnitySceneImporter.js +732 -0
- package/dist/cjs/import/UnitySceneImporter.js.map +1 -0
- package/dist/cjs/import/index.js +305 -0
- package/dist/cjs/import/index.js.map +1 -0
- package/dist/cjs/index.js +291 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/input/GamepadNavigation.js +21 -21
- package/dist/cjs/input/useGamepads.js +6 -6
- package/dist/cjs/integration/IconSprite.js +281 -0
- package/dist/cjs/integration/IconSprite.js.map +1 -0
- package/dist/cjs/inventory/InventorySystem.js +930 -0
- package/dist/cjs/inventory/InventorySystem.js.map +1 -0
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -1
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -1
- package/dist/cjs/quest/QuestSystem.js +924 -0
- package/dist/cjs/quest/QuestSystem.js.map +1 -0
- package/dist/cjs/rendering/WebGPURenderPipeline.js +658 -0
- package/dist/cjs/rendering/WebGPURenderPipeline.js.map +1 -0
- package/dist/cjs/scripting/GraphToAST.js +567 -0
- package/dist/cjs/scripting/GraphToAST.js.map +1 -0
- package/dist/cjs/scripting/LanguageExporter.js +321 -0
- package/dist/cjs/scripting/LanguageExporter.js.map +1 -0
- package/dist/cjs/scripting/ScriptAST.js +67 -0
- package/dist/cjs/scripting/ScriptAST.js.map +1 -0
- package/dist/cjs/scripting/VisualScripting2.js +1140 -0
- package/dist/cjs/scripting/VisualScripting2.js.map +1 -0
- package/dist/cjs/scripting/exporters/CSharpExporter.js +503 -0
- package/dist/cjs/scripting/exporters/CSharpExporter.js.map +1 -0
- package/dist/cjs/scripting/exporters/GDScriptExporter.js +452 -0
- package/dist/cjs/scripting/exporters/GDScriptExporter.js.map +1 -0
- package/dist/cjs/scripting/exporters/LuaExporter.js +457 -0
- package/dist/cjs/scripting/exporters/LuaExporter.js.map +1 -0
- package/dist/cjs/scripting/exporters/PythonExporter.js +565 -0
- package/dist/cjs/scripting/exporters/PythonExporter.js.map +1 -0
- package/dist/cjs/scripting/exporters/RustExporter.js +525 -0
- package/dist/cjs/scripting/exporters/RustExporter.js.map +1 -0
- package/dist/cjs/scripting/exporters/TypeScriptExporter.js +570 -0
- package/dist/cjs/scripting/exporters/TypeScriptExporter.js.map +1 -0
- package/dist/cjs/systems/ParticleSystem2.js +1478 -0
- package/dist/cjs/systems/ParticleSystem2.js.map +1 -0
- package/dist/cjs/xr/ARVR.js.map +1 -1
- package/dist/esm/ai/BehaviorTree.js +1186 -0
- package/dist/esm/ai/BehaviorTree.js.map +1 -0
- package/dist/esm/ai/StateMachine.js +767 -0
- package/dist/esm/ai/StateMachine.js.map +1 -0
- package/dist/esm/audio/AudioBridge.js +446 -0
- package/dist/esm/audio/AudioBridge.js.map +1 -0
- package/dist/esm/devtools/GameplayAnalytics.js +639 -0
- package/dist/esm/devtools/GameplayAnalytics.js.map +1 -0
- package/dist/esm/dialogue/DialogueSystem.js +1008 -0
- package/dist/esm/dialogue/DialogueSystem.js.map +1 -0
- package/dist/esm/editor/NiceGameEditor.js +556 -58
- package/dist/esm/editor/NiceGameEditor.js.map +1 -1
- package/dist/esm/editor/ShaderGraph.js +1606 -0
- package/dist/esm/editor/ShaderGraph.js.map +1 -0
- package/dist/esm/editor/TimelineEditor.js +800 -0
- package/dist/esm/editor/TimelineEditor.js.map +1 -0
- package/dist/esm/engine/SaveSystemV2.js +487 -0
- package/dist/esm/engine/SaveSystemV2.js.map +1 -0
- package/dist/esm/export/GodotExporter.js +1100 -0
- package/dist/esm/export/GodotExporter.js.map +1 -0
- package/dist/esm/export/PlatformExporter.js +230 -0
- package/dist/esm/export/PlatformExporter.js.map +1 -0
- package/dist/esm/export/ThreeJSExporter.js +1114 -0
- package/dist/esm/export/ThreeJSExporter.js.map +1 -0
- package/dist/esm/export/UnityExporter.js +1191 -0
- package/dist/esm/export/UnityExporter.js.map +1 -0
- package/dist/esm/export/WebExporter.js +1033 -0
- package/dist/esm/export/WebExporter.js.map +1 -0
- package/dist/esm/export/index.js +44 -0
- package/dist/esm/export/index.js.map +1 -0
- package/dist/esm/import/AsepriteImporter.js +759 -0
- package/dist/esm/import/AsepriteImporter.js.map +1 -0
- package/dist/esm/import/DragonBonesImporter.js +496 -0
- package/dist/esm/import/DragonBonesImporter.js.map +1 -0
- package/dist/esm/import/GameMakerImporter.js +556 -0
- package/dist/esm/import/GameMakerImporter.js.map +1 -0
- package/dist/esm/import/GodotSceneImporter.js +822 -0
- package/dist/esm/import/GodotSceneImporter.js.map +1 -0
- package/dist/esm/import/LDtkImporter.js +479 -0
- package/dist/esm/import/LDtkImporter.js.map +1 -0
- package/dist/esm/import/Live2DImporter.js +550 -0
- package/dist/esm/import/Live2DImporter.js.map +1 -0
- package/dist/esm/import/NdgFormat.js +490 -0
- package/dist/esm/import/NdgFormat.js.map +1 -0
- package/dist/esm/import/OgmoImporter.js +526 -0
- package/dist/esm/import/OgmoImporter.js.map +1 -0
- package/dist/esm/import/RPGMakerImporter.js +517 -0
- package/dist/esm/import/RPGMakerImporter.js.map +1 -0
- package/dist/esm/import/SceneImporter.js +441 -0
- package/dist/esm/import/SceneImporter.js.map +1 -0
- package/dist/esm/import/SpineImporter.js +580 -0
- package/dist/esm/import/SpineImporter.js.map +1 -0
- package/dist/esm/import/SpriterImporter.js +649 -0
- package/dist/esm/import/SpriterImporter.js.map +1 -0
- package/dist/esm/import/TiledMapImporter.js +857 -0
- package/dist/esm/import/TiledMapImporter.js.map +1 -0
- package/dist/esm/import/UnitySceneImporter.js +730 -0
- package/dist/esm/import/UnitySceneImporter.js.map +1 -0
- package/dist/esm/import/index.js +279 -0
- package/dist/esm/import/index.js.map +1 -0
- package/dist/esm/index.js +47 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/integration/IconSprite.js +266 -0
- package/dist/esm/integration/IconSprite.js.map +1 -0
- package/dist/esm/inventory/InventorySystem.js +924 -0
- package/dist/esm/inventory/InventorySystem.js.map +1 -0
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -1
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -1
- package/dist/esm/quest/QuestSystem.js +916 -0
- package/dist/esm/quest/QuestSystem.js.map +1 -0
- package/dist/esm/rendering/WebGPURenderPipeline.js +642 -0
- package/dist/esm/rendering/WebGPURenderPipeline.js.map +1 -0
- package/dist/esm/scripting/GraphToAST.js +564 -0
- package/dist/esm/scripting/GraphToAST.js.map +1 -0
- package/dist/esm/scripting/LanguageExporter.js +311 -0
- package/dist/esm/scripting/LanguageExporter.js.map +1 -0
- package/dist/esm/scripting/ScriptAST.js +52 -0
- package/dist/esm/scripting/ScriptAST.js.map +1 -0
- package/dist/esm/scripting/VisualScripting2.js +1130 -0
- package/dist/esm/scripting/VisualScripting2.js.map +1 -0
- package/dist/esm/scripting/exporters/CSharpExporter.js +501 -0
- package/dist/esm/scripting/exporters/CSharpExporter.js.map +1 -0
- package/dist/esm/scripting/exporters/GDScriptExporter.js +450 -0
- package/dist/esm/scripting/exporters/GDScriptExporter.js.map +1 -0
- package/dist/esm/scripting/exporters/LuaExporter.js +455 -0
- package/dist/esm/scripting/exporters/LuaExporter.js.map +1 -0
- package/dist/esm/scripting/exporters/PythonExporter.js +563 -0
- package/dist/esm/scripting/exporters/PythonExporter.js.map +1 -0
- package/dist/esm/scripting/exporters/RustExporter.js +523 -0
- package/dist/esm/scripting/exporters/RustExporter.js.map +1 -0
- package/dist/esm/scripting/exporters/TypeScriptExporter.js +568 -0
- package/dist/esm/scripting/exporters/TypeScriptExporter.js.map +1 -0
- package/dist/esm/systems/ParticleSystem2.js +1471 -0
- package/dist/esm/systems/ParticleSystem2.js.map +1 -0
- package/dist/esm/xr/ARVR.js.map +1 -1
- package/dist/types/__tests__/setup.d.ts +1 -1
- package/dist/types/ai/BehaviorTree.d.ts +375 -0
- package/dist/types/ai/StateMachine.d.ts +296 -0
- package/dist/types/audio/AudioBridge.d.ts +199 -0
- package/dist/types/devtools/GameplayAnalytics.d.ts +279 -0
- package/dist/types/dialogue/DialogueSystem.d.ts +326 -0
- package/dist/types/dialogue/index.d.ts +2 -0
- package/dist/types/editor/NiceGameEditor.d.ts +12 -1
- package/dist/types/editor/ShaderGraph.d.ts +207 -0
- package/dist/types/editor/TimelineEditor.d.ts +393 -0
- package/dist/types/engine/SaveSystemV2.d.ts +155 -0
- package/dist/types/export/GodotExporter.d.ts +56 -0
- package/dist/types/export/PlatformExporter.d.ts +201 -0
- package/dist/types/export/ThreeJSExporter.d.ts +40 -0
- package/dist/types/export/UnityExporter.d.ts +69 -0
- package/dist/types/export/WebExporter.d.ts +58 -0
- package/dist/types/export/index.d.ts +19 -0
- package/dist/types/import/AsepriteImporter.d.ts +46 -0
- package/dist/types/import/DragonBonesImporter.d.ts +331 -0
- package/dist/types/import/GameMakerImporter.d.ts +375 -0
- package/dist/types/import/GodotSceneImporter.d.ts +34 -0
- package/dist/types/import/LDtkImporter.d.ts +177 -0
- package/dist/types/import/Live2DImporter.d.ts +237 -0
- package/dist/types/import/NdgFormat.d.ts +387 -0
- package/dist/types/import/OgmoImporter.d.ts +237 -0
- package/dist/types/import/RPGMakerImporter.d.ts +186 -0
- package/dist/types/import/SceneImporter.d.ts +276 -0
- package/dist/types/import/SpineImporter.d.ts +372 -0
- package/dist/types/import/SpriterImporter.d.ts +230 -0
- package/dist/types/import/TiledMapImporter.d.ts +57 -0
- package/dist/types/import/UnitySceneImporter.d.ts +87 -0
- package/dist/types/import/index.d.ts +59 -0
- package/dist/types/index.d.ts +46 -18
- package/dist/types/integration/IconSprite.d.ts +196 -0
- package/dist/types/inventory/InventorySystem.d.ts +336 -0
- package/dist/types/performance/WebGPUCompute.d.ts +0 -10
- package/dist/types/quest/QuestSystem.d.ts +287 -0
- package/dist/types/rendering/WebGPURenderPipeline.d.ts +255 -0
- package/dist/types/scripting/GraphToAST.d.ts +55 -0
- package/dist/types/scripting/LanguageExporter.d.ts +136 -0
- package/dist/types/scripting/ScriptAST.d.ts +312 -0
- package/dist/types/scripting/VisualScripting2.d.ts +353 -0
- package/dist/types/scripting/exporters/CSharpExporter.d.ts +44 -0
- package/dist/types/scripting/exporters/GDScriptExporter.d.ts +46 -0
- package/dist/types/scripting/exporters/LuaExporter.d.ts +46 -0
- package/dist/types/scripting/exporters/PythonExporter.d.ts +49 -0
- package/dist/types/scripting/exporters/RustExporter.d.ts +46 -0
- package/dist/types/scripting/exporters/TypeScriptExporter.d.ts +48 -0
- package/dist/types/scripting/exporters/index.d.ts +8 -0
- package/dist/types/scripting/index.d.ts +11 -0
- package/dist/types/systems/ParticleSystem2.d.ts +646 -0
- package/package.json +7 -1
|
@@ -0,0 +1,1186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file BehaviorTree.ts
|
|
3
|
+
* @description Behavior Tree System with Graphical Editor Support
|
|
4
|
+
* PRO-1.4: Professional Editor Features
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Full behavior tree implementation
|
|
8
|
+
* - Composite nodes (Selector, Sequence, Parallel)
|
|
9
|
+
* - Decorator nodes (Inverter, Repeater, Succeeder, etc.)
|
|
10
|
+
* - Leaf nodes (Actions, Conditions)
|
|
11
|
+
* - Real-time preview and debugging
|
|
12
|
+
* - Blackboard system for shared data
|
|
13
|
+
* - Subtree support
|
|
14
|
+
* - Serialization support
|
|
15
|
+
*/
|
|
16
|
+
/** Blackboard for sharing data between nodes */
|
|
17
|
+
class BTBlackboard {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.data = new Map();
|
|
20
|
+
this.types = new Map();
|
|
21
|
+
this.listeners = new Map();
|
|
22
|
+
}
|
|
23
|
+
/** Set a value in the blackboard */
|
|
24
|
+
set(key, value) {
|
|
25
|
+
const name = typeof key === 'string' ? key : key.name;
|
|
26
|
+
const oldValue = this.data.get(name);
|
|
27
|
+
this.data.set(name, value);
|
|
28
|
+
if (typeof key !== 'string') {
|
|
29
|
+
this.types.set(name, key.type);
|
|
30
|
+
}
|
|
31
|
+
// Notify listeners
|
|
32
|
+
if (oldValue !== value) {
|
|
33
|
+
const callbacks = this.listeners.get(name);
|
|
34
|
+
if (callbacks) {
|
|
35
|
+
for (const cb of callbacks) {
|
|
36
|
+
cb(value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Get a value from the blackboard */
|
|
42
|
+
get(key) {
|
|
43
|
+
const name = typeof key === 'string' ? key : key.name;
|
|
44
|
+
const value = this.data.get(name);
|
|
45
|
+
if (value === undefined && typeof key !== 'string' && key.defaultValue !== undefined) {
|
|
46
|
+
return key.defaultValue;
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
/** Check if key exists */
|
|
51
|
+
has(key) {
|
|
52
|
+
const name = typeof key === 'string' ? key : key.name;
|
|
53
|
+
return this.data.has(name);
|
|
54
|
+
}
|
|
55
|
+
/** Remove a key */
|
|
56
|
+
delete(key) {
|
|
57
|
+
const name = typeof key === 'string' ? key : key.name;
|
|
58
|
+
this.data.delete(name);
|
|
59
|
+
this.types.delete(name);
|
|
60
|
+
}
|
|
61
|
+
/** Clear all data */
|
|
62
|
+
clear() {
|
|
63
|
+
this.data.clear();
|
|
64
|
+
}
|
|
65
|
+
/** Subscribe to key changes */
|
|
66
|
+
subscribe(key, callback) {
|
|
67
|
+
const name = typeof key === 'string' ? key : key.name;
|
|
68
|
+
let callbacks = this.listeners.get(name);
|
|
69
|
+
if (!callbacks) {
|
|
70
|
+
callbacks = [];
|
|
71
|
+
this.listeners.set(name, callbacks);
|
|
72
|
+
}
|
|
73
|
+
callbacks.push(callback);
|
|
74
|
+
// Return unsubscribe function
|
|
75
|
+
return () => {
|
|
76
|
+
const idx = callbacks.indexOf(callback);
|
|
77
|
+
if (idx >= 0) {
|
|
78
|
+
callbacks.splice(idx, 1);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** Get all keys */
|
|
83
|
+
keys() {
|
|
84
|
+
return Array.from(this.data.keys());
|
|
85
|
+
}
|
|
86
|
+
/** Clone the blackboard */
|
|
87
|
+
clone() {
|
|
88
|
+
const bb = new BTBlackboard();
|
|
89
|
+
for (const [key, value] of this.data) {
|
|
90
|
+
bb.data.set(key, value);
|
|
91
|
+
}
|
|
92
|
+
for (const [key, type] of this.types) {
|
|
93
|
+
bb.types.set(key, type);
|
|
94
|
+
}
|
|
95
|
+
return bb;
|
|
96
|
+
}
|
|
97
|
+
/** Export to JSON */
|
|
98
|
+
toJSON() {
|
|
99
|
+
const result = {};
|
|
100
|
+
for (const [key, value] of this.data) {
|
|
101
|
+
result[key] = value;
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
/** Import from JSON */
|
|
106
|
+
fromJSON(data) {
|
|
107
|
+
for (const [key, value] of Object.entries(data)) {
|
|
108
|
+
this.data.set(key, value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Base class for all behavior tree nodes */
|
|
113
|
+
class BTNode {
|
|
114
|
+
constructor(type, category, name) {
|
|
115
|
+
/** Position in editor */
|
|
116
|
+
this.position = { x: 0, y: 0 };
|
|
117
|
+
/** Custom properties */
|
|
118
|
+
this.properties = {};
|
|
119
|
+
/** Current execution status (for debugging) */
|
|
120
|
+
this.lastStatus = 'inactive';
|
|
121
|
+
/** Last execution time (for profiling) */
|
|
122
|
+
this.lastExecutionTime = 0;
|
|
123
|
+
this.id = `bt_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
124
|
+
this.type = type;
|
|
125
|
+
this.category = category;
|
|
126
|
+
this.name = name;
|
|
127
|
+
}
|
|
128
|
+
/** Initialize the node */
|
|
129
|
+
initialize(context) { }
|
|
130
|
+
/** Reset the node state */
|
|
131
|
+
reset() {
|
|
132
|
+
this.lastStatus = 'inactive';
|
|
133
|
+
}
|
|
134
|
+
/** Called when execution is aborted */
|
|
135
|
+
abort() {
|
|
136
|
+
this.lastStatus = 'aborted';
|
|
137
|
+
}
|
|
138
|
+
/** Serialize to JSON */
|
|
139
|
+
toJSON() {
|
|
140
|
+
return {
|
|
141
|
+
id: this.id,
|
|
142
|
+
type: this.type,
|
|
143
|
+
category: this.category,
|
|
144
|
+
name: this.name,
|
|
145
|
+
position: this.position,
|
|
146
|
+
properties: this.properties,
|
|
147
|
+
comment: this.comment,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// ============================================================
|
|
152
|
+
// Composite Nodes
|
|
153
|
+
// ============================================================
|
|
154
|
+
/** Base class for composite nodes (have children) */
|
|
155
|
+
class BTComposite extends BTNode {
|
|
156
|
+
constructor(type, name) {
|
|
157
|
+
super(type, 'composite', name);
|
|
158
|
+
this.children = [];
|
|
159
|
+
this.currentChild = 0;
|
|
160
|
+
}
|
|
161
|
+
addChild(child) {
|
|
162
|
+
this.children.push(child);
|
|
163
|
+
}
|
|
164
|
+
removeChild(child) {
|
|
165
|
+
const idx = this.children.indexOf(child);
|
|
166
|
+
if (idx >= 0) {
|
|
167
|
+
this.children.splice(idx, 1);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
reset() {
|
|
171
|
+
super.reset();
|
|
172
|
+
this.currentChild = 0;
|
|
173
|
+
for (const child of this.children) {
|
|
174
|
+
child.reset();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
abort() {
|
|
178
|
+
super.abort();
|
|
179
|
+
// Abort currently running child
|
|
180
|
+
if (this.currentChild < this.children.length) {
|
|
181
|
+
this.children[this.currentChild].abort();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
toJSON() {
|
|
185
|
+
return {
|
|
186
|
+
...super.toJSON(),
|
|
187
|
+
children: this.children.map((c) => c.toJSON()),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** Sequence: Runs children in order until one fails */
|
|
192
|
+
class BTSequence extends BTComposite {
|
|
193
|
+
constructor(name = 'Sequence') {
|
|
194
|
+
super('sequence', name);
|
|
195
|
+
}
|
|
196
|
+
tick(context) {
|
|
197
|
+
while (this.currentChild < this.children.length) {
|
|
198
|
+
const child = this.children[this.currentChild];
|
|
199
|
+
const status = child.tick(context);
|
|
200
|
+
child.lastStatus = status;
|
|
201
|
+
if (status === 'running') {
|
|
202
|
+
this.lastStatus = 'running';
|
|
203
|
+
return 'running';
|
|
204
|
+
}
|
|
205
|
+
if (status === 'failure') {
|
|
206
|
+
this.currentChild = 0;
|
|
207
|
+
this.lastStatus = 'failure';
|
|
208
|
+
return 'failure';
|
|
209
|
+
}
|
|
210
|
+
this.currentChild++;
|
|
211
|
+
}
|
|
212
|
+
this.currentChild = 0;
|
|
213
|
+
this.lastStatus = 'success';
|
|
214
|
+
return 'success';
|
|
215
|
+
}
|
|
216
|
+
clone() {
|
|
217
|
+
const node = new BTSequence(this.name);
|
|
218
|
+
node.properties = { ...this.properties };
|
|
219
|
+
node.position = { ...this.position };
|
|
220
|
+
node.children = this.children.map((c) => c.clone());
|
|
221
|
+
return node;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** Selector: Runs children until one succeeds */
|
|
225
|
+
class BTSelector extends BTComposite {
|
|
226
|
+
constructor(name = 'Selector') {
|
|
227
|
+
super('selector', name);
|
|
228
|
+
}
|
|
229
|
+
tick(context) {
|
|
230
|
+
while (this.currentChild < this.children.length) {
|
|
231
|
+
const child = this.children[this.currentChild];
|
|
232
|
+
const status = child.tick(context);
|
|
233
|
+
child.lastStatus = status;
|
|
234
|
+
if (status === 'running') {
|
|
235
|
+
this.lastStatus = 'running';
|
|
236
|
+
return 'running';
|
|
237
|
+
}
|
|
238
|
+
if (status === 'success') {
|
|
239
|
+
this.currentChild = 0;
|
|
240
|
+
this.lastStatus = 'success';
|
|
241
|
+
return 'success';
|
|
242
|
+
}
|
|
243
|
+
this.currentChild++;
|
|
244
|
+
}
|
|
245
|
+
this.currentChild = 0;
|
|
246
|
+
this.lastStatus = 'failure';
|
|
247
|
+
return 'failure';
|
|
248
|
+
}
|
|
249
|
+
clone() {
|
|
250
|
+
const node = new BTSelector(this.name);
|
|
251
|
+
node.properties = { ...this.properties };
|
|
252
|
+
node.position = { ...this.position };
|
|
253
|
+
node.children = this.children.map((c) => c.clone());
|
|
254
|
+
return node;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** Parallel: Runs all children simultaneously */
|
|
258
|
+
class BTParallel extends BTComposite {
|
|
259
|
+
constructor(name = 'Parallel') {
|
|
260
|
+
super('parallel', name);
|
|
261
|
+
/** Policy for determining success */
|
|
262
|
+
this.successPolicy = 'all';
|
|
263
|
+
/** Policy for determining failure */
|
|
264
|
+
this.failurePolicy = 'one';
|
|
265
|
+
this.childStatuses = [];
|
|
266
|
+
}
|
|
267
|
+
initialize(context) {
|
|
268
|
+
this.childStatuses = new Array(this.children.length).fill('running');
|
|
269
|
+
}
|
|
270
|
+
tick(context) {
|
|
271
|
+
let successCount = 0;
|
|
272
|
+
let failureCount = 0;
|
|
273
|
+
let runningCount = 0;
|
|
274
|
+
for (let i = 0; i < this.children.length; i++) {
|
|
275
|
+
// Skip already completed children
|
|
276
|
+
if (this.childStatuses[i] !== 'running') {
|
|
277
|
+
if (this.childStatuses[i] === 'success')
|
|
278
|
+
successCount++;
|
|
279
|
+
else if (this.childStatuses[i] === 'failure')
|
|
280
|
+
failureCount++;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const child = this.children[i];
|
|
284
|
+
const status = child.tick(context);
|
|
285
|
+
child.lastStatus = status;
|
|
286
|
+
this.childStatuses[i] = status;
|
|
287
|
+
if (status === 'success')
|
|
288
|
+
successCount++;
|
|
289
|
+
else if (status === 'failure')
|
|
290
|
+
failureCount++;
|
|
291
|
+
else
|
|
292
|
+
runningCount++;
|
|
293
|
+
}
|
|
294
|
+
// Check failure policy
|
|
295
|
+
if (this.failurePolicy === 'one' && failureCount > 0) {
|
|
296
|
+
this.reset();
|
|
297
|
+
this.lastStatus = 'failure';
|
|
298
|
+
return 'failure';
|
|
299
|
+
}
|
|
300
|
+
if (this.failurePolicy === 'all' && failureCount === this.children.length) {
|
|
301
|
+
this.reset();
|
|
302
|
+
this.lastStatus = 'failure';
|
|
303
|
+
return 'failure';
|
|
304
|
+
}
|
|
305
|
+
// Check success policy
|
|
306
|
+
if (this.successPolicy === 'one' && successCount > 0) {
|
|
307
|
+
this.reset();
|
|
308
|
+
this.lastStatus = 'success';
|
|
309
|
+
return 'success';
|
|
310
|
+
}
|
|
311
|
+
if (this.successPolicy === 'all' && successCount === this.children.length) {
|
|
312
|
+
this.reset();
|
|
313
|
+
this.lastStatus = 'success';
|
|
314
|
+
return 'success';
|
|
315
|
+
}
|
|
316
|
+
// Still running
|
|
317
|
+
if (runningCount > 0) {
|
|
318
|
+
this.lastStatus = 'running';
|
|
319
|
+
return 'running';
|
|
320
|
+
}
|
|
321
|
+
// Mixed results, depends on policy
|
|
322
|
+
this.reset();
|
|
323
|
+
this.lastStatus = 'failure';
|
|
324
|
+
return 'failure';
|
|
325
|
+
}
|
|
326
|
+
reset() {
|
|
327
|
+
super.reset();
|
|
328
|
+
this.childStatuses = [];
|
|
329
|
+
}
|
|
330
|
+
clone() {
|
|
331
|
+
const node = new BTParallel(this.name);
|
|
332
|
+
node.properties = { ...this.properties };
|
|
333
|
+
node.position = { ...this.position };
|
|
334
|
+
node.successPolicy = this.successPolicy;
|
|
335
|
+
node.failurePolicy = this.failurePolicy;
|
|
336
|
+
node.children = this.children.map((c) => c.clone());
|
|
337
|
+
return node;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
/** Random Selector: Selects children in random order */
|
|
341
|
+
class BTRandomSelector extends BTComposite {
|
|
342
|
+
constructor(name = 'Random Selector') {
|
|
343
|
+
super('randomSelector', name);
|
|
344
|
+
this.shuffledOrder = [];
|
|
345
|
+
}
|
|
346
|
+
initialize(context) {
|
|
347
|
+
this.shuffle();
|
|
348
|
+
}
|
|
349
|
+
shuffle() {
|
|
350
|
+
this.shuffledOrder = this.children.map((_, i) => i);
|
|
351
|
+
for (let i = this.shuffledOrder.length - 1; i > 0; i--) {
|
|
352
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
353
|
+
[this.shuffledOrder[i], this.shuffledOrder[j]] = [
|
|
354
|
+
this.shuffledOrder[j],
|
|
355
|
+
this.shuffledOrder[i],
|
|
356
|
+
];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
tick(context) {
|
|
360
|
+
while (this.currentChild < this.shuffledOrder.length) {
|
|
361
|
+
const childIdx = this.shuffledOrder[this.currentChild];
|
|
362
|
+
const child = this.children[childIdx];
|
|
363
|
+
const status = child.tick(context);
|
|
364
|
+
child.lastStatus = status;
|
|
365
|
+
if (status === 'running') {
|
|
366
|
+
this.lastStatus = 'running';
|
|
367
|
+
return 'running';
|
|
368
|
+
}
|
|
369
|
+
if (status === 'success') {
|
|
370
|
+
this.currentChild = 0;
|
|
371
|
+
this.shuffle();
|
|
372
|
+
this.lastStatus = 'success';
|
|
373
|
+
return 'success';
|
|
374
|
+
}
|
|
375
|
+
this.currentChild++;
|
|
376
|
+
}
|
|
377
|
+
this.currentChild = 0;
|
|
378
|
+
this.shuffle();
|
|
379
|
+
this.lastStatus = 'failure';
|
|
380
|
+
return 'failure';
|
|
381
|
+
}
|
|
382
|
+
clone() {
|
|
383
|
+
const node = new BTRandomSelector(this.name);
|
|
384
|
+
node.properties = { ...this.properties };
|
|
385
|
+
node.position = { ...this.position };
|
|
386
|
+
node.children = this.children.map((c) => c.clone());
|
|
387
|
+
return node;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/** Random Sequence: Runs children in random order */
|
|
391
|
+
class BTRandomSequence extends BTComposite {
|
|
392
|
+
constructor(name = 'Random Sequence') {
|
|
393
|
+
super('randomSequence', name);
|
|
394
|
+
this.shuffledOrder = [];
|
|
395
|
+
}
|
|
396
|
+
initialize(context) {
|
|
397
|
+
this.shuffle();
|
|
398
|
+
}
|
|
399
|
+
shuffle() {
|
|
400
|
+
this.shuffledOrder = this.children.map((_, i) => i);
|
|
401
|
+
for (let i = this.shuffledOrder.length - 1; i > 0; i--) {
|
|
402
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
403
|
+
[this.shuffledOrder[i], this.shuffledOrder[j]] = [
|
|
404
|
+
this.shuffledOrder[j],
|
|
405
|
+
this.shuffledOrder[i],
|
|
406
|
+
];
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
tick(context) {
|
|
410
|
+
while (this.currentChild < this.shuffledOrder.length) {
|
|
411
|
+
const childIdx = this.shuffledOrder[this.currentChild];
|
|
412
|
+
const child = this.children[childIdx];
|
|
413
|
+
const status = child.tick(context);
|
|
414
|
+
child.lastStatus = status;
|
|
415
|
+
if (status === 'running') {
|
|
416
|
+
this.lastStatus = 'running';
|
|
417
|
+
return 'running';
|
|
418
|
+
}
|
|
419
|
+
if (status === 'failure') {
|
|
420
|
+
this.currentChild = 0;
|
|
421
|
+
this.shuffle();
|
|
422
|
+
this.lastStatus = 'failure';
|
|
423
|
+
return 'failure';
|
|
424
|
+
}
|
|
425
|
+
this.currentChild++;
|
|
426
|
+
}
|
|
427
|
+
this.currentChild = 0;
|
|
428
|
+
this.shuffle();
|
|
429
|
+
this.lastStatus = 'success';
|
|
430
|
+
return 'success';
|
|
431
|
+
}
|
|
432
|
+
clone() {
|
|
433
|
+
const node = new BTRandomSequence(this.name);
|
|
434
|
+
node.properties = { ...this.properties };
|
|
435
|
+
node.position = { ...this.position };
|
|
436
|
+
node.children = this.children.map((c) => c.clone());
|
|
437
|
+
return node;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
// ============================================================
|
|
441
|
+
// Decorator Nodes
|
|
442
|
+
// ============================================================
|
|
443
|
+
/** Base class for decorator nodes (single child) */
|
|
444
|
+
class BTDecorator extends BTNode {
|
|
445
|
+
constructor(type, name) {
|
|
446
|
+
super(type, 'decorator', name);
|
|
447
|
+
this.child = null;
|
|
448
|
+
}
|
|
449
|
+
setChild(child) {
|
|
450
|
+
this.child = child;
|
|
451
|
+
}
|
|
452
|
+
reset() {
|
|
453
|
+
var _a;
|
|
454
|
+
super.reset();
|
|
455
|
+
(_a = this.child) === null || _a === void 0 ? void 0 : _a.reset();
|
|
456
|
+
}
|
|
457
|
+
abort() {
|
|
458
|
+
var _a;
|
|
459
|
+
super.abort();
|
|
460
|
+
(_a = this.child) === null || _a === void 0 ? void 0 : _a.abort();
|
|
461
|
+
}
|
|
462
|
+
toJSON() {
|
|
463
|
+
return {
|
|
464
|
+
...super.toJSON(),
|
|
465
|
+
children: this.child ? [this.child.toJSON()] : [],
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/** Inverter: Inverts child's result */
|
|
470
|
+
class BTInverter extends BTDecorator {
|
|
471
|
+
constructor(name = 'Inverter') {
|
|
472
|
+
super('inverter', name);
|
|
473
|
+
}
|
|
474
|
+
tick(context) {
|
|
475
|
+
if (!this.child) {
|
|
476
|
+
this.lastStatus = 'failure';
|
|
477
|
+
return 'failure';
|
|
478
|
+
}
|
|
479
|
+
const status = this.child.tick(context);
|
|
480
|
+
this.child.lastStatus = status;
|
|
481
|
+
if (status === 'running') {
|
|
482
|
+
this.lastStatus = 'running';
|
|
483
|
+
return 'running';
|
|
484
|
+
}
|
|
485
|
+
const inverted = status === 'success' ? 'failure' : 'success';
|
|
486
|
+
this.lastStatus = inverted;
|
|
487
|
+
return inverted;
|
|
488
|
+
}
|
|
489
|
+
clone() {
|
|
490
|
+
var _a;
|
|
491
|
+
const node = new BTInverter(this.name);
|
|
492
|
+
node.properties = { ...this.properties };
|
|
493
|
+
node.position = { ...this.position };
|
|
494
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
495
|
+
return node;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/** Succeeder: Always returns success */
|
|
499
|
+
class BTSucceeder extends BTDecorator {
|
|
500
|
+
constructor(name = 'Succeeder') {
|
|
501
|
+
super('succeeder', name);
|
|
502
|
+
}
|
|
503
|
+
tick(context) {
|
|
504
|
+
if (!this.child) {
|
|
505
|
+
this.lastStatus = 'success';
|
|
506
|
+
return 'success';
|
|
507
|
+
}
|
|
508
|
+
const status = this.child.tick(context);
|
|
509
|
+
this.child.lastStatus = status;
|
|
510
|
+
if (status === 'running') {
|
|
511
|
+
this.lastStatus = 'running';
|
|
512
|
+
return 'running';
|
|
513
|
+
}
|
|
514
|
+
this.lastStatus = 'success';
|
|
515
|
+
return 'success';
|
|
516
|
+
}
|
|
517
|
+
clone() {
|
|
518
|
+
var _a;
|
|
519
|
+
const node = new BTSucceeder(this.name);
|
|
520
|
+
node.properties = { ...this.properties };
|
|
521
|
+
node.position = { ...this.position };
|
|
522
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
523
|
+
return node;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/** Failer: Always returns failure */
|
|
527
|
+
class BTFailer extends BTDecorator {
|
|
528
|
+
constructor(name = 'Failer') {
|
|
529
|
+
super('failer', name);
|
|
530
|
+
}
|
|
531
|
+
tick(context) {
|
|
532
|
+
if (!this.child) {
|
|
533
|
+
this.lastStatus = 'failure';
|
|
534
|
+
return 'failure';
|
|
535
|
+
}
|
|
536
|
+
const status = this.child.tick(context);
|
|
537
|
+
this.child.lastStatus = status;
|
|
538
|
+
if (status === 'running') {
|
|
539
|
+
this.lastStatus = 'running';
|
|
540
|
+
return 'running';
|
|
541
|
+
}
|
|
542
|
+
this.lastStatus = 'failure';
|
|
543
|
+
return 'failure';
|
|
544
|
+
}
|
|
545
|
+
clone() {
|
|
546
|
+
var _a;
|
|
547
|
+
const node = new BTFailer(this.name);
|
|
548
|
+
node.properties = { ...this.properties };
|
|
549
|
+
node.position = { ...this.position };
|
|
550
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
551
|
+
return node;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
/** Repeater: Repeats child N times */
|
|
555
|
+
class BTRepeater extends BTDecorator {
|
|
556
|
+
constructor(name = 'Repeater', times = -1) {
|
|
557
|
+
super('repeater', name);
|
|
558
|
+
this.times = -1; // -1 = infinite
|
|
559
|
+
this.currentCount = 0;
|
|
560
|
+
this.times = times;
|
|
561
|
+
}
|
|
562
|
+
tick(context) {
|
|
563
|
+
if (!this.child) {
|
|
564
|
+
this.lastStatus = 'failure';
|
|
565
|
+
return 'failure';
|
|
566
|
+
}
|
|
567
|
+
while (this.times === -1 || this.currentCount < this.times) {
|
|
568
|
+
const status = this.child.tick(context);
|
|
569
|
+
this.child.lastStatus = status;
|
|
570
|
+
if (status === 'running') {
|
|
571
|
+
this.lastStatus = 'running';
|
|
572
|
+
return 'running';
|
|
573
|
+
}
|
|
574
|
+
this.currentCount++;
|
|
575
|
+
this.child.reset();
|
|
576
|
+
}
|
|
577
|
+
this.currentCount = 0;
|
|
578
|
+
this.lastStatus = 'success';
|
|
579
|
+
return 'success';
|
|
580
|
+
}
|
|
581
|
+
reset() {
|
|
582
|
+
super.reset();
|
|
583
|
+
this.currentCount = 0;
|
|
584
|
+
}
|
|
585
|
+
clone() {
|
|
586
|
+
var _a;
|
|
587
|
+
const node = new BTRepeater(this.name, this.times);
|
|
588
|
+
node.properties = { ...this.properties };
|
|
589
|
+
node.position = { ...this.position };
|
|
590
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
591
|
+
return node;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
/** RepeatUntilFail: Repeats until child fails */
|
|
595
|
+
class BTRepeatUntilFail extends BTDecorator {
|
|
596
|
+
constructor(name = 'Repeat Until Fail') {
|
|
597
|
+
super('repeatUntilFail', name);
|
|
598
|
+
}
|
|
599
|
+
tick(context) {
|
|
600
|
+
if (!this.child) {
|
|
601
|
+
this.lastStatus = 'failure';
|
|
602
|
+
return 'failure';
|
|
603
|
+
}
|
|
604
|
+
const status = this.child.tick(context);
|
|
605
|
+
this.child.lastStatus = status;
|
|
606
|
+
if (status === 'failure') {
|
|
607
|
+
this.lastStatus = 'success';
|
|
608
|
+
return 'success';
|
|
609
|
+
}
|
|
610
|
+
if (status === 'running') {
|
|
611
|
+
this.lastStatus = 'running';
|
|
612
|
+
return 'running';
|
|
613
|
+
}
|
|
614
|
+
// Success - reset and continue
|
|
615
|
+
this.child.reset();
|
|
616
|
+
this.lastStatus = 'running';
|
|
617
|
+
return 'running';
|
|
618
|
+
}
|
|
619
|
+
clone() {
|
|
620
|
+
var _a;
|
|
621
|
+
const node = new BTRepeatUntilFail(this.name);
|
|
622
|
+
node.properties = { ...this.properties };
|
|
623
|
+
node.position = { ...this.position };
|
|
624
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
625
|
+
return node;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
/** RepeatUntilSuccess: Repeats until child succeeds */
|
|
629
|
+
class BTRepeatUntilSuccess extends BTDecorator {
|
|
630
|
+
constructor(name = 'Repeat Until Success') {
|
|
631
|
+
super('repeatUntilSuccess', name);
|
|
632
|
+
}
|
|
633
|
+
tick(context) {
|
|
634
|
+
if (!this.child) {
|
|
635
|
+
this.lastStatus = 'failure';
|
|
636
|
+
return 'failure';
|
|
637
|
+
}
|
|
638
|
+
const status = this.child.tick(context);
|
|
639
|
+
this.child.lastStatus = status;
|
|
640
|
+
if (status === 'success') {
|
|
641
|
+
this.lastStatus = 'success';
|
|
642
|
+
return 'success';
|
|
643
|
+
}
|
|
644
|
+
if (status === 'running') {
|
|
645
|
+
this.lastStatus = 'running';
|
|
646
|
+
return 'running';
|
|
647
|
+
}
|
|
648
|
+
// Failure - reset and continue
|
|
649
|
+
this.child.reset();
|
|
650
|
+
this.lastStatus = 'running';
|
|
651
|
+
return 'running';
|
|
652
|
+
}
|
|
653
|
+
clone() {
|
|
654
|
+
var _a;
|
|
655
|
+
const node = new BTRepeatUntilSuccess(this.name);
|
|
656
|
+
node.properties = { ...this.properties };
|
|
657
|
+
node.position = { ...this.position };
|
|
658
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
659
|
+
return node;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
/** Timeout: Fails if child takes too long */
|
|
663
|
+
class BTTimeout extends BTDecorator {
|
|
664
|
+
constructor(name = 'Timeout', seconds = 5) {
|
|
665
|
+
super('timeout', name);
|
|
666
|
+
this.elapsed = 0;
|
|
667
|
+
this.started = false;
|
|
668
|
+
this.timeoutSeconds = seconds;
|
|
669
|
+
}
|
|
670
|
+
tick(context) {
|
|
671
|
+
if (!this.child) {
|
|
672
|
+
this.lastStatus = 'failure';
|
|
673
|
+
return 'failure';
|
|
674
|
+
}
|
|
675
|
+
if (!this.started) {
|
|
676
|
+
this.elapsed = 0;
|
|
677
|
+
this.started = true;
|
|
678
|
+
}
|
|
679
|
+
this.elapsed += context.dt;
|
|
680
|
+
if (this.elapsed >= this.timeoutSeconds) {
|
|
681
|
+
this.child.abort();
|
|
682
|
+
this.started = false;
|
|
683
|
+
this.lastStatus = 'failure';
|
|
684
|
+
return 'failure';
|
|
685
|
+
}
|
|
686
|
+
const status = this.child.tick(context);
|
|
687
|
+
this.child.lastStatus = status;
|
|
688
|
+
if (status !== 'running') {
|
|
689
|
+
this.started = false;
|
|
690
|
+
}
|
|
691
|
+
this.lastStatus = status;
|
|
692
|
+
return status;
|
|
693
|
+
}
|
|
694
|
+
reset() {
|
|
695
|
+
super.reset();
|
|
696
|
+
this.elapsed = 0;
|
|
697
|
+
this.started = false;
|
|
698
|
+
}
|
|
699
|
+
clone() {
|
|
700
|
+
var _a;
|
|
701
|
+
const node = new BTTimeout(this.name, this.timeoutSeconds);
|
|
702
|
+
node.properties = { ...this.properties };
|
|
703
|
+
node.position = { ...this.position };
|
|
704
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
705
|
+
return node;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
/** Cooldown: Rate limits child execution */
|
|
709
|
+
class BTCooldown extends BTDecorator {
|
|
710
|
+
constructor(name = 'Cooldown', seconds = 1) {
|
|
711
|
+
super('cooldown', name);
|
|
712
|
+
this.lastExecution = -Infinity;
|
|
713
|
+
this.cooldownSeconds = seconds;
|
|
714
|
+
}
|
|
715
|
+
tick(context) {
|
|
716
|
+
if (!this.child) {
|
|
717
|
+
this.lastStatus = 'failure';
|
|
718
|
+
return 'failure';
|
|
719
|
+
}
|
|
720
|
+
const elapsed = context.time - this.lastExecution;
|
|
721
|
+
if (elapsed < this.cooldownSeconds) {
|
|
722
|
+
this.lastStatus = 'failure';
|
|
723
|
+
return 'failure';
|
|
724
|
+
}
|
|
725
|
+
const status = this.child.tick(context);
|
|
726
|
+
this.child.lastStatus = status;
|
|
727
|
+
if (status !== 'running') {
|
|
728
|
+
this.lastExecution = context.time;
|
|
729
|
+
}
|
|
730
|
+
this.lastStatus = status;
|
|
731
|
+
return status;
|
|
732
|
+
}
|
|
733
|
+
reset() {
|
|
734
|
+
super.reset();
|
|
735
|
+
// Don't reset lastExecution - cooldown should persist
|
|
736
|
+
}
|
|
737
|
+
clone() {
|
|
738
|
+
var _a;
|
|
739
|
+
const node = new BTCooldown(this.name, this.cooldownSeconds);
|
|
740
|
+
node.properties = { ...this.properties };
|
|
741
|
+
node.position = { ...this.position };
|
|
742
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
743
|
+
return node;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/** Condition Guard: Only runs child if condition is true */
|
|
747
|
+
class BTConditionGuard extends BTDecorator {
|
|
748
|
+
constructor(name = 'Guard', condition = () => true) {
|
|
749
|
+
super('guard', name);
|
|
750
|
+
this.condition = condition;
|
|
751
|
+
}
|
|
752
|
+
tick(context) {
|
|
753
|
+
if (!this.child) {
|
|
754
|
+
this.lastStatus = 'failure';
|
|
755
|
+
return 'failure';
|
|
756
|
+
}
|
|
757
|
+
if (!this.condition(context)) {
|
|
758
|
+
this.lastStatus = 'failure';
|
|
759
|
+
return 'failure';
|
|
760
|
+
}
|
|
761
|
+
const status = this.child.tick(context);
|
|
762
|
+
this.child.lastStatus = status;
|
|
763
|
+
this.lastStatus = status;
|
|
764
|
+
return status;
|
|
765
|
+
}
|
|
766
|
+
clone() {
|
|
767
|
+
var _a;
|
|
768
|
+
const node = new BTConditionGuard(this.name, this.condition);
|
|
769
|
+
node.properties = { ...this.properties };
|
|
770
|
+
node.position = { ...this.position };
|
|
771
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
772
|
+
return node;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
/** Wait Action: Waits for a duration */
|
|
776
|
+
class BTWait extends BTNode {
|
|
777
|
+
constructor(name = 'Wait', seconds = 1) {
|
|
778
|
+
super('wait', 'action', name);
|
|
779
|
+
this.elapsed = 0;
|
|
780
|
+
this.started = false;
|
|
781
|
+
this.seconds = seconds;
|
|
782
|
+
}
|
|
783
|
+
tick(context) {
|
|
784
|
+
if (!this.started) {
|
|
785
|
+
this.elapsed = 0;
|
|
786
|
+
this.started = true;
|
|
787
|
+
}
|
|
788
|
+
this.elapsed += context.dt;
|
|
789
|
+
if (this.elapsed >= this.seconds) {
|
|
790
|
+
this.started = false;
|
|
791
|
+
this.lastStatus = 'success';
|
|
792
|
+
return 'success';
|
|
793
|
+
}
|
|
794
|
+
this.lastStatus = 'running';
|
|
795
|
+
return 'running';
|
|
796
|
+
}
|
|
797
|
+
reset() {
|
|
798
|
+
super.reset();
|
|
799
|
+
this.elapsed = 0;
|
|
800
|
+
this.started = false;
|
|
801
|
+
}
|
|
802
|
+
clone() {
|
|
803
|
+
const node = new BTWait(this.name, this.seconds);
|
|
804
|
+
node.properties = { ...this.properties };
|
|
805
|
+
node.position = { ...this.position };
|
|
806
|
+
return node;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
/** Log Action: Logs a message */
|
|
810
|
+
class BTLog extends BTNode {
|
|
811
|
+
constructor(name = 'Log', message = '', level = 'info') {
|
|
812
|
+
super('log', 'action', name);
|
|
813
|
+
this.message = message;
|
|
814
|
+
this.level = level;
|
|
815
|
+
}
|
|
816
|
+
tick(context) {
|
|
817
|
+
console.log(`[BT ${this.level}] ${this.message}`);
|
|
818
|
+
this.lastStatus = 'success';
|
|
819
|
+
return 'success';
|
|
820
|
+
}
|
|
821
|
+
clone() {
|
|
822
|
+
const node = new BTLog(this.name, this.message, this.level);
|
|
823
|
+
node.properties = { ...this.properties };
|
|
824
|
+
node.position = { ...this.position };
|
|
825
|
+
return node;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
// ============================================================
|
|
829
|
+
// Subtree Node
|
|
830
|
+
// ============================================================
|
|
831
|
+
class BTSubtree extends BTNode {
|
|
832
|
+
constructor(name, subtreeId) {
|
|
833
|
+
super('subtree', 'subtree', name);
|
|
834
|
+
this.subtree = null;
|
|
835
|
+
this.subtreeId = subtreeId;
|
|
836
|
+
}
|
|
837
|
+
setSubtree(tree) {
|
|
838
|
+
this.subtree = tree;
|
|
839
|
+
}
|
|
840
|
+
tick(context) {
|
|
841
|
+
if (!this.subtree) {
|
|
842
|
+
this.lastStatus = 'failure';
|
|
843
|
+
return 'failure';
|
|
844
|
+
}
|
|
845
|
+
const status = this.subtree.tick(context);
|
|
846
|
+
this.lastStatus = status;
|
|
847
|
+
return status;
|
|
848
|
+
}
|
|
849
|
+
reset() {
|
|
850
|
+
var _a;
|
|
851
|
+
super.reset();
|
|
852
|
+
(_a = this.subtree) === null || _a === void 0 ? void 0 : _a.reset();
|
|
853
|
+
}
|
|
854
|
+
clone() {
|
|
855
|
+
var _a;
|
|
856
|
+
const node = new BTSubtree(this.name, this.subtreeId);
|
|
857
|
+
node.properties = { ...this.properties };
|
|
858
|
+
node.position = { ...this.position };
|
|
859
|
+
node.subtree = ((_a = this.subtree) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
860
|
+
return node;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
// ============================================================
|
|
864
|
+
// Root Node
|
|
865
|
+
// ============================================================
|
|
866
|
+
class BTRoot extends BTNode {
|
|
867
|
+
constructor(name = 'Root') {
|
|
868
|
+
super('root', 'root', name);
|
|
869
|
+
this.child = null;
|
|
870
|
+
}
|
|
871
|
+
setChild(child) {
|
|
872
|
+
this.child = child;
|
|
873
|
+
}
|
|
874
|
+
tick(context) {
|
|
875
|
+
if (!this.child) {
|
|
876
|
+
this.lastStatus = 'success';
|
|
877
|
+
return 'success';
|
|
878
|
+
}
|
|
879
|
+
const status = this.child.tick(context);
|
|
880
|
+
this.child.lastStatus = status;
|
|
881
|
+
this.lastStatus = status;
|
|
882
|
+
return status;
|
|
883
|
+
}
|
|
884
|
+
reset() {
|
|
885
|
+
var _a;
|
|
886
|
+
super.reset();
|
|
887
|
+
(_a = this.child) === null || _a === void 0 ? void 0 : _a.reset();
|
|
888
|
+
}
|
|
889
|
+
toJSON() {
|
|
890
|
+
return {
|
|
891
|
+
...super.toJSON(),
|
|
892
|
+
children: this.child ? [this.child.toJSON()] : [],
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
clone() {
|
|
896
|
+
var _a;
|
|
897
|
+
const node = new BTRoot(this.name);
|
|
898
|
+
node.properties = { ...this.properties };
|
|
899
|
+
node.position = { ...this.position };
|
|
900
|
+
node.child = ((_a = this.child) === null || _a === void 0 ? void 0 : _a.clone()) || null;
|
|
901
|
+
return node;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
// ============================================================
|
|
905
|
+
// Behavior Tree
|
|
906
|
+
// ============================================================
|
|
907
|
+
class BehaviorTree {
|
|
908
|
+
constructor(name = 'Behavior Tree') {
|
|
909
|
+
/** Execution statistics */
|
|
910
|
+
this.stats = {
|
|
911
|
+
tickCount: 0,
|
|
912
|
+
lastTickTime: 0,
|
|
913
|
+
averageTickTime: 0,
|
|
914
|
+
};
|
|
915
|
+
/** Debugging */
|
|
916
|
+
this.debugMode = false;
|
|
917
|
+
this.breakpoints = new Set();
|
|
918
|
+
this.isPaused = false;
|
|
919
|
+
this.id = `tree_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
920
|
+
this.name = name;
|
|
921
|
+
this.root = new BTRoot();
|
|
922
|
+
this.blackboard = new BTBlackboard();
|
|
923
|
+
}
|
|
924
|
+
/** Tick the tree */
|
|
925
|
+
tick(context) {
|
|
926
|
+
if (this.isPaused) {
|
|
927
|
+
return 'running';
|
|
928
|
+
}
|
|
929
|
+
// Merge blackboards
|
|
930
|
+
const mergedContext = {
|
|
931
|
+
...context,
|
|
932
|
+
blackboard: this.blackboard,
|
|
933
|
+
};
|
|
934
|
+
const startTime = performance.now();
|
|
935
|
+
const status = this.root.tick(mergedContext);
|
|
936
|
+
const endTime = performance.now();
|
|
937
|
+
// Update stats
|
|
938
|
+
this.stats.tickCount++;
|
|
939
|
+
this.stats.lastTickTime = endTime - startTime;
|
|
940
|
+
this.stats.averageTickTime =
|
|
941
|
+
(this.stats.averageTickTime * (this.stats.tickCount - 1) + this.stats.lastTickTime) /
|
|
942
|
+
this.stats.tickCount;
|
|
943
|
+
return status;
|
|
944
|
+
}
|
|
945
|
+
/** Reset the tree */
|
|
946
|
+
reset() {
|
|
947
|
+
this.root.reset();
|
|
948
|
+
}
|
|
949
|
+
/** Pause execution */
|
|
950
|
+
pause() {
|
|
951
|
+
this.isPaused = true;
|
|
952
|
+
}
|
|
953
|
+
/** Resume execution */
|
|
954
|
+
resume() {
|
|
955
|
+
this.isPaused = false;
|
|
956
|
+
}
|
|
957
|
+
/** Add breakpoint */
|
|
958
|
+
addBreakpoint(nodeId) {
|
|
959
|
+
this.breakpoints.add(nodeId);
|
|
960
|
+
}
|
|
961
|
+
/** Remove breakpoint */
|
|
962
|
+
removeBreakpoint(nodeId) {
|
|
963
|
+
this.breakpoints.delete(nodeId);
|
|
964
|
+
}
|
|
965
|
+
/** Toggle breakpoint */
|
|
966
|
+
toggleBreakpoint(nodeId) {
|
|
967
|
+
if (this.breakpoints.has(nodeId)) {
|
|
968
|
+
this.breakpoints.delete(nodeId);
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
this.breakpoints.add(nodeId);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
/** Find node by ID */
|
|
975
|
+
findNode(nodeId) {
|
|
976
|
+
return this.findNodeRecursive(this.root, nodeId);
|
|
977
|
+
}
|
|
978
|
+
findNodeRecursive(node, nodeId) {
|
|
979
|
+
if (node.id === nodeId)
|
|
980
|
+
return node;
|
|
981
|
+
if (node instanceof BTComposite) {
|
|
982
|
+
for (const child of node.children) {
|
|
983
|
+
const found = this.findNodeRecursive(child, nodeId);
|
|
984
|
+
if (found)
|
|
985
|
+
return found;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
else if (node instanceof BTDecorator && node.child) {
|
|
989
|
+
return this.findNodeRecursive(node.child, nodeId);
|
|
990
|
+
}
|
|
991
|
+
else if (node instanceof BTRoot && node.child) {
|
|
992
|
+
return this.findNodeRecursive(node.child, nodeId);
|
|
993
|
+
}
|
|
994
|
+
return null;
|
|
995
|
+
}
|
|
996
|
+
/** Get all nodes */
|
|
997
|
+
getAllNodes() {
|
|
998
|
+
const nodes = [];
|
|
999
|
+
this.collectNodes(this.root, nodes);
|
|
1000
|
+
return nodes;
|
|
1001
|
+
}
|
|
1002
|
+
collectNodes(node, nodes) {
|
|
1003
|
+
nodes.push(node);
|
|
1004
|
+
if (node instanceof BTComposite) {
|
|
1005
|
+
for (const child of node.children) {
|
|
1006
|
+
this.collectNodes(child, nodes);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
else if (node instanceof BTDecorator && node.child) {
|
|
1010
|
+
this.collectNodes(node.child, nodes);
|
|
1011
|
+
}
|
|
1012
|
+
else if (node instanceof BTRoot && node.child) {
|
|
1013
|
+
this.collectNodes(node.child, nodes);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
/** Clone the tree */
|
|
1017
|
+
clone() {
|
|
1018
|
+
const tree = new BehaviorTree(this.name);
|
|
1019
|
+
tree.root = this.root.clone();
|
|
1020
|
+
tree.blackboard = this.blackboard.clone();
|
|
1021
|
+
return tree;
|
|
1022
|
+
}
|
|
1023
|
+
/** Serialize to JSON */
|
|
1024
|
+
toJSON() {
|
|
1025
|
+
return {
|
|
1026
|
+
id: this.id,
|
|
1027
|
+
name: this.name,
|
|
1028
|
+
root: this.root.toJSON(),
|
|
1029
|
+
blackboard: this.blackboard.toJSON(),
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
// ============================================================
|
|
1034
|
+
// Node Registry
|
|
1035
|
+
// ============================================================
|
|
1036
|
+
class BTNodeRegistry {
|
|
1037
|
+
constructor() {
|
|
1038
|
+
this.definitions = new Map();
|
|
1039
|
+
}
|
|
1040
|
+
register(definition) {
|
|
1041
|
+
this.definitions.set(definition.type, definition);
|
|
1042
|
+
}
|
|
1043
|
+
get(type) {
|
|
1044
|
+
return this.definitions.get(type);
|
|
1045
|
+
}
|
|
1046
|
+
getAll() {
|
|
1047
|
+
return Array.from(this.definitions.values());
|
|
1048
|
+
}
|
|
1049
|
+
getByCategory(category) {
|
|
1050
|
+
return this.getAll().filter((d) => d.category === category);
|
|
1051
|
+
}
|
|
1052
|
+
createNode(type, properties) {
|
|
1053
|
+
const def = this.definitions.get(type);
|
|
1054
|
+
if (!def)
|
|
1055
|
+
return null;
|
|
1056
|
+
return def.create(properties);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
// ============================================================
|
|
1060
|
+
// Factory Functions
|
|
1061
|
+
// ============================================================
|
|
1062
|
+
function createBehaviorTree(name) {
|
|
1063
|
+
return new BehaviorTree(name);
|
|
1064
|
+
}
|
|
1065
|
+
function createBlackboard() {
|
|
1066
|
+
return new BTBlackboard();
|
|
1067
|
+
}
|
|
1068
|
+
function createNodeRegistry() {
|
|
1069
|
+
const registry = new BTNodeRegistry();
|
|
1070
|
+
// Register built-in nodes
|
|
1071
|
+
registry.register({
|
|
1072
|
+
type: 'sequence',
|
|
1073
|
+
category: 'composite',
|
|
1074
|
+
name: 'Sequence',
|
|
1075
|
+
description: 'Executes children in order until one fails',
|
|
1076
|
+
icon: 'arrow-right',
|
|
1077
|
+
color: '#3b82f6',
|
|
1078
|
+
create: () => new BTSequence(),
|
|
1079
|
+
});
|
|
1080
|
+
registry.register({
|
|
1081
|
+
type: 'selector',
|
|
1082
|
+
category: 'composite',
|
|
1083
|
+
name: 'Selector',
|
|
1084
|
+
description: 'Executes children until one succeeds',
|
|
1085
|
+
icon: 'git-branch',
|
|
1086
|
+
color: '#22c55e',
|
|
1087
|
+
create: () => new BTSelector(),
|
|
1088
|
+
});
|
|
1089
|
+
registry.register({
|
|
1090
|
+
type: 'parallel',
|
|
1091
|
+
category: 'composite',
|
|
1092
|
+
name: 'Parallel',
|
|
1093
|
+
description: 'Executes all children simultaneously',
|
|
1094
|
+
icon: 'layers',
|
|
1095
|
+
color: '#8b5cf6',
|
|
1096
|
+
properties: [
|
|
1097
|
+
{ name: 'successPolicy', type: 'enum', enumValues: ['all', 'one'], default: 'all' },
|
|
1098
|
+
{ name: 'failurePolicy', type: 'enum', enumValues: ['all', 'one'], default: 'one' },
|
|
1099
|
+
],
|
|
1100
|
+
create: (props) => {
|
|
1101
|
+
const node = new BTParallel();
|
|
1102
|
+
if (props === null || props === void 0 ? void 0 : props.successPolicy)
|
|
1103
|
+
node.successPolicy = props.successPolicy;
|
|
1104
|
+
if (props === null || props === void 0 ? void 0 : props.failurePolicy)
|
|
1105
|
+
node.failurePolicy = props.failurePolicy;
|
|
1106
|
+
return node;
|
|
1107
|
+
},
|
|
1108
|
+
});
|
|
1109
|
+
registry.register({
|
|
1110
|
+
type: 'inverter',
|
|
1111
|
+
category: 'decorator',
|
|
1112
|
+
name: 'Inverter',
|
|
1113
|
+
description: 'Inverts the result of its child',
|
|
1114
|
+
icon: 'refresh-cw',
|
|
1115
|
+
color: '#f97316',
|
|
1116
|
+
create: () => new BTInverter(),
|
|
1117
|
+
});
|
|
1118
|
+
registry.register({
|
|
1119
|
+
type: 'repeater',
|
|
1120
|
+
category: 'decorator',
|
|
1121
|
+
name: 'Repeater',
|
|
1122
|
+
description: 'Repeats child N times (-1 = infinite)',
|
|
1123
|
+
icon: 'repeat',
|
|
1124
|
+
color: '#f97316',
|
|
1125
|
+
properties: [{ name: 'times', type: 'number', default: -1, min: -1 }],
|
|
1126
|
+
create: (props) => { var _a; return new BTRepeater('Repeater', (_a = props === null || props === void 0 ? void 0 : props.times) !== null && _a !== void 0 ? _a : -1); },
|
|
1127
|
+
});
|
|
1128
|
+
registry.register({
|
|
1129
|
+
type: 'timeout',
|
|
1130
|
+
category: 'decorator',
|
|
1131
|
+
name: 'Timeout',
|
|
1132
|
+
description: 'Fails if child takes too long',
|
|
1133
|
+
icon: 'clock',
|
|
1134
|
+
color: '#f97316',
|
|
1135
|
+
properties: [{ name: 'seconds', type: 'number', default: 5, min: 0 }],
|
|
1136
|
+
create: (props) => { var _a; return new BTTimeout('Timeout', (_a = props === null || props === void 0 ? void 0 : props.seconds) !== null && _a !== void 0 ? _a : 5); },
|
|
1137
|
+
});
|
|
1138
|
+
registry.register({
|
|
1139
|
+
type: 'wait',
|
|
1140
|
+
category: 'action',
|
|
1141
|
+
name: 'Wait',
|
|
1142
|
+
description: 'Waits for a duration',
|
|
1143
|
+
icon: 'pause',
|
|
1144
|
+
color: '#6b7280',
|
|
1145
|
+
properties: [{ name: 'seconds', type: 'number', default: 1, min: 0 }],
|
|
1146
|
+
create: (props) => { var _a; return new BTWait('Wait', (_a = props === null || props === void 0 ? void 0 : props.seconds) !== null && _a !== void 0 ? _a : 1); },
|
|
1147
|
+
});
|
|
1148
|
+
registry.register({
|
|
1149
|
+
type: 'log',
|
|
1150
|
+
category: 'action',
|
|
1151
|
+
name: 'Log',
|
|
1152
|
+
description: 'Logs a message',
|
|
1153
|
+
icon: 'message-square',
|
|
1154
|
+
color: '#6b7280',
|
|
1155
|
+
properties: [
|
|
1156
|
+
{ name: 'message', type: 'string', default: '' },
|
|
1157
|
+
{ name: 'level', type: 'enum', enumValues: ['info', 'warn', 'error'], default: 'info' },
|
|
1158
|
+
],
|
|
1159
|
+
create: (props) => {
|
|
1160
|
+
var _a, _b;
|
|
1161
|
+
return new BTLog('Log', (_a = props === null || props === void 0 ? void 0 : props.message) !== null && _a !== void 0 ? _a : '', (_b = props === null || props === void 0 ? void 0 : props.level) !== null && _b !== void 0 ? _b : 'info');
|
|
1162
|
+
},
|
|
1163
|
+
});
|
|
1164
|
+
return registry;
|
|
1165
|
+
}
|
|
1166
|
+
/** Create execution context */
|
|
1167
|
+
function createBTContext(params) {
|
|
1168
|
+
let isAborted = false;
|
|
1169
|
+
return {
|
|
1170
|
+
entityId: params.entityId,
|
|
1171
|
+
blackboard: params.blackboard || new BTBlackboard(),
|
|
1172
|
+
dt: params.dt,
|
|
1173
|
+
time: params.time,
|
|
1174
|
+
world: params.world,
|
|
1175
|
+
emit: () => { },
|
|
1176
|
+
abort: () => {
|
|
1177
|
+
isAborted = true;
|
|
1178
|
+
},
|
|
1179
|
+
get isAborted() {
|
|
1180
|
+
return isAborted;
|
|
1181
|
+
},
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
export { BTBlackboard, BTComposite, BTConditionGuard, BTCooldown, BTDecorator, BTFailer, BTInverter, BTLog, BTNode, BTNodeRegistry, BTParallel, BTRandomSelector, BTRandomSequence, BTRepeatUntilFail, BTRepeatUntilSuccess, BTRepeater, BTRoot, BTSelector, BTSequence, BTSubtree, BTSucceeder, BTTimeout, BTWait, BehaviorTree, createBTContext, createBehaviorTree, createBlackboard, createNodeRegistry };
|
|
1186
|
+
//# sourceMappingURL=BehaviorTree.js.map
|