/
Path.cs
103 lines (95 loc) · 3.89 KB
/
Path.cs
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
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BattleFigures {
static public class Path {
static public int[,] tempMap;
static public List<Point> tempObstacles = new List<Point>();
static public List<Point> FindMovementRadius(Point[] points, int movementPoints, int[,] map) {
int d = 0;
List<Point> wave = new List<Point>();
List<Point> waveNew = new List<Point>();
List<Point> waveRes = new List<Point>();
tempObstacles = new List<Point>();
wave.AddRange(points);
for (; d < movementPoints && wave.Count != 0; d++) {
for (int i = 0; i < wave.Count; i++) {
if (wave[i].Y - 1 >= 0) {
if (map[wave[i].Y - 1, wave[i].X] == 0) {
map[wave[i].Y - 1, wave[i].X] = d + 1;
waveNew.Add(new Point(wave[i].X, wave[i].Y - 1));
} else tempObstacles.Add(new Point(wave[i].X, wave[i].Y - 1));
}
if (wave[i].X + 1 < GameLogic.Field.Width) {
if (map[wave[i].Y, wave[i].X + 1] == 0) {
map[wave[i].Y, wave[i].X + 1] = d + 1;
waveNew.Add(new Point(wave[i].X + 1, wave[i].Y));
} else tempObstacles.Add(new Point(wave[i].X + 1, wave[i].Y));
}
if (wave[i].Y + 1 < GameLogic.Field.Height) {
if (map[wave[i].Y + 1, wave[i].X] == 0) {
map[wave[i].Y + 1, wave[i].X] = d + 1;
waveNew.Add(new Point(wave[i].X, wave[i].Y + 1));
} else tempObstacles.Add(new Point(wave[i].X, wave[i].Y + 1));
}
if (wave[i].X - 1 >= 0) {
if (map[wave[i].Y, wave[i].X - 1] == 0) {
map[wave[i].Y, wave[i].X - 1] = d + 1;
waveNew.Add(new Point(wave[i].X - 1, wave[i].Y));
} else tempObstacles.Add(new Point(wave[i].X - 1, wave[i].Y));
}
}
wave = new List<Point>(waveNew);
waveRes.AddRange(waveNew);
waveNew = new List<Point>();
}
tempMap = map;
return waveRes;
}
static public List<Point> FindMovementRadius(Point point, int movementPoints) {
return FindMovementRadius(new Point[] { point }, movementPoints, FieldToMap());
}
static public List<Point> FindMovementRadius(Point[] points, int movementPoints) {
return FindMovementRadius(points, movementPoints, FieldToMap());
}
static public List<Point> FindPath(Point[] from, Point to, int[,] map) {
List<Point> path = new List<Point>();
path.Add(to);
foreach (Point p in from)
if (Math.Abs(p.Y - to.Y) + Math.Abs(p.X - to.X) == 1) {
return path;
}
while (!from.Contains(to)) {
if (to.Y - 1 >= 0 && map[to.Y - 1, to.X] == map[to.Y, to.X] - 1) {
to = new Point(to.X, to.Y - 1);
path.Add(to);
} else if (to.X + 1 < GameLogic.Field.Width && map[to.Y, to.X + 1] == map[to.Y, to.X] - 1) {
to = new Point(to.X + 1, to.Y);
path.Add(to);
} else if (to.Y + 1 < GameLogic.Field.Height && map[to.Y + 1, to.X] == map[to.Y, to.X] - 1) {
to = new Point(to.X, to.Y + 1);
path.Add(to);
} else if (to.X - 1 >= 0 && map[to.Y, to.X - 1] == map[to.Y, to.X] - 1) {
to = new Point(to.X - 1, to.Y);
path.Add(to);
} else break;
}
return path;
}
static public List<Point> FindPath(Point from, Point to) {
return FindPath(new Point[] { from }, to, tempMap);
}
static public List<Point> FindPath(Point[] from, Point to) {
return FindPath(from, to, tempMap);
}
static public int[,] FieldToMap() {
int[,] map = new int[GameLogic.Field.Height, GameLogic.Field.Width];
for (int i = 0; i < GameLogic.Field.Height; i++)
for (int j = 0; j < GameLogic.Field.Width; j++)
map[i, j] = GameLogic.Field[i, j].Empty ? 0 : -1;
return map;
}
}
}