Vector3 GetPosition(Struct.DoubleIndex index) { Vector3 worldPosition = new Vector3(field.position.x + index.first, 0, field.position.z + index.second); worldPosition += wallSize / 2; return(worldPosition); }
Vector3 TopLocation() { if (pathPoints == null || pathPoints.Count == 0) { return(targetLocation); } Struct.DoubleIndex nextPoint = pathPoints[pathPoints.Count - 1]; pathPoints.RemoveAt(pathPoints.Count - 1); return(GetPosition(nextPoint)); }
public void SetDestination(Vector3 targetPosition) { Struct.DoubleIndex enemyIndex = new Struct.DoubleIndex(0, 0); Struct.DoubleIndex targetIndex = new Struct.DoubleIndex(0, 0); FindIndex(ref enemyIndex, gameObject.transform.position); FindIndex(ref targetIndex, targetPosition); char [,] movementMap = NavigationManager.Instance.movementMap; char wallFlag = NavigationManager.Instance.wallFlag; pathPoints = ShortestPathAlgorithms.Dfs(movementMap, enemyIndex, targetIndex, wallFlag); targetLocation = gameObject.transform.position; }
public void SetDestination(Vector3 targetPosition) { Struct.DoubleIndex enemyIndex = new Struct.DoubleIndex(0, 0); Struct.DoubleIndex targetIndex = new Struct.DoubleIndex(0, 0); FindIndex (ref enemyIndex, gameObject.transform.position); FindIndex (ref targetIndex, targetPosition); char [,] movementMap = NavigationManager.Instance.movementMap; char wallFlag = NavigationManager.Instance.wallFlag; pathPoints = ShortestPathAlgorithms.Dfs (movementMap, enemyIndex, targetIndex, wallFlag); targetLocation = gameObject.transform.position; }
public static List <Struct.DoubleIndex> Dfs(char [,] map, Struct.DoubleIndex current, Struct.DoubleIndex target, char wall, bool isNew = true) { if (isNew) { currentPath.Clear(); bestPath = null; } currentPath.Insert(0, current.Clone()); if (current.Equals(target) && (bestPath == null || bestPath.Count > currentPath.Count)) { bestPath = new List <Struct.DoubleIndex> (currentPath); } if (bestPath != null && bestPath.Count <= currentPath.Count) { return(bestPath); } for (int i = 0; i < directionsX.Length; i++) { current.first += directionsY[i]; current.second += directionsX[i]; if (!currentPath.Contains(current) && CanMove(map, current, wall)) { Dfs(map, current, target, wall, false); } current.first -= directionsY[i]; current.second -= directionsX[i]; } currentPath.RemoveAt(0); return(bestPath); }
static bool CanMove(char [,] map, Struct.DoubleIndex index, char wall) { return(index.first >= 0 && index.second >= 0 && index.first < map.GetLength(0) && index.second < map.GetLength(1) && map[index.first, index.second] != wall); }
void FindIndex(ref Struct.DoubleIndex index, Vector3 targetPosition) { index.first = Mathf.Abs((int)(field.position.x - targetPosition.x)); index.second = Mathf.Abs((int)(field.position.z - targetPosition.z)); }