Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created July 26, 2011 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/1106593 to your computer and use it in GitHub Desktop.
Save AngryAnt/1106593 to your computer and use it in GitHub Desktop.
Examples of Behave repeat decorator implementations (untested).
private Dictionary<string, int> activeRepeats = new Dictionary<string, float> ();
private Dictionary<string, float> activeTimers = new Dictionary<string, float> ();
// The repeat decorator works more or less like a for loop //
public BehaveResult InitRepeatDecorator (Tree sender, string name, float repeats, IAgent agent, object data)
{
activeRepeats[name] = 0;
return BehaveResult.Success;
}
public BehaveResult TickRepeatDecorator (Tree sender, string name, float repeats, IAgent agent, object data)
{
return activeRepeats[name] += 1 > (int)repeats ? BehaveResult.Failure : BehaveResult.Running;
}
public void ResetRepeatDecorator(Tree sender, string name, float repeats, IAgent agent, object data)
{
activeRepeats.Remove (name);
}
// The timed repeat decorator works like the repeat decorator, but on a timeout in stead of an iteration count //
public BehaveResult InitTimedRepeatDecorator (Tree sender, string name, float timeout, IAgent agent, object data)
{
activeTimers[name] = Time.time;
return BehaveResult.Success;
}
public BehaveResult TickTimedRepeatDecorator (Tree sender, string name, float timeout, IAgent agent, object data)
{
return Time.time - activeTimers[name] > timeout ? BehaveResult.Failure : BehaveResult.Running;
}
public void ResetTimedRepeatDecorator(Tree sender, string name, float timeout, IAgent agent, object data)
{
activeTimers.Remove (name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment