예제 #1
0
    void MakeConnection(OCT_Tile tile, Flags direction, out OCT_Tile otherTile)
    {
        tile.flags |= direction;
        int x = tile.x + (int)direction.Dir()[0].x;
        int y = tile.y + (int)direction.Dir()[0].y;

        otherTile          = tiles[x, y];
        tiles[x, y].flags |= direction.GetOpposites();
    }
예제 #2
0
 void RandomizeTile(OCT_Tile tile)
 {
     if (!tile.turnable)
     {
         return;
     }
     for (int i = 0; i < UnityEngine.Random.Range(0, 8); i++)
     {
         tile.flags = tile.flags.RotateCW();
     }
 }
예제 #3
0
 void Flood(OCT_Tile tile)
 {
     searchedTiles.Add(tile);
     foreach (var item in GetNeighbors(tile))
     {
         if (!searchedTiles.Contains(item))
         {
             Flood(item);
         }
     }
 }
예제 #4
0
    OCT_Tile GetTileInDirection(OCT_Tile tile, Flags direction)
    {
        int x = tile.x + (int)direction.Dir()[0].x;
        int y = tile.y + (int)direction.Dir()[0].y;

        if (x >= 0 && x < sizeX && y >= 0 && y < sizeY)
        {
            return(tiles[x, y]);
        }
        else
        {
            return(null);
        }
    }
예제 #5
0
    void MakeConnection(OCT_Tile tile, OCT_Tile tile2)
    {
        OCT_Tile nullTile;

        if (Vector3.Distance(tile.transform.position, tile2.transform.position) > 1.9f)
        {
            Debug.DrawLine(tile.transform.position, tile2.transform.position);
            throw new Exception("Can't connect Tiles not adjacent");
        }

        var direction = (tile2.transform.position - tile.transform.position).FromDirection();

        MakeConnection(tile, direction, out nullTile);
    }
예제 #6
0
    List <OCT_Tile> GetAdjacent(OCT_Tile tile)
    {
        List <OCT_Tile> adjacent = new List <OCT_Tile>();

        foreach (Flags val in Enum.GetValues(typeof(Flags)))
        {
            int x = tile.x + (int)val.Dir()[0].x;
            int y = tile.y + (int)val.Dir()[0].y;
            if (
                x >= 0 && x < sizeX &&
                y >= 0 && y < sizeY
                )
            {
                adjacent.Add(tiles[x, y]);
            }
        }
        return(adjacent);
    }
예제 #7
0
    List <OCT_Tile> GetNeighbors(OCT_Tile tile)
    {
        List <OCT_Tile> neighbors = new List <OCT_Tile>();

        foreach (Flags val in Enum.GetValues(typeof(Flags)))
        {
            if (tile.flags.HasFlag(val))
            {
                int x = tile.x + (int)val.Dir()[0].x;
                int y = tile.y + (int)val.Dir()[0].y;
                if (
                    x >= 0 && x < sizeX &&
                    y >= 0 && y < sizeY &&
                    tiles[x, y].flags.HasFlag(val.GetOpposites())
                    )
                {
                    neighbors.Add(tiles[x, y]);
                }
            }
        }
        return(neighbors);
    }
예제 #8
0
    IEnumerator MakePath(OCT_Tile fromTile)
    {
        List <OCT_Tile> tilesToCheck = new List <OCT_Tile>();

        checkedTiles.Clear();
        int iterator = 0;
        int curI     = 0;

        foreach (var item in tiles)
        {
            tilesToCheck.Add(item);
            if (fromTile == item)
            {
                iterator = curI;
            }
            curI++;
        }

        var tile     = tilesToCheck[iterator];
        int failSafe = 0;

        while (tilesToCheck.Count > 0 && failSafe < 1000)
        {
            yield return(null);

            var f         = (Flags)(1 << UnityEngine.Random.Range(0, 8));
            var otherTile = GetTileInDirection(tile, f);

            bool b = false;

            for (int i = 0; i < 7; i++)
            {
                f         = f.RotateCW();
                otherTile = GetTileInDirection(tile, f);
                if (otherTile && tilesToCheck.Contains(otherTile))
                {
                    MakeConnection(tile, otherTile);
                    tilesToCheck.Remove(otherTile);
                    checkedTiles.Add(otherTile);

                    tile = otherTile;

                    b = true;
                    break;
                }
            }
            if (b)
            {
                continue;
            }

            yield return(null);

            var newTile = tilesToCheck[UnityEngine.Random.Range(0, tilesToCheck.Count - 1)];

            // TODO: This is messy....
            int failsafe = 0;
            while (!GetAdjacent(newTile).Contains(tile) && failsafe < 1000)
            {
                //UnityEngine.Random.InitState(randSeed);
                newTile = tilesToCheck[UnityEngine.Random.Range(0, tilesToCheck.Count - 1)];
                foreach (var item in GetAdjacent(newTile))
                {
                    if (checkedTiles.Contains(item))
                    {
                        tile = item;
                    }
                }
                Debug.DrawLine(newTile.transform.position, tile.transform.position);
                //randSeed++;
                failsafe++;
            }
            MakeConnection(tile, newTile);

            tilesToCheck.Remove(newTile);
            checkedTiles.Add(newTile);

            tile = newTile;



            failSafe++;
            yield return(null);
        }
        Debug.Log("failsafe: " + failSafe);
    }
예제 #9
0
    void MakeConnection(OCT_Tile tile, Flags direction)
    {
        OCT_Tile nullTile;

        MakeConnection(tile, direction, out nullTile);
    }