feat(bt): Add wait and whilst, rename leaf to action

This commit is contained in:
hyper
2026-06-28 15:07:34 +08:00
parent 204b9100e6
commit 9bf0e5e049
8 changed files with 571 additions and 319 deletions
+13 -12
View File
@@ -3,11 +3,11 @@
// Architecture:
// Behaviour Tree (buildTree) — controls game flow:
// parallel
// ├── gravityTick (leaf) — generator loop, auto-drop piece on timer
// ├── gravityTick (action) — generator loop, auto-drop piece on timer
// └── cycle
// └── seq (sequential)
// ├── handleInput (leaf) — reads queued commands
// └── render (leaf) — draws via blessed
// ├── handleInput (action) — reads queued commands
// └── render (action) — draws via blessed
//
// CommandQueue — processes input:
// Keyboard → spawn command entities → CommandQueue.execute()
@@ -22,10 +22,11 @@
import { World } from "../../src/index";
import {
buildTree,
leaf,
action,
parallel,
cycle,
sequential,
whilst,
} from "../../src/bt/index";
import { CommandQueue } from "../../src/commands/index";
@@ -73,11 +74,11 @@ pieces.spawnPiece();
const runner = buildTree(
world,
parallel([
leaf(function* gravityTick() {
while (true) {
const dt: number = yield;
whilst(
() => true,
action((_world, _entity, dt) => {
if (world.hasSingleton(GameOver) || world.hasSingleton(Paused)) {
continue;
return;
}
const timer = world.getSingleton(TickTimer);
timer.accumulator += dt;
@@ -93,14 +94,14 @@ const runner = buildTree(
}
}
}
}
}),
}),
),
cycle(
sequential([
leaf(() => {
action(() => {
commands.execute();
}),
leaf(() => {
action(() => {
render(world, ui);
}),
]),