1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brains.Framework.Designer;
namespace Brains.Framework.Behaviors
{
/// <summary>
/// The Selector Behavior is a list of behaviors that are tried one after the other until one succeeds.
/// </summary>
/// <remarks>Only fails on a critical error, otherwise continues to loop until one succeeds</remarks>
[BehaviorAttribute("Selector")]
public class SelectorBehavior : CompositeBehavior, ICompositeBehavior
{
public SelectorBehavior()
{
}
/// <summary>
/// Runs the currently active Sub Behavior
/// </summary>
/// <param name="gameTime"></param>
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void OnSuccess()
{
Reset();
}
public override void OnFailure()
{
Reset();
}
public override void OnSubBehaviorFailure()
{
SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
CurrentSubBehavior++;
if (CurrentSubBehavior == SubBehaviors.Count)
{
//Once a selector has reached the end, loop back to start
CurrentSubBehavior = 0;
State = BehaviorState.Failed;
}
}
public override void OnSubBehaviorSuccess()
{
//Reset the state of the sub behavior
SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
CurrentSubBehavior = 0;
State = BehaviorState.Success;
return;
}
}
} |