Skip to content

Commit 18517fd

Browse files
authored
3d pathfinding (#1467)
* 3d Pathfinding * Work tiles above and below * CompareTo to == * Who yah gonna call? Pathfinders!
1 parent 19ee676 commit 18517fd

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

Assets/Scripts/Controllers/Sprites/CharacterSpriteController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected override void OnChanged(Character character)
123123

124124
GameObject char_go = objectGameObjectMap[character];
125125

126-
char_go.transform.position = new Vector3(character.X, character.Y, 0);
126+
char_go.transform.position = new Vector3(character.X, character.Y, character.Z);
127127
}
128128

129129
protected override void OnRemoved(Character character)

Assets/Scripts/Models/Buildable/Tile.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,30 @@ public bool IsNeighbour(Tile tile, bool diagOkay = false)
345345
/// <param name="diagOkay">Is diagonal movement okay?.</param>
346346
public Tile[] GetNeighbours(bool diagOkay = false)
347347
{
348-
Tile[] tiles = diagOkay == false ? new Tile[4] : new Tile[8];
348+
Tile[] tiles = diagOkay == false ? new Tile[6] : new Tile[10];
349349
tiles[0] = World.Current.GetTileAt(X, Y + 1, Z);
350350
tiles[1] = World.Current.GetTileAt(X + 1, Y, Z);
351351
tiles[2] = World.Current.GetTileAt(X, Y - 1, Z);
352352
tiles[3] = World.Current.GetTileAt(X - 1, Y, Z);
353353

354+
// FIXME: This is a bit of a dirty hack, but it works for preventing characters from phasing through the floor for now.
355+
Tile tileup = World.Current.GetTileAt(X, Y, Z - 1);
356+
if (tileup != null && tileup.Type == TileType.Empty)
357+
{
358+
tiles[4] = World.Current.GetTileAt(X, Y, Z - 1);
359+
}
360+
361+
if (Type == TileType.Empty)
362+
{
363+
tiles[5] = World.Current.GetTileAt(X, Y, Z + 1);
364+
}
365+
354366
if (diagOkay == true)
355367
{
356-
tiles[4] = World.Current.GetTileAt(X + 1, Y + 1, Z);
357-
tiles[5] = World.Current.GetTileAt(X + 1, Y - 1, Z);
358-
tiles[6] = World.Current.GetTileAt(X - 1, Y - 1, Z);
359-
tiles[7] = World.Current.GetTileAt(X - 1, Y + 1, Z);
368+
tiles[6] = World.Current.GetTileAt(X + 1, Y + 1, Z);
369+
tiles[7] = World.Current.GetTileAt(X + 1, Y - 1, Z);
370+
tiles[8] = World.Current.GetTileAt(X - 1, Y - 1, Z);
371+
tiles[9] = World.Current.GetTileAt(X - 1, Y + 1, Z);
360372
}
361373

362374
return tiles.Where(tile => tile != null).ToArray();

Assets/Scripts/Models/Job/Job.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,7 @@ public Pathfinder.GoalEvaluator IsTileAtJobSite
225225
}
226226

227227
// TODO: This doesn't handle multi-tile furniture
228-
if (adjacent)
229-
{
230-
return otherTile => (
231-
tile.Z == otherTile.Z &&
232-
(tile.X - 1) <= otherTile.X && (tile.X + 1) >= otherTile.X &&
233-
(tile.Y - 1) <= otherTile.Y && (tile.Y + 1) >= otherTile.Y &&
234-
tile.IsClippingCorner(otherTile) == false);
235-
}
236-
else
237-
{
238-
return otherTile => this.tile.CompareTo(otherTile) == 0;
239-
}
228+
return Pathfinder.GoalTileEvaluator(tile, adjacent);
240229
}
241230
}
242231

Assets/Scripts/Pathfinding/Path_TileGraph.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public Path_TileGraph(World world)
3232
{
3333
for (int y = 0; y < world.Height; y++)
3434
{
35-
Tile t = world.GetTileAt(x, y, 0);
35+
for (int z = 0; z < world.Depth; z++)
36+
{
37+
Tile t = world.GetTileAt(x, y, z);
3638

37-
////if(t.movementCost > 0) { // Tiles with a move cost of 0 are unwalkable
38-
Path_Node<Tile> n = new Path_Node<Tile>();
39-
n.data = t;
40-
nodes.Add(t, n);
41-
////}
39+
////if(t.movementCost > 0) { // Tiles with a move cost of 0 are unwalkable
40+
Path_Node<Tile> n = new Path_Node<Tile>();
41+
n.data = t;
42+
nodes.Add(t, n);
43+
}
4244
}
4345
}
4446

Assets/Scripts/Pathfinding/Pathfinder.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static PathfindingHeuristic DefaultDistanceHeuristic(Tile goalTile)
155155
/// </summary>
156156
public static PathfindingHeuristic ManhattanDistance(Tile goalTile)
157157
{
158-
return tile => Mathf.Abs(tile.X - goalTile.X) + Mathf.Abs(tile.Y - goalTile.Y);
158+
return tile => Mathf.Abs(tile.X - goalTile.X) + Mathf.Abs(tile.Y - goalTile.Y) + Mathf.Abs(tile.Z - goalTile.Z);
159159
}
160160

161161
/// <summary>
@@ -177,15 +177,21 @@ public static GoalEvaluator GoalTileEvaluator(Tile goalTile, bool adjacent)
177177
int maxX = goalTile.X + 1;
178178
int minY = goalTile.Y - 1;
179179
int maxY = goalTile.Y + 1;
180+
int minZ = goalTile.Z - 1;
181+
int maxZ = goalTile.Z + 1;
180182

181183
return tile => (
182-
tile.X >= minX && tile.X <= maxX &&
184+
(tile.X >= minX && tile.X <= maxX &&
183185
tile.Y >= minY && tile.Y <= maxY &&
184-
goalTile.IsClippingCorner(tile) == false);
186+
tile.Z == goalTile.Z &&
187+
goalTile.IsClippingCorner(tile) == false) ||
188+
(tile.Z >= minZ && tile.Z <= maxZ &&
189+
tile.X == goalTile.X &&
190+
tile.Y == goalTile.Y));
185191
}
186192
else
187193
{
188-
return tile => goalTile == tile;
194+
return tile => tile == goalTile;
189195
}
190196
}
191197

0 commit comments

Comments
 (0)