Example #1
0
    /// <summary>
    /// Finds tiles adjacent to the position specified.
    /// </summary>
    /// <param name="position"></param>
    /// <returns>A list of GameTiles which are adjacent(cardinal) to the position</returns>
    public List <GameTile> GetTilesAdjacentToPosition(Vector3 position)
    {
        GameTile ti = GetTileAtPosition(position);

        if (ti != null)
        {
            List <GameTile> adjacentTiles = ti.GetAdjacentTiles();
            // Debug.Log(adjacentTiles);
            return(adjacentTiles);
        }
        else
        {
            List <GameTile> adjacentTiles = new List <GameTile>();


            RaycastHit2D hit = Physics2D.Raycast(position, Vector2.up);
            if (hit.collider != null)
            {
                GameTile t = hit.collider.gameObject.GetComponent <GameTile>();
                adjacentTiles.Add(t);
            }
            hit = Physics2D.Raycast(position, Vector2.right);
            if (hit.collider != null)
            {
                GameTile t = hit.collider.gameObject.GetComponent <GameTile>();
                adjacentTiles.Add(t);
            }
            hit = Physics2D.Raycast(position, Vector2.down);
            if (hit.collider != null)
            {
                GameTile t = hit.collider.gameObject.GetComponent <GameTile>();
                adjacentTiles.Add(t);
            }
            hit = Physics2D.Raycast(position, Vector2.left);
            if (hit.collider != null)
            {
                GameTile t = hit.collider.gameObject.GetComponent <GameTile>();
                adjacentTiles.Add(t);
            }
            // Debug.Log(adjacentTiles);
            return(adjacentTiles);
        }
    }