Last active
August 29, 2015 14:07
-
-
Save thecodejunkie/d9fe7156e746d256c3b6 to your computer and use it in GitHub Desktop.
Spike of a Random composite node for a Behavior Tree
This file contains hidden or 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
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