Skip to content

Instantly share code, notes, and snippets.

@ElliotWood
Created March 28, 2013 02:25
Show Gist options
  • Save ElliotWood/5260029 to your computer and use it in GitHub Desktop.
Save ElliotWood/5260029 to your computer and use it in GitHub Desktop.
AI Game Behavior Tree's
public static class Behavior
{
//Branch
public static Action Selector(Func<bool> cond, Action ifTrue, Action ifFalse) {
return () => { if (cond()) { ifTrue(); } else { ifFalse(); } };
}
public static Action Sequencer(Action a, Action b) {
return () => { a(); b(); }
}
//Example trees
public static Func<bool> ifPlayerIsInSight = () => { return true; /*...true iff WorldState shows guard can see player...*/};
public static Action shootAtPlayer = () => { /*...aim guard's weapon at player and fire...*/ };
public static Func<bool> ifUnderFire = () => { return true; /*...true iff WorldState shows guard hears player gunfire...*/};
public static Action takeCover = () => { /*...guard runs for nearest shelter... */};
public static Action walkBackAndForthGuardingDoorway = () => { /*...default guard patrol behaviour...*/ };
public static Action patrollingGuardBehaviour =
Selector(Behavior.ifPlayerIsInSight, Behavior.shootAtPlayer,
Selector(Behavior.ifUnderFire, Behavior.takeCover,
Behavior.walkBackAndForthGuardingDoorway));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment