Skip to content

Instantly share code, notes, and snippets.

@thecodejunkie
Last active August 29, 2015 14:07
Show Gist options
  • Save thecodejunkie/d9fe7156e746d256c3b6 to your computer and use it in GitHub Desktop.
Save thecodejunkie/d9fe7156e746d256c3b6 to your computer and use it in GitHub Desktop.
Spike of a Random composite node for a Behavior Tree
public class Random : BehaviorComposite
{
private readonly System.Random random;
private Behavior current;
public Random(params Behavior[] behaviors)
: this(null, behaviors)
{
}
public Random(string name, params Behavior[] behaviors)
: base(name, behaviors)
{
this.random =
new System.Random(Guid.NewGuid().GetHashCode());
}
public override BehaviorResult OnBehave(BehaviorContext context)
{
var behavior =
this.GetBehavior();
var result =
behavior.Behave(context);
this.current = (result == BehaviorResult.Running)
? behavior
: null;
return result;
}
private Behavior GetBehavior()
{
if (this.current != null)
{
return current;
}
var index =
this.random.Next(this.Behaviors.Count());
return this.Behaviors[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment