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
// ├── dealerPlay (leaf) — generator loop, auto-plays dealer hand
// ├── dealerPlay (action) — generator loop, auto-plays dealer hand
// └── 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()
@@ -25,10 +25,11 @@
import { World } from "../../src/index";
import {
buildTree,
leaf,
action,
parallel,
cycle,
sequential,
whilst,
} from "../../src/bt/index";
import { CommandQueue } from "../../src/commands/index";
@@ -72,11 +73,11 @@ registerCommands(world, commands, cards);
const runner = buildTree(
world,
parallel([
leaf(function* dealerPlay() {
while (true) {
yield;
whilst(
() => true,
action(() => {
const phase = world.getSingleton(GamePhase);
if (phase.phase !== "dealerTurn") continue;
if (phase.phase !== "dealerTurn") return;
if (dealerShouldHit(cards.getHand(InDealerHand))) {
const cardEntity = cards.drawCard();
@@ -86,14 +87,14 @@ const runner = buildTree(
} else {
resolveRound(world, cards);
}
}
}),
}),
),
cycle(
sequential([
leaf(() => {
action(() => {
commands.execute();
}),
leaf(() => {
action(() => {
render(world, ui);
}),
]),