Beispiel #1
0
    /// <summary>
    /// Returns the corresponding mapTile from a position
    /// </summary>
    /// <param name="position">A vector3 world position</param>
    /// <returns>The corresponding mapTile at position</returns>
    public MapTile getTileFromPosition(Vector3 position)
    {
        iVector2 coord = getCoordFromPosition(position);

        // Return tile from array
        return(tiles[coord.x, coord.y]);
    }
Beispiel #2
0
 public bool IsEquals(iVector2 v2)
 {
     if (x == v2.x && y == v2.y)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Gets the MapTile from an iVector2 coordinate
 /// </summary>
 /// <param name="coord">The iVector2 coordinate</param>
 /// <returns>The corresponding MapTile or null if not in range</returns>
 public MapTile getTile(iVector2 coord)
 {
     if ((coord.x >= 0 && coord.x < gridDimensions.x) && (coord.y >= 0 && coord.y < gridDimensions.y))
     {
         return(tiles[coord.x, coord.y]);
     }
     else
     {
         return(null);
     }
 }
Beispiel #4
0
    public void moveGameMap(iVector2 moveMap)
    {
        int i, j;

        if (moveMap.x != 0)
        {
            if (moveMap.x == 1)
            {
                for (i = mapSize.x; i > 0; i--)
                {
                    for (j = 0; j <= mapSize.z; j++)
                    {
                        gameMap[i, j] = gameMap[i - 1, j];
                    }
                }
            }
            else if (moveMap.x == -1)
            {
                for (i = 0; i < mapSize.z; i++)
                {
                    for (j = 0; j <= mapSize.z; j++)
                    {
                        gameMap[i, j] = gameMap[i + 1, j];
                    }
                }
            }
        }
        else if (moveMap.z != 0)
        {
            if (moveMap.z == 1)
            {
                for (i = mapSize.z; i > 0; i--)
                {
                    for (j = 0; j <= mapSize.x; j++)
                    {
                        gameMap[j, i] = gameMap[j, i - 1];
                    }
                }
            }
            else if (moveMap.z == -1)
            {
                for (i = 0; i < mapSize.z; i++)
                {
                    for (j = 0; j <= mapSize.x; j++)
                    {
                        gameMap[j, i] = gameMap[j, i + 1];
                    }
                }
            }
        }
    }
Beispiel #5
0
    private iVector2 gridDimensions; // A local copy of the map's grid dimensions.

    // Use this for initialization
    void Start()
    {
        path = gameObject.AddComponent <Path>(); // Create a path attached to this game object

        gridDimensions = mapGrid.getGridDimensions();

        tileData = new LocalTile[gridDimensions.x, gridDimensions.y]; // Allocate size of local tileData array

        for (int x = 0; x < gridDimensions.x; x++)                    // Loop through mapGrid tiles
        {
            for (int y = 0; y < gridDimensions.y; y++)
            {
                tileData[x, y] = new LocalTile(mapGrid.getTile(new iVector2(x, y))); // Create a new localTile
            }
        }
    }
Beispiel #6
0
    /// <summary>
    /// Finds the closest walkable tile
    /// </summary>
    /// <param name="tile">The target tile</param>
    /// <returns>The closest walkable neighbour of this tile</returns>
    MapTile findWalkableNeighbour(MapTile tile)
    {
        iVector2 closestNeighbour = new iVector2();
        float    lowestHeuristic  = Mathf.Infinity;

        // Loop through tileData and remember the walkable node with the lowest heuristic
        for (int x = 0; x < tileData.GetLength(0); x++)
        {
            for (int y = 0; y < tileData.GetLength(1); y++)
            {
                if (tileData[x, y].heuristic < lowestHeuristic && tileData[x, y].tile.walkable)
                {
                    closestNeighbour.x = x;
                    closestNeighbour.y = y;
                    lowestHeuristic    = tileData[x, y].heuristic;
                }
            }
        }

        return(mapGrid.getTile(closestNeighbour));
    }
Beispiel #7
0
    /// <summary>
    /// Calculates the heuristic values for all tiles
    /// </summary>
    /// <returns>The tile with the lowest heuristic</returns>
    MapTile calculateHeuristics()
    {
        float    lowestHeuristic = Mathf.Infinity; // Lowest heuristic cost for each heuristic to be tested againts (start high to ensure an initial value is found)
        iVector2 closestTile     = new iVector2(); // The grid coordinates of the tile

        for (int x = 0; x < gridDimensions.x; x++) // Loop through mapGrid
        {
            for (int y = 0; y < gridDimensions.y; y++)
            {
                Vector3 distance = targetPos - mapGrid.getTile(new iVector2(x, y)).position; // Get distance vector between target and current tile

                tileData[x, y].heuristic = distance.magnitude;                               // Assign the magnitude of this distance vector to tile's heuristic

                if (distance.magnitude < lowestHeuristic)                                    // If it is the current lowest heuristic found
                {
                    lowestHeuristic = distance.magnitude;                                    // Remember the tile and heuristic
                    closestTile.x   = x;
                    closestTile.y   = y;
                }
            }
        }

        return(mapGrid.getTile(closestTile)); // Return the mapGrid tile with the lowest heuristic
    }
Beispiel #8
0
 public iVector2 Sub(iVector2 a)
 {
     this.x -= a.x;
     this.y -= a.y;
     return(this);
 }
Beispiel #9
0
 public iVector2 Add(iVector2 a)
 {
     this.x += a.x;
     this.y += a.y;
     return(this);
 }
Beispiel #10
0
    /// <summary>
    /// Carries out the A* search
    /// </summary>
    void search()
    {
        List <LocalTile> fringe = new List <LocalTile>();                                                                                 // Stores possible next moves

        LocalTile currentTile = tileData[mapGrid.getCoordFromPosition(start.position).x, mapGrid.getCoordFromPosition(start.position).y]; // Set current tile to start

        while (currentTile.tile != end)                                                                                                   // While it hasn't reached the end tile
        {
            currentTile.visited = true;                                                                                                   // Mark tile as visited

            iVector2 coord = mapGrid.getCoordFromPosition(currentTile.tile.position);                                                     // Remember grid coord of tile (to easily reference neighbours)

            List <LocalTile> neighbours = new List <LocalTile>();                                                                         // Create a new list of its neighbours

            if (coord.x > 0)                                                                                                              // Current tile isn't on left border
            {
                neighbours.Add(tileData[coord.x - 1, coord.y]);
            }

            if (coord.x < gridDimensions.x - 1) // Current tile isn't on right border
            {
                neighbours.Add(tileData[coord.x + 1, coord.y]);
            }

            if (coord.y > 0) // Current tile isn't on bottom border
            {
                neighbours.Add(tileData[coord.x, coord.y - 1]);
            }

            if (coord.y < gridDimensions.y - 1) // Current tile isn't on top border
            {
                neighbours.Add(tileData[coord.x, coord.y + 1]);
            }

            for (int i = 0; i < neighbours.Count; i++) // Loop through neighbours
            {
                LocalTile currentNeighbour = neighbours[i];

                if (!currentNeighbour.visited && currentNeighbour.tile.walkable)                    // If current neighbour hasn't been visited AND is walkable
                {
                    if (currentNeighbour.cost == 0 || currentNeighbour.cost > currentTile.cost + 1) // Check if a cost has not yet been established, or if the cost from this tile is cheaper
                    {
                        currentNeighbour.cost         = currentTile.cost + 1;                       // Set new cost and previousTile
                        currentNeighbour.previousTile = currentTile;

                        if (!currentNeighbour.inFringe)   // If this neighbour is not in fringe
                        {
                            fringe.Add(currentNeighbour); // Add it
                            currentNeighbour.inFringe = true;
                        }
                    }
                }
            }

            if (fringe.Count == 0) // If fringe is empty, return early (to counter a possible infinite loop)
            {
                return;
            }

            // Find the next lowest costing node to move to next
            int lowestMove = 0;
            for (int i = 0; i < fringe.Count; i++)
            {
                if (fringe[i].heuristic + fringe[i].cost < fringe[lowestMove].heuristic + fringe[lowestMove].cost)
                {
                    lowestMove = i;
                }
            }

            currentTile = fringe[lowestMove];
            fringe.RemoveAt(lowestMove);
            currentTile.inFringe = false;
        }
    }