Skip to content

Commit 3050efe

Browse files
committed
Update and rename BTree SelectorNode and SequenceNode
1 parent d75eb2c commit 3050efe

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

korge-fleks/src/commonMain/kotlin/korlibs/korge/fleks/entity/behavior/BehaviorTreeNodes.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,43 @@ interface BTNode {
1515
/**
1616
* A simple leaf node that always returns success. This can be used as a placeholder or a default node in the behavior tree.
1717
*/
18-
class EmptyTree : BTNode {
18+
class EmptyTreeNode : BTNode {
1919
override fun World.tick(entity: Entity, deltaTime: Float): BTStatus = BTStatus.Success
2020
}
2121

2222
/**
2323
* First Composite Node: A selector node (symbol: [?]) that ticks its children in order and returns success on the
2424
* first child that succeeds, failure if all children failed, and running if any child is still running.
25+
*
26+
* Optionally it can take a "message" function to be invoked when all children fail. This can be used to log
27+
* or perform some action when the selector fails, which can be helpful for debugging or for triggering certain
28+
* behaviors in the game when a certain branch of the behavior tree fails.
2529
*/
26-
class Selector(private val children: List<BTNode>) : BTNode {
30+
class SelectorNode(
31+
private val children: List<BTNode>,
32+
private val failureCall: () -> Unit = {}
33+
) : BTNode {
34+
2735
override fun World.tick(entity: Entity, deltaTime: Float): BTStatus {
2836
children.forEach { child ->
2937
when (child.run { tick(entity, deltaTime) }) {
3038
BTStatus.Success -> return BTStatus.Success
3139
BTStatus.Running -> return BTStatus.Running
32-
BTStatus.Failure -> {}
40+
BTStatus.Failure -> {}
3341
}
3442
}
43+
failureCall.invoke()
44+
3545
return BTStatus.Failure
3646
}
3747
}
3848

49+
3950
/**
4051
* Second Composite Node: A sequence node (symbol: [->]) that ticks its children in order and returns failure on the
4152
* first child that fails, success if all children succeed, and running if any child is still running.
4253
*/
43-
class Sequence(private val children: List<BTNode>) : BTNode {
54+
class SequenceNode(private val children: List<BTNode>) : BTNode {
4455
override fun World.tick(entity: Entity, deltaTime: Float): BTStatus {
4556
children.forEach { child ->
4657
when (child.run { tick(entity, deltaTime) }) {

korge-fleks/src/commonMain/kotlin/korlibs/korge/fleks/entity/behavior/BehaviorTreeStorage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object BehaviorTreeStorage {
1616
// Internal storage for behavior tree blueprint objects
1717
private val behaviorTreeBlueprints: MutableMap<String, BehaviorTreeBlueprint> = mutableMapOf()
1818

19-
private val emptyTree = EmptyTree()
19+
private val emptyTreeNode = EmptyTreeNode()
2020

2121
/**
2222
* Adds a [BehaviorTreeBlueprint] to the internal storage.
@@ -52,7 +52,7 @@ object BehaviorTreeStorage {
5252
blueprint.btree
5353
} else {
5454
println("ERROR: BehaviorTreeBlueprint with name '$name' not registered in BehaviorTreeStorage!")
55-
emptyTree
55+
emptyTreeNode
5656
}
5757
}
5858
}

0 commit comments

Comments
 (0)