public void FollowPath() { waitUntilMove++; GetActiveDungeon(); if (dungeonActive) { if (waitUntilMove >= speed) { waitUntilMove = 0; if (dungeon == null) { Debug.Log("NO dungeon"); } if (GetComponent <Pathfinding>().path == null) { Debug.Log("NO PATH"); } if (dungeon.TileFromWorldPoint(GetComponent <Pathfinding>().path[0].worldPos) == null) { Debug.Log("NO TILE"); } if (!dungeon.TileFromWorldPoint(GetComponent <Pathfinding>().path[0].worldPos).isPlayer) { transform.position = GetComponent <Pathfinding>().path[0].worldPos + new Vector3(0, 1, 0); } } } else if (cavernActive) { if (waitUntilMove >= speed) { waitUntilMove = 0; if (!cavern.TileFromWorldPoint(GetComponent <Pathfinding>().path[0].worldPos).isPlayer) { transform.position = GetComponent <Pathfinding>().path[0].worldPos + new Vector3(0, 1, 0); } } } }
public void FindPath(Vector3 startPos, Vector3 targetPos) { GetActiveDungeon(); if (cavernActive) { Tile startTile = cavern.TileFromWorldPoint(startPos); Tile targetTile = cavern.TileFromWorldPoint(targetPos); Heap <Tile> openSet = new Heap <Tile>(cavern.MaxSize); HashSet <Tile> closedSet = new HashSet <Tile>(); openSet.Add(startTile); while (openSet.Count > 0) { Tile currentTile = openSet.RemoveFirst(); closedSet.Add(currentTile); if (currentTile == targetTile) { RetracePath(startTile, targetTile); return; } foreach (Tile neighbour in cavern.GetNeighbours(currentTile)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newCostToNeighbour = currentTile.gCost + GetDistance(currentTile, neighbour); if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetTile); neighbour.parent = currentTile; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } } else if (dungeonActive) { Tile startTile = dungeon.TileFromWorldPoint(startPos); Tile targetTile = dungeon.TileFromWorldPoint(targetPos); Heap <Tile> openSet = new Heap <Tile>(dungeon.MaxSize); HashSet <Tile> closedSet = new HashSet <Tile>(); openSet.Add(startTile); while (openSet.Count > 0) { Tile currentTile = openSet.RemoveFirst(); closedSet.Add(currentTile); if (currentTile == targetTile) { RetracePath(startTile, targetTile); return; } foreach (Tile neighbour in dungeon.GetNeighbours(currentTile)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newCostToNeighbour = currentTile.gCost + GetDistance(currentTile, neighbour); if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetTile); neighbour.parent = currentTile; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } } }