public void AddNeighbor(TileNode neighbor) { if (DJ_Util.GetDistance(pos, neighbor.pos, DJ_Distance.PATH) <= 1) { float dx = neighbor.pos.X - pos.X; float dy = neighbor.pos.Y - pos.Y; if (dx != dy) { if (dx > 0) { neighbors[(int)DJ_Dir.RIGHT] = neighbor; } else if (dx < 0) { neighbors[(int)DJ_Dir.LEFT] = neighbor; } else if (dy > 0) { neighbors[(int)DJ_Dir.UP] = neighbor; } else if (dy < 0) { neighbors[(int)DJ_Dir.DOWN] = neighbor; } } } }
private bool GetNextMove(DJ_Point from, DJ_Point to, float currDist, float maxDist, DJ_Distance distanceType, float currentCost) { if (from.Equals(to)) { return(true); } if (!tileMap.ContainsKey(to) && DJ_Util.GetDistance(from, to, DJ_Distance.EUCLIDEAN) <= 1) { return(false); } if (currDist > maxDist) { return(true); } if (tileMap.ContainsKey(from)) { DJ_Dir prefDir = DJ_Util.GetPreferredDirection(from, to); int index; //immediately return none if the preferred direction //happens to be none. This either means the positions, from //and to, are the same or that there is no path from "from" to "to" if (prefDir == DJ_Dir.NONE) { return(true); } if (tileMap[from].neighbors[(int)prefDir] != null && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[(int)prefDir].pos)) { if (!tilesVisited.Contains(tileMap[from].neighbors[(int)prefDir].pos)) { tilesVisited.Add(tileMap[from].neighbors[(int)prefDir].pos); currentCosts.Add(currentCost); } else { index = tilesVisited.IndexOf(tileMap[from].neighbors[(int)prefDir].pos); float currCost = currentCosts[index]; if (currentCost < currCost) { tilesVisited.RemoveAt(index); currentCosts.RemoveAt(index); tilesVisited.Add(tileMap[from].neighbors[(int)prefDir].pos); currentCosts.Add(currentCost); } else { return(false); } } if (GetNextMove(tileMap[from].neighbors[(int)prefDir].pos, to, currDist + 1.0f, maxDist, distanceType, currentCost + 1.0f)) { return(true); } index = tilesVisited.IndexOf(tileMap[from].neighbors[(int)prefDir].pos); currentCosts.RemoveAt(index); tilesVisited.Remove(tileMap[from].neighbors[(int)prefDir].pos); } for (int i = 0; i < tileMap[from].neighbors.Length; ++i) { if (tileMap[from].neighbors[i] != null && i != (int)prefDir && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[i].pos)) { tilesVisited.Add(tileMap[from].neighbors[i].pos); currentCosts.Add(currentCost); if (GetNextMove(tileMap[from].neighbors[i].pos, to, currDist + 1.0f, maxDist, distanceType, currentCost + 1.0f)) { return(true); } index = tilesVisited.IndexOf(tileMap[from].neighbors[i].pos); currentCosts.RemoveAt(index); tilesVisited.Remove(tileMap[from].neighbors[i].pos); } } } return(false); }