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 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brains.Framework.Map;
namespace Brains.Framework.Behaviors.PathFinding
{
/// <summary>
/// This behaviour allows to define a list of point which should be visited in
/// a sequential fashion. When last point is reached, the whole behaviour starts
/// over, going to point #1 and repeating sequence.
///
/// How it works:
/// A list of AIBehaviourGoto instances is being build and added to local
/// subbehaviours container.
/// </summary>
public class CyclicPathBehavior : CompositeBehavior,IActionBehavior
{
protected List<GridCell> nodesToVisit;
protected bool initialized;
protected int sequenceCounter;
public List<GridCell> CellsToVisit { get { return nodesToVisit; } }
public CyclicPathBehavior()
{
initialized = false;
nodesToVisit = new List<GridCell>();
}
~CyclicPathBehavior()
{
}
public void AddPoint(GridCell intermediateNode)
{
nodesToVisit.Add(intermediateNode);
}
public override void Reset()
{
sequenceCounter = 0;
this.State = BehaviorState.Idle;
base.Reset();
}
public override void OnSuccess()
{
base.OnSuccess();
sequenceCounter++;
if (sequenceCounter > nodesToVisit.Count - 1)
{
this.Reset();
}
else
{
// stop actor for a moment, until we get new desired direction
this.Owner.DesiredOrientation = this.Owner.DesiredOrientation;
}
}
public override void OnSubBehaviorSuccess()
{
SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
CurrentSubBehavior++;
if (CurrentSubBehavior >= nodesToVisit.Count)
{
this.Reset();
}
}
public override void Update(GameTime gameTime)
{
if (!initialized)
{
SubBehaviors = new BehaviorList<IBehavior>();
if (Owner != null)
{
// 1 add goto behaviour from character position to first node
CompositeBehavior newBehaviour = new GoToBehavior();
newBehaviour.SetOwner(this.Owner);
//newBehaviour.gr= this.Map;
// loop
for (int index = 0; index < nodesToVisit.Count - 1; index++)
{
CompositeBehavior newIBehaviour = new GoToBehavior();
newIBehaviour.SetOwner(this.Owner);
((GoToBehavior)(newIBehaviour)).StartNode = nodesToVisit[index];
((GoToBehavior)(newIBehaviour)).EndNode = nodesToVisit[index + 1];
SubBehaviors.Add(newIBehaviour);
}
newBehaviour = new GoToBehavior();
newBehaviour.SetOwner(Owner);
((GoToBehavior)(newBehaviour)).StartNode = nodesToVisit[nodesToVisit.Count - 1];
((GoToBehavior)(newBehaviour)).EndNode = nodesToVisit[0];
SubBehaviors.Add(newBehaviour);
// add goto behaviour from last position to first position
}
initialized = true;
}
base.Update(gameTime);
}
}
} |