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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace Brains.Framework.Behaviors
{
public class CompositeBehavior : IBehavior,ISubBehaviorHolder
{
private Agent _agent;
private BehaviorList<IBehavior> _subBehaviors;
private int _currentSubBehavior;
/// <summary>
/// Default Constructor
/// </summary>
public CompositeBehavior()
{
_subBehaviors = new BehaviorList<IBehavior>();
_currentSubBehavior = 0;
}
/// <summary>
/// Gets the count of subbehaviors
/// </summary>
public int Count
{
get { return SubBehaviors.Count; }
}
/// <summary>
/// Gets the Agent owner of the behavior
/// </summary>
public Agent Owner
{
get { return _agent; }
}
/// <summary>
/// Gets or sets the subbehaviors list
/// </summary>
public BehaviorList<IBehavior> SubBehaviors
{
get
{
return _subBehaviors;
}
set
{
_subBehaviors = value;
}
}
/// <summary>
/// Gets or sets the index of the currently active sub behavior
/// </summary>
public int CurrentSubBehavior
{
get
{
return _currentSubBehavior;
}
set
{
if (_currentSubBehavior != value)
{
_currentSubBehavior = value;
}
}
}
/// <summary>
/// Gets or sets the current state of the behavior.
/// </summary>
/// <remarks>If the state is Idle and the curent index changes to this behavior, State will automatically get set to Running</remarks>
public BehaviorState State { get; set; }
/// <summary>
/// Sets the Agent owner of the Behavior
/// </summary>
/// <param name="actor">The agent</param>
/// <remarks>This method sets the agent of all subbehaviors if exist</remarks>
public virtual void SetOwner(Agent actor)
{
_agent = actor;
_subBehaviors.Owner = this;
}
/// <summary>
/// Updates the currently active sub behavior of this behavior.
/// </summary>
/// <param name="gameTime"></param>
/// <remarks>If the sub behavior is Idle, it get sets to Running.
/// If the sub behavior is Success or Failed it calls the overridable methods OnSubBehaviorSuccess and OnSubBehaviorFailure.</remarks>
public virtual void Update(GameTime gameTime)
{
if (_subBehaviors != null && Count > 0)
{
UpdateCurrentSubBehavior(gameTime);
}
}
private void UpdateCurrentSubBehavior(GameTime gameTime)
{
IBehavior _behavior = _subBehaviors[_currentSubBehavior];
if (_behavior.State == BehaviorState.Idle || _behavior.State == BehaviorState.Running)
{
_behavior.State = BehaviorState.Running;
_behavior.Update(gameTime);
}
else
{
//Check state of sub behavior after running it
if (_behavior.State == BehaviorState.Success)
OnSubBehaviorSuccess();
else if (_behavior.State == BehaviorState.Failed)
OnSubBehaviorFailure();
}
}
public virtual void OnSuccess()
{
}
public virtual void OnFailure()
{
}
public virtual void OnSubBehaviorSuccess()
{
}
public virtual void OnSubBehaviorFailure()
{
}
public virtual void Reset()
{
State = BehaviorState.Idle;
CurrentSubBehavior = 0;
foreach (var item in SubBehaviors)
{
item.Reset();
}
}
/// <summary>
/// Gets the currently active sub behavior as an IBehavior
/// </summary>
public IBehavior CurrentBehavior
{
get { return SubBehaviors[CurrentSubBehavior]; }
}
}
} |