Example #1
0
 /// <summary>
 /// This function sets a connection between this tile and another.
 /// </summary>
 /// <param name="dir">The connection direction</param>
 /// <param name="t">The tile to connect with</param>
 public void setConnection(TileConnections dir, Tile t)
 {
     connections[(int) dir] = t;
 }
Example #2
0
 public static TileConnections GetInvertedConnection(TileConnections con)
 {
     return((BuildTile.TileConnections)(((int)con + 2) % 4));
 }
Example #3
0
 /// <summary>
 /// This function returns a connection between this tile and another.
 /// </summary>
 /// <param name="dir">The connection direction</param>
 /// <returns>The tile object if the connection exists, otherwise null</returns>
 public Tile getConnection(TileConnections dir)
 {
     return connections[(int) dir];
 }
Example #4
0
        /// <summary>
        /// This function generates a tile recursively in the arena.
        /// </summary>
        /// <param name="parent">The parent tile (or null if it's the first tile)</param>
        /// <param name="dir">The direction from the parent (or null)</param>
        /// <param name="numConnections">The number of connections to try to create</param>
        /// <param name="maxLength">The maximum length of this path</param>
        /// <returns>The generated tile</returns>
        private Tile generateTile(Tile parent, TileConnections dir, int numConnections, int maxLength)
        {
            Tile tile;

            if (parent == null)
            {
                // This is the first tile in the arena
                int xPos = r.Next(EDGE_START_BUFFER, tiles.GetLength(1) - EDGE_START_BUFFER);
                int yPos = r.Next(EDGE_START_BUFFER, tiles.GetLength(0) - EDGE_START_BUFFER);

                tile = new Tile(parent, xPos, yPos);
                startTile = tile;
                tiles[yPos, xPos] = tile;
            }
            else
            {
                // This is an extension of the arena

                // Set the connection with the parent
                switch(dir)
                {
                    case TileConnections.LEFT:
                    {
                        tile = new Tile(parent, (int)parent.getPos().X - 1, (int)parent.getPos().Y);
                        tile.setConnection(TileConnections.RIGHT, parent);
                        tiles[(int)parent.getPos().Y, (int)parent.getPos().X - 1] = tile;

                        break;
                    }

                    case TileConnections.RIGHT:
                    {
                        tile = new Tile(parent, (int)parent.getPos().X + 1, (int)parent.getPos().Y);
                        tile.setConnection(TileConnections.LEFT, parent);
                        tiles[(int)parent.getPos().Y, (int)parent.getPos().X + 1] = tile;

                        break;
                    }

                    case TileConnections.TOP:
                    {
                        tile = new Tile(parent, (int)parent.getPos().X, (int)parent.getPos().Y - 1);
                        tile.setConnection(TileConnections.BOTTOM, parent);
                        tiles[(int)parent.getPos().Y - 1, (int)parent.getPos().X] = tile;

                        break;
                    }

                    case TileConnections.BOTTOM:
                    {
                        tile = new Tile(parent, (int)parent.getPos().X, (int)parent.getPos().Y + 1);
                        tile.setConnection(TileConnections.TOP, parent);
                            tiles[(int)parent.getPos().Y + 1, (int)parent.getPos().X] = tile;

                        break;
                    }

                    default:
                    {
                        // We won't get here
                        tile = new Tile(parent, (int)parent.getPos().X, (int)parent.getPos().Y);

                        break;
                    }
                }
            }

            // Determine the directions
            for (int i = 0; i < numConnections; i++)
            {
                TileConnections connection = TileConnections.NONE;

                // Find a free connection
                for (int j = 0; j < MAX_TRIES; j++)
                {
                    connection = (TileConnections)r.Next(4);

                    if (tile.getConnection(connection) == null)
                    {
                        int x = (int) tile.getPos().X;
                        int y = (int) tile.getPos().Y;

                        // Check the position in the array
                        if ((y + 1 < height && connection == TileConnections.BOTTOM && tiles[y + 1, x] == null)||
                            (y - 1 >= 0 && connection == TileConnections.TOP && tiles[y - 1, x] == null) ||
                            (x + 1 < width && connection == TileConnections.RIGHT && tiles[y, x + 1] == null) ||
                            (x - 1 >= 0 && connection == TileConnections.LEFT && tiles[y, x - 1] == null))
                        {
                            // We've found our next path
                            break;
                        }
                        else
                        {
                            connection = TileConnections.NONE;
                        }
                    }
                    else
                    {
                        connection = TileConnections.NONE;
                    }
                }

                // If we found the next connection
                if (connection != TileConnections.NONE)
                {
                    // Determine the next number of connections
                    int nextNumConnections = r.Next(1, 4);
                    if (maxLength == 0)
                    {
                        nextNumConnections = 0;
                    }

                    tile.setConnection(connection, generateTile(tile, connection, nextNumConnections, maxLength - 1));
                }
                else
                {
                    // We have no more connection opportunities
                    break;
                }
            }

            // Set the tile's art
            setArenaTile(tile);

            return tile;
        }