-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikolas Howard
committed
Mar 11, 2024
1 parent
e504634
commit 3b21c3b
Showing
7 changed files
with
127 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Composite from "./Composite"; | ||
import Node from "../Node"; | ||
import { Agent } from "../../Agent"; | ||
import Attribute from "../../attributes/Attribute"; | ||
import { BehaviourTreeOptions } from "../../BehaviourTreeOptions"; | ||
/** | ||
* A RACE node. | ||
* The child nodes are executed concurrently until one succeeds or all fail. | ||
*/ | ||
export default class Race extends Composite { | ||
/** | ||
* @param attributes The node attributes. | ||
* @param children The child nodes. | ||
*/ | ||
constructor(attributes: Attribute[], children: Node[]); | ||
/** | ||
* Called when the node is being updated. | ||
* @param agent The agent. | ||
* @param options The behaviour tree options object. | ||
*/ | ||
protected onUpdate(agent: Agent, options: BehaviourTreeOptions): void; | ||
/** | ||
* Gets the name of the node. | ||
*/ | ||
getName: () => string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Composite from "./Composite"; | ||
import State from "../../State"; | ||
import Node from "../Node"; | ||
import { Agent } from "../../Agent"; | ||
import Attribute from "../../attributes/Attribute"; | ||
import { BehaviourTreeOptions } from "../../BehaviourTreeOptions"; | ||
|
||
/** | ||
* A RACE node. | ||
* The child nodes are executed concurrently until one succeeds or all fail. | ||
*/ | ||
export default class Race extends Composite { | ||
/** | ||
* @param attributes The node attributes. | ||
* @param children The child nodes. | ||
*/ | ||
constructor(attributes: Attribute[], children: Node[]) { | ||
super("race", attributes, children); | ||
} | ||
|
||
/** | ||
* Called when the node is being updated. | ||
* @param agent The agent. | ||
* @param options The behaviour tree options object. | ||
*/ | ||
protected onUpdate(agent: Agent, options: BehaviourTreeOptions): void { | ||
// Iterate over all of the children of this node, updating any that aren't in a settled state. | ||
for (const child of this.children) { | ||
// If the child has never been updated or is running then we will need to update it now. | ||
if (child.getState() === State.READY || child.getState() === State.RUNNING) { | ||
// Update the child of this node. | ||
child.update(agent, options); | ||
} | ||
} | ||
|
||
// If any of our child nodes have succeeded then this node has also succeeded | ||
if (this.children.find((child) => child.is(State.SUCCEEDED))) { | ||
// This node is a 'SUCCEEDED' node. | ||
this.setState(State.SUCCEEDED); | ||
|
||
// Abort every running child. | ||
for (const child of this.children) { | ||
if (child.getState() === State.RUNNING) { | ||
child.abort(agent); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
// A race node will move into the failed state if all child nodes move into the failed state as none can succeed. | ||
if (this.children.every((child) => child.is(State.FAILED))) { | ||
// This node is a 'FAILED' node. | ||
this.setState(State.FAILED); | ||
|
||
return; | ||
} | ||
|
||
// If we didn't move to a succeeded or failed state then this node is still running. | ||
this.setState(State.RUNNING); | ||
} | ||
|
||
/** | ||
* Gets the name of the node. | ||
*/ | ||
getName = () => "RACE"; | ||
} |