root/src/BRAINSFramework/Behaviors/GoToBehavior.cs

1322
6
using Brains.Framework.Behaviors.PathFinding;
6
using Brains.Framework.Behaviors.PathFinding;
7
using Microsoft.Xna.Framework;
7
using Microsoft.Xna.Framework;
8
using Brains.Framework.Map;
8
using Brains.Framework.Map;
9
using Brains.Framework.Utility;
9
10
10
namespace Brains.Framework.Behaviors
11
namespace Brains.Framework.Behaviors
11
{
12
{
12
    public class GoToBehavior :BehaviorBase,IActionBehavior
13
    public class GoToBehavior :CompositeBehavior,IActionBehavior
13
    {
14
    {
14
        protected BehaviorBase findPath = new FindPathBehavior();
15
        protected CompositeBehavior findPath = new FindPathBehavior();
15
        protected BehaviorBase followPath = new FollowPathBehavior();
16
        protected CompositeBehavior followPath = new FollowPathBehavior();
16
        GridCell _startNode;
17
        GridCell _startNode;
17
        GridCell _endNode;
18
        GridCell _endNode;
18
        Journey currentJourney;
19
        Journey currentJourney;
...
...
75
            SubJournies.Clear();
76
            SubJournies.Clear();
76
            GridCell start = StartNode;
77
            GridCell start = StartNode;
77
            GridCell end = EndNode;
78
            GridCell end = EndNode;
78
            if (StartNode.Parent != EndNode.Parent)
79
            
80
            List<int> grididschecked = new List<int>();
81
            if (start.Parent != end.Parent)
79
            {
82
            {
80
                if (end.Parent.Position.X > start.Parent.Position.X) //Right
83
                GridCell currentStartCell;
84
                GridCell currentEndCell;
85
86
                Grid startGrid = StartNode.Parent;
87
                Grid endGrid = EndNode.Parent;
88
                int startId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(startGrid);
89
                int endId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(endGrid);
90
91
92
                List<Grid> gridsBetween = GetGridsBetween(StartNode, EndNode);
93
94
                int ii = 0;
95
                foreach (var item in gridsBetween)
81
                {
96
                {
82
                    end = start.Parent.GetCell(start.Parent.Cols - 1, end.Y);
97
                    GridCell startJourney = StartNode;
83
                    SubJournies.Add(new Journey(start, end));
98
                    GridCell endJourney = null;
84
99
85
                    start = EndNode.Parent.GetCell(0, end.Y);
100
                    int index = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(item);
86
                    end = EndNode;
101
                    int y = (index - (index % Owner.ParentWorld.Map.ClusterGrid.Cols)) / Owner.ParentWorld.Map.ClusterGrid.Cols;
87
                    SubJournies.Add(new Journey(start, end));
102
                    int x = index % Owner.ParentWorld.Map.ClusterGrid.Cols;
103
                    Console.WriteLine(x + " : " + y);
104
                    if (item == startGrid)
105
                    {
106
                        Grid next = gridsBetween[ii + 1];
107
108
                        Grid nextPlusOne = gridsBetween[ii + 2];
109
                        foreach (var path in next.PredeterminedPaths)
110
                        {
111
                            int tmpindex = index;
112
                            int tmpindex1 =
113
                                Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(nextPlusOne);
114
                            if (path.FromGrid == tmpindex &&
115
                                path.ToGrid == tmpindex1)
116
                            {
117
                                int _newX = path.Nodes[0].X;
118
                                int _newY = path.Nodes[0].X;
119
                                if (path.Nodes[0].X == 0)
120
                                    _newX = startGrid.Cols - 1;
121
                                if (path.Nodes[0].X == startGrid.Cols - 1)
122
                                    _newX = 0;
123
                                if (path.Nodes[0].Y == 0)
124
                                    _newY = startGrid.Rows - 1;
125
                                if (path.Nodes[0].Y == startGrid.Rows - 1)
126
                                    _newY = 0;
127
                                Journey j = new Journey(StartNode,
128
                                    startGrid.GetCell(_newX, _newY));
129
                            }
130
131
                        }
132
                        //Journey j=new Journey(StartNode,
133
                        //startGrid.GetCell(
134
                    }
135
                    else
136
                    {
137
                        Grid next = gridsBetween[ii + 1];
138
                        Grid nextPlusOne = gridsBetween[ii + 2];
139
                        foreach (var path in next.PredeterminedPaths)
140
                        {
141
                            int tmpindex = index;
142
                            int tmpindex1 =
143
                                Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(nextPlusOne);
144
                            if (path.FromGrid == tmpindex &&
145
                                path.ToGrid == tmpindex1)
146
                            {
147
                                int _newX = path.Nodes[0].X;
148
                                int _newY = path.Nodes[0].X;
149
                                if (path.Nodes[0].X == 0)
150
                                    _newX = startGrid.Cols - 1;
151
                                if (path.Nodes[0].X == startGrid.Cols - 1)
152
                                    _newX = 0;
153
                                if (path.Nodes[0].Y == 0)
154
                                    _newY = startGrid.Rows - 1;
155
                                if (path.Nodes[0].Y == startGrid.Rows - 1)
156
                                    _newY = 0;
157
                                Journey j = new Journey(item.GetCell(_newX, _newY),
158
                                    item.GetCell(
159
                                    path.Nodes[path.Nodes.Count - 1].X,
160
                                    path.Nodes[path.Nodes.Count - 1].Y));
161
                            }
162
                            else
163
                            {
164
                            }
165
                        }
166
                    }
167
                    ii++;
88
                }
168
                }
89
                else if (end.Parent.Position.X < start.Parent.Position.X) //Left
169
                currentStartCell = StartNode;
170
171
                bool finished = false;
172
                Grid current = startGrid;
173
                int currentId = startId;
174
175
                Grid closest = null;
176
                Cluster cluster = Owner.ParentWorld.Map.ClusterGrid;
177
                int lastId = 0;
178
179
180
181
182
            }
183
            else
184
                SubJournies.Add(new Journey(start, end));
185
186
187
188
            
189
            
190
        }
191
192
       
193
        private List<Grid> GetGridsBetween(GridCell StartNode, GridCell EndNode)
194
        {
195
            Grid start = StartNode.Parent;
196
            Grid end = EndNode.Parent;
197
            int startId=Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(start);
198
            int endId=Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(end);
199
            List<Grid> ids = new List<Grid>();
200
            ids.Add(start);
201
            bool finished=false;
202
            Grid current=start;
203
            int currentId=startId;
204
            Grid closest=null;
205
            Cluster cluster = Owner.ParentWorld.Map.ClusterGrid;
206
            while (!finished)
207
            {
208
                float dist = 0;
209
                int y = (currentId - (currentId % cluster.Cols)) / cluster.Cols;
210
                int x = currentId% cluster.Cols;
211
                
212
                //Top
213
                if(y>0)
90
                {
214
                {
91
                    end = start.Parent.GetCell(0, end.Y);
215
                    int newindex = (y - 1) * cluster.Cols + x;
92
                    SubJournies.Add(new Journey(start, end));
216
                    Grid topGrid = cluster.Grids[newindex];
217
                    float tmpDist = Vector2.Distance(topGrid.Center, end.Center);
218
                    if (tmpDist < dist || closest == null)
219
                    {
220
                        dist=tmpDist;
221
                        closest=topGrid;
222
                    }
223
                }
93
224
94
                    start = EndNode.Parent.GetCell(end.Parent.Cols - 1, end.Y);
225
                //Bottom
95
                    end = EndNode;
226
                if (y<cluster.Rows-1)
96
                    SubJournies.Add(new Journey(start, end));
227
                {
228
                    int newindex = (y + 1) * cluster.Cols + x;
229
                    Grid bottomGrid = cluster.Grids[newindex];
230
                    float tmpDist = Vector2.Distance(bottomGrid.Center, end.Center);
231
                    if (tmpDist < dist || closest == null)
232
                    {
233
                        dist = tmpDist;
234
                        closest = bottomGrid;
235
                    }
97
                }
236
                }
98
                else if (end.Parent.Position.Y > start.Parent.Position.Y) //Down
237
238
                //Right
239
                if (x<cluster.Cols-1)
99
                {
240
                {
100
                    end = start.Parent.GetCell(end.X, start.Parent.Rows- 1);
241
                    int newindex = y * cluster.Cols + (x+1);
101
                    SubJournies.Add(new Journey(start, end));
242
                    Grid rightGrid = cluster.Grids[newindex];
243
                    float tmpDist = Vector2.Distance(rightGrid.Center, end.Center);
244
                    if (tmpDist < dist || closest == null)
245
                    {
246
                        dist = tmpDist;
247
                        closest = rightGrid;
248
                    }
249
                }
102
250
103
                    start = EndNode.Parent.GetCell(end.X, 0);
251
                //Left
104
                    end = EndNode;
252
                if (x>0)
105
                    SubJournies.Add(new Journey(start, end));
253
                {
254
                    int newindex = y * cluster.Cols + (x - 1);
255
                    Grid leftGrid = cluster.Grids[newindex];
256
                    float tmpDist = Vector2.Distance(leftGrid.Center, end.Center);
257
                    if (tmpDist < dist || closest == null)
258
                    {
259
                        dist = tmpDist;
260
                        closest = leftGrid;
261
                    }
106
                }
262
                }
263
264
                if (dist > 0)
265
                {
266
                    ids.Add(closest);
267
                    current = closest;
268
                    currentId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(closest);
269
                }
107
                else
270
                else
108
                {
271
                {
109
                    end = start.Parent.GetCell(end.X, 0);
272
                    if (closest == end)
110
                    SubJournies.Add(new Journey(start, end));
273
                    {
111
274
                        finished = true;
112
                    start = EndNode.Parent.GetCell(end.X, end.Parent.Rows - 1);
275
                    }
113
                    end = EndNode;
114
                    SubJournies.Add(new Journey(start, end));
115
                }
276
                }
277
                
278
                closest = null;
116
            }
279
            }
117
            else
118
            {
119
                SubJournies.Add(new Journey(start, end));
120
            }
121
280
122
            
281
            ids.Add(end);
282
            return ids;
123
        }
283
        }
124
284
125
        public override void Update(GameTime gameTime)
285
        public override void Update(GameTime gameTime)