Skip to content

Instantly share code, notes, and snippets.

@cubed2d
Created February 13, 2012 20:02
Show Gist options
  • Save cubed2d/1819731 to your computer and use it in GitHub Desktop.
Save cubed2d/1819731 to your computer and use it in GitHub Desktop.
Example of my new Fluent-ish behavior tree definition API
public class PatrolBehavior : LoopNode
{
private int minimumPatrolTime = 4;
private int patrolTimeModifier = 8;
public PatrolBehavior()
{
// The Patrol Behavior has two major parts, a timer/skillcheck to decide how long it runs for and movement/flip branch.
Parallel
(
new LoopNode().Sequence // lets define the first branch. this controls the amount of time we spend patroling
(
new SetStateDebugName("Patrol"), // First, lets set the debug name to patrol so we can see what state the ai is in. we could do it in either branch, dosnt matter.
new TimerNode(() => TimeSpan.FromSeconds(minimumPatrolTime + Helpers.Instance.GetRandomInt(patrolTimeModifier))), // then we wait...
new ConditionNode(LazinessCheck) // Ok, times up! Laziness test. if this fails, the whole behavior will bail out.
).Root(),
new LoopNode().Selector// here we have to decided if the Guard walks forward or flips round. we do this with a selector. it tries to run all its child nodes untill one succeeds. If a node fails, it runs the next one
(
new SequenceNode // this sequence decided if we need to flip.
(
new SelectorNode // this selector runs some conditions. if both pass we flip, if any fail, we bail out and walk forward instead.
(
new ConditionNode(() => !this.Owner.Blackboard.GetRecord<bool>("HasDetectedStableGround") && !this.Owner.bCanFallOffPlatform),
new ConditionNode(() => this.Owner.Blackboard.GetRecord<bool>("IsBlockedByWall"))
),
new ActionNode(FlipCharacter)
),
new WalkForwardBehavior()// ok, cool. we dont need to flip. lets walk forward!
).Root()
);
}
#region helpers
/// <summary>
/// Does a laziness stat check.
/// </summary>
private bool LazinessCheck()
{
return !(Helpers.Instance.GetRandomInt(100) > Owner._behaviorTreeRunner._guardStats.iLaziness);
}
/// <summary>
/// Flips the character.
/// </summary>
/// <param name="node"></param>
private void FlipCharacter()
{
if (Owner.direction == Direction.Left)
{
Owner.SetDirection(Direction.Right);
}
else
{
Owner.SetDirection(Direction.Left);
}
// move the character in the new direction, this helps to make sure we dont fall off platforms
this.Owner.Controler.moveStick = new VirtualStick((int) this.Owner.direction, 0);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment