@@ -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) }) {
0 commit comments