1
- using HexBoardGame . SharedData ;
1
+ using System . Collections . Generic ;
2
+ using HexBoardGame . SharedData ;
2
3
using Tools . Extensions . Arrays ;
3
4
using UnityEngine ;
4
5
5
6
namespace HexBoardGame . Runtime
6
7
{
7
8
/// <summary>
8
9
/// The way to manipulate a board in the Odd-Row layout.
10
+ /// TODO: Open for many memory/cache optimizations and algorithms improvements.
9
11
/// </summary>
10
12
public class BoardManipulationOddR : IBoardManipulation
11
13
{
@@ -30,8 +32,7 @@ public Hex[] GetNeighbours(Vector3Int cell)
30
32
foreach ( var direction in NeighboursDirections )
31
33
{
32
34
var neighbour = Hex . Add ( center [ 0 ] , direction ) ;
33
- var array = new [ ] { neighbour } ;
34
- neighbours = neighbours . Append ( array ) ;
35
+ neighbours = neighbours . Append ( GetIfExistsOrEmpty ( neighbour ) ) ;
35
36
}
36
37
37
38
return neighbours ;
@@ -42,33 +43,34 @@ public Hex[] GetNeighbours(Vector3Int cell)
42
43
/// </summary>
43
44
private Hex [ ] GetIfExistsOrEmpty ( Hex hex )
44
45
{
45
- foreach ( var i in _hexPoints )
46
- if ( i == hex )
47
- return new [ ] { i } ;
48
-
49
- return new Hex [ ] { } ;
46
+ var cell = GetCellCoordinate ( hex ) ;
47
+ return Contains ( cell ) ? new [ ] { GetHexCoordinate ( cell ) } : new Hex [ ] { } ;
50
48
}
51
49
52
50
#region Operations
53
51
54
52
public bool Contains ( Vector3Int cell )
55
53
{
56
- var center = GetHexCoordinate ( cell ) ;
57
- return GetIfExistsOrEmpty ( center ) . Length > 0 ;
54
+ var hex = GetHexCoordinate ( cell ) ;
55
+ foreach ( var i in _hexPoints )
56
+ if ( i == hex )
57
+ return true ;
58
+ return false ;
58
59
}
59
60
60
61
public Hex [ ] GetVertical ( Vector3Int cell , int length )
61
62
{
63
+ //For Odd-R the vertical is always empty.
62
64
return new Hex [ ] { } ;
63
65
}
64
66
65
67
public Hex [ ] GetHorizontal ( Vector3Int cell , int length )
66
68
{
67
- var center = GetHexCoordinate ( cell ) ;
69
+ var point = GetHexCoordinate ( cell ) ;
68
70
var halfLength = length / 2 ;
69
- var points = GetIfExistsOrEmpty ( center ) ;
70
- var x = center . q ;
71
- var y = center . r ;
71
+ var points = GetIfExistsOrEmpty ( point ) ;
72
+ var x = point . q ;
73
+ var y = point . r ;
72
74
73
75
for ( var i = 1 ; i <= halfLength ; i ++ )
74
76
points = points . Append ( GetIfExistsOrEmpty ( new Hex ( x + i , y ) ) ) ;
@@ -81,11 +83,11 @@ public Hex[] GetHorizontal(Vector3Int cell, int length)
81
83
82
84
public Hex [ ] GetDiagonalAscendant ( Vector3Int cell , int length )
83
85
{
84
- var center = GetHexCoordinate ( cell ) ;
86
+ var point = GetHexCoordinate ( cell ) ;
85
87
var halfLength = length / 2 ;
86
- var points = GetIfExistsOrEmpty ( center ) ;
87
- var x = center . q ;
88
- var y = center . r ;
88
+ var points = GetIfExistsOrEmpty ( point ) ;
89
+ var x = point . q ;
90
+ var y = point . r ;
89
91
90
92
for ( var i = 1 ; i <= halfLength ; i ++ )
91
93
points = points . Append ( GetIfExistsOrEmpty ( new Hex ( x , y + i ) ) ) ;
@@ -98,11 +100,11 @@ public Hex[] GetDiagonalAscendant(Vector3Int cell, int length)
98
100
99
101
public Hex [ ] GetDiagonalDescendant ( Vector3Int cell , int length )
100
102
{
101
- var center = GetHexCoordinate ( cell ) ;
103
+ var point = GetHexCoordinate ( cell ) ;
102
104
var halfLength = length / 2 ;
103
- var points = GetIfExistsOrEmpty ( center ) ;
104
- var x = center . q ;
105
- var y = center . r ;
105
+ var points = GetIfExistsOrEmpty ( point ) ;
106
+ var x = point . q ;
107
+ var y = point . r ;
106
108
107
109
for ( var i = 1 ; i <= halfLength ; i ++ )
108
110
points = points . Append ( GetIfExistsOrEmpty ( new Hex ( x - i , y + i ) ) ) ;
@@ -113,6 +115,50 @@ public Hex[] GetDiagonalDescendant(Vector3Int cell, int length)
113
115
return points ;
114
116
}
115
117
118
+ public Hex [ ] GetPathBreadthSearch ( Vector3Int begin , Vector3Int end )
119
+ {
120
+ var beginHex = GetHexCoordinate ( begin ) ;
121
+ var endHex = GetHexCoordinate ( end ) ;
122
+ var frontier = new Queue < Hex > ( ) ;
123
+ frontier . Enqueue ( beginHex ) ;
124
+ var visited = new Dictionary < Hex , Hex > ( ) ;
125
+
126
+
127
+ //Creating the breadcrumbs
128
+
129
+ while ( frontier . Count > 0 )
130
+ {
131
+ var current = frontier . Dequeue ( ) ;
132
+ if ( current == endHex )
133
+ break ;
134
+
135
+ var currentCell = GetCellCoordinate ( current ) ;
136
+ var neighbours = GetNeighbours ( currentCell ) ;
137
+ foreach ( var next in neighbours )
138
+ {
139
+ if ( ! visited . ContainsKey ( next ) )
140
+ {
141
+ frontier . Enqueue ( next ) ;
142
+ visited [ next ] = current ;
143
+ }
144
+ }
145
+ }
146
+
147
+ //Backtracking from the ending point
148
+
149
+
150
+ var path = new List < Hex > ( ) ;
151
+ while ( endHex != beginHex )
152
+ {
153
+ path . Add ( endHex ) ;
154
+ endHex = visited [ endHex ] ;
155
+ }
156
+
157
+ path . Add ( beginHex ) ;
158
+ path . Reverse ( ) ;
159
+ return path . ToArray ( ) ;
160
+ }
161
+
116
162
/// <summary>
117
163
/// Unity by default makes use the R-Offset Odd to reference tiles inside a TileMap with a vector3Int cell.
118
164
/// The internal board manipulation works with HexCoordinates, this method converts vector3int cell to hex.
0 commit comments