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
66
67 |
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 sequence behavior runs a sub behavior until it is complete, then moves onto the next sub behavior.
/// </summary>
[BehaviorAttribute("Sequence")]
public class SequenceBehavior : CompositeBehavior, ICompositeBehavior
{
public SequenceBehavior()
{
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void OnSuccess()
{
Reset();
}
public override void OnFailure()
{
Reset();
}
/// <summary>
/// If the child behavior has failed cleanly we mark the behavior as a failure too.
/// </summary>
public override void OnSubBehaviorFailure()
{
//If we fail one, we fail them all
State = BehaviorState.Failed;
}
/// <summary>
/// If a child behavior succeeds, we continue execution to the next behavior
/// </summary>
public override void OnSubBehaviorSuccess()
{
//Reset the current sub behavior to idle
SubBehaviors[CurrentSubBehavior].Reset();
//If it's the last behavior the sequence is a success
if (CurrentSubBehavior == Count - 1)
{
State = BehaviorState.Success;
}
else //Move onto the next item in the sequence.
{
CurrentSubBehavior++;
}
}
}
} |