public Tile(TVec2 ID, TVec3 scale, TVec3 position, TMesh cube, TMesh ball, bool walkable) { this.ID = ID; this.cube = LE.CopyEntity(cube); this.ball = LE.CopyEntity(ball); this.walkable = walkable; LE.ShowEntity(this.cube); //set position and scale LE.ScaleEntity(this.cube, scale); LE.PositionEntity(this.cube, position); LE.ScaleEntity(this.ball, new TVec3(scale.X / 2, scale.Y / 2, scale.Z / 2)); LE.PositionEntity(this.ball, new TVec3(position.X, position.Y + 1, position.Z)); //Set a different Color when the tile is non walkable if (!this.walkable) { LE.EntityColor(this.cube, Tile.BLACK); } else { LE.EntityColor(this.cube, Tile.PINK); LE.EntityColor(this.ball, Tile.YELLOW); } }
/////////////////////// A* /////////////////////// ///add the starting node to the open list ///while the open list is not empty /// { /// current node = node from open list with the lowest cost /// if currentnode = goal /// path complete /// else /// move current node to the closed list /// for each adjacent node /// if it lies with the field /// and it isn't an obstacle /// and it isn't on the open list /// and isn't on the closed list /// move it to the open list and calculate cost ////////////////////////////////////////////////// #endregion public void SearchPath(TVec2 startTile, TVec2 endTile) { this.startTile = startTile; this.endTile = endTile; //Reset all the values for (int i = 0; i < AStartTest.gridWidth; i++) { for (int j = 0; j < AStartTest.gridHeight; j++) { grid[i, j].cost = 0; grid[i, j].heuristic = 0; } } #region Path validation bool canSearch = true; if (grid[(int)startTile.X, (int)startTile.Y].walkable == false) { Console.WriteLine("The start tile is non walkable. Choose a different value than: " + startTile.ToString()); canSearch = false; } if (grid[(int)endTile.X, (int)endTile.Y].walkable == false) { Console.WriteLine("The end tile is non walkable. Choose a different value than: " + endTile.ToString()); canSearch = false; } #endregion //Start the A* algorithm if (canSearch) { //add the starting tile to the open list openList.Add(startTile); currentTile = new TVec2(-1, -1); //while Openlist is not empty while (openList.Count != 0) { //current node = node from open list with the lowest cost currentTile = GetTileWithLowestTotal(openList); //If the currentTile is the endtile, then we can stop searching if (currentTile.X == endTile.X && currentTile.Y == endTile.Y) { Console.WriteLine("YEHA, We found the end tile!!!! :D"); break; } else { //move the current tile to the closed list and remove it from the open list openList.Remove(currentTile); closedList.Add(currentTile); //Get all the adjacent Tiles List <TVec2> adjacentTiles = GetAdjacentTiles(currentTile); foreach (TVec2 adjacentTile in adjacentTiles) { //adjacent tile can not be in the open list if (!openList.Contains(adjacentTile)) { //adjacent tile can not be in the closed list if (!closedList.Contains(adjacentTile)) { //move it to the open list and calculate cost openList.Add(adjacentTile); Tile tile = grid[(int)adjacentTile.X, (int)adjacentTile.Y]; //Calculate the cost tile.cost = grid[(int)currentTile.X, (int)currentTile.Y].cost + 1; //Calculate the manhattan distance tile.heuristic = ManhattanDistance(adjacentTile); //calculate the total amount tile.total = tile.cost + tile.heuristic; //make this tile green LE.EntityColor(tile.cube, Tile.GREEN); } } } } } } //Pain the start and end tile red LE.EntityColor(grid[(int)startTile.X, (int)startTile.Y].cube, Tile.RED); LE.EntityColor(grid[(int)endTile.X, (int)endTile.Y].cube, Tile.RED); //Show the path ShowPath(); }