Skip to content

Commit f9f5641

Browse files
authored
Replace stop status with stop condition (#12)
1 parent eb9b83d commit f9f5641

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/main/kotlin/com/tpcly/behaviourtree/Builder.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fun TreeNode.inverted() = Inverter(name, this)
1212

1313
fun succeeder(name: String = "", init: () -> TreeNode): Succeeder = Succeeder(name, init())
1414

15+
fun repeatUntil(stopCondition: (TreeNodeResult) -> Boolean, name: String = "", init: () -> TreeNode) = RepeatUntil(name, stopCondition, init())
16+
1517
fun repeatUntil(status: Status, name: String = "", init: () -> TreeNode) = RepeatUntil(name, status, init())
1618

1719
fun gate(validate: (blackboard: Blackboard) -> Boolean, name: String = "", init: () -> TreeNode) = Gate(name, validate, init())

src/main/kotlin/com/tpcly/behaviourtree/node/RepeatUntil.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@ import com.tpcly.behaviourtree.Status
55
import com.tpcly.behaviourtree.TreeNodeResult
66

77
/**
8-
* A decorator node that repeatedly executes its child until the specified [status] is returned
8+
* A decorator node that repeatedly executes its child until the specified [stopCondition] is met
99
*
10-
* @property status the status to loop until
10+
* @property stopCondition the condition which determines when the loop terminates
1111
*/
1212
class RepeatUntil(
1313
name: String,
14-
private val status: Status,
14+
private val stopCondition: (TreeNodeResult) -> Boolean,
1515
child: TreeNode
1616
) : Decorator(name, child) {
17+
constructor(name: String, status: Status, child: TreeNode)
18+
: this(name, { it.status == status }, child)
19+
1720
override fun execute(blackboard: Blackboard): TreeNodeResult {
1821
val results = mutableListOf<TreeNodeResult>()
1922

2023
var result: TreeNodeResult
2124
do {
2225
result = child.execute(blackboard)
2326
results.add(result)
24-
} while (result.status != status && result.status != Status.ABORT)
27+
} while (!stopCondition(result) && result.status != Status.ABORT)
2528

2629
return TreeNodeResult(this, result.status, results)
2730
}

0 commit comments

Comments
 (0)