Exemple #1
0
 /// <summary>
 /// Loads a tile associated with a specific string id used when loading a tilemap.
 /// The tile is stored in a list of tile types used to create tiles for a tilemap.
 /// </summary>
 /// <param name="id">The unique string id associated with this tile.</param>
 /// <param name="tile">The tile object to store in the list of tile types.</param>
 /// <returns>The tile object that was loaded.</returns>
 public GenTile LoadTile(string id, GenTile tile)
 {
     return TileTypes[id] = tile;
 }
Exemple #2
0
        /// <summary>
        /// Adds a tile to the tilemap at the specified position.
        /// </summary>
        /// <param name="x">The column position of the tile, starting from 0.</param>
        /// <param name="y">The row position of the tile, starting from 0.</param>
        /// <param name="replace">A flag used to determine if any existing tile will be replaced.</param>
        /// <returns>The tile that was added to the tilemap. Null if no tile was added.</returns>
        public GenTile AddTile(int x, int y, bool replace = false)
        {
            if ((x < 0) || (y < 0))
                return null;

            if ((x >= Tiles[0].Length) || (y >= Tiles.Length))
                return null;

            if ((Tiles[y][x] != null) && !replace)
                return null;

            Tiles[y][x] = new GenTile(x * _tileWidth, y * _tileHeight, _tileWidth, _tileHeight);

            if (TileSheetTexture != null)
            {
                Tiles[y][x].LoadTexture(TileSheetTexture, false);
                GetAutoTile(x, y);
            }

            // Check for a tile to the left of the current tile, and flag internal edges as closed.
            if (x > 0)
            {
                if (CloseTileEdge(x - 1, y, GenObject.Direction.Right))
                    CloseTileEdge(x, y, GenObject.Direction.Left);
            }

            // Check for a tile to the right of the current tile, and flag internal edges as closed.
            if (x < _columns - 1)
            {
                if (CloseTileEdge(x + 1, y, GenObject.Direction.Left))
                    CloseTileEdge(x, y, GenObject.Direction.Right);
            }

            // Check for a tile above the current tile, and flag internal edges as closed.
            if (y > 0)
            {
                if (CloseTileEdge(x, y - 1, GenObject.Direction.Down))
                    CloseTileEdge(x, y, GenObject.Direction.Up);
            }

            // Check for a tile below the current tile, and flag internal edges as closed.
            if (y < _rows - 1)
            {
                if (CloseTileEdge(x, y + 1, GenObject.Direction.Up))
                    CloseTileEdge(x, y, GenObject.Direction.Down);
            }

            return Tiles[y][x];
        }
Exemple #3
0
        /// <summary>
        /// Loads a CSV tilemap containing comma separated string values, each associated with a predefined tile used by the LoadTile method.
        /// </summary>
        /// <param name="mapData">The CSV (Comma Separated Values) tilemap to load.</param>
        /// <param name="tileWidth">The width of each tile.</param>
        /// <param name="tileHeight">The height of each tile.</param>
        /// <param name="tileSheet">The tilesheet texture used for automatic tilemap texturing. A value of null will not use a tilesheet texture.</param>
        public void LoadMap(string mapData, int tileWidth, int tileHeight, Texture2D tileSheet = null)
        {
            TileWidth = tileWidth;
            TileHeight = tileHeight;

            string[] rows = mapData.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            string[] row;

            int width = rows[0].Split(new char[] { ',' }).Length;

            // Create the rows.
            Tiles = new GenTile[rows.Length][];

            // Create the columns.
            for (int i = 0; i < Tiles.Length; i++)
                Tiles[i] = new GenTile[width];

            // Get the number of rows and columns of the tilemap.
            _rows = Tiles.Length;
            _columns = Tiles[0].Length;

            int x;
            int y;

            for (y = 0; y < rows.Length; y++)
            {
                row = rows[y].Split(new char[] { ',' });

                if (row.Length != width)
                    throw new Exception(String.Format("The length of row {0} is different from all preceeding rows.", row.Length));

                for (x = 0; x < width; x++)
                {
                    if (row[x] != "0")
                    {
                        Tiles[y][x] = new GenTile(x * _tileWidth, y * _tileHeight, _tileWidth, _tileHeight);

                        if (tileSheet == null)
                            Tiles[y][x].LoadTexture(TileTypes[row[x]].Texture);

                        // Check for a tile to the left of the current tile, and flag internal edges as closed.
                        if ((x > 0) && (Tiles[y][x - 1] != null))
                        {

                            Tiles[y][x].OpenEdges &= ~GenObject.Direction.Left;
                            Tiles[y][x - 1].OpenEdges &= ~GenObject.Direction.Right;
                        }

                        // Check for a tile on top of the current tile, and flag internal edges as closed.
                        if ((y > 0) && (Tiles[y - 1][x] != null))
                        {
                            Tiles[y][x].OpenEdges &= ~GenObject.Direction.Up;
                            Tiles[y - 1][x].OpenEdges &= ~GenObject.Direction.Down;
                        }
                    }
                    else
                        Tiles[y][x] = null;
                }
            }

            // Use a tile sheet for the tile images if one is provided.
            if (tileSheet != null)
            {
                LoadTileSheet(tileSheet);
            }
        }
Exemple #4
0
        /// <summary>
        /// Generates a cave using cellular automaton, given the specified settings.
        /// </summary>
        /// <param name="width">The number of cells along the x-axis, a value evenly divisible by the cell size.</param>
        /// <param name="height">The number of cells along the y-axis, a value evenly divisible by the cell size.</param>
        /// <param name="startPercentage">The initial percentage of alive cells.</param>
        /// <param name="tileWidth">The width of the tile in each cell.</param>
        /// <param name="tileHeight">The height of the tile in each cell.</param>
        /// <param name="cellSize">The initial number of individual cells, horizontally and vertically, that make one cell.</param>
        /// <param name="born">An array of numbers of neighboring cells needed for a cell to be born. Null will use an array of default values.</param>
        /// <param name="survive">An array of numbers of neighboring cells needed for a cell to survive. Null will use an array of default values.</param>
        public void MakeCave(int width, int height, int startPercentage = 45, int tileWidth = 8, int tileHeight = 8, int cellSize = 2, int[] born = null, int[] survive = null)
        {
            _width = width;
            _height = height;
            _cellSize = cellSize;

            if (born == null)
                born = new int[] { 6, 7, 8 };

            if (survive == null)
                survive = new int[] { 3, 4, 5, 6, 7, 8 };

            _cells = new int[width, height];

            for (int y = 0; y < height; y += cellSize)
            {
                for (int x = 0; x < width; x += cellSize)
                {
                    bool alive = false;

                    if ((x == 0) || (x == (_width - cellSize)) || (y == 0) || (y == (_height - cellSize)))
                        alive = true;
                    else
                        alive = (GenU.Random(0, 101) <= startPercentage) ? true : false;

                    for (int i = 0; i < cellSize; i++)
                    {
                        for (int j = 0; j < cellSize; j++)
                            _cells[x + j, y + i] = alive ? 1 : 0;
                    }
                }
            }

            ApplyAutomaton(2, born, survive, false);

            // Apply smoothing automatons.
            while (_cellSize >= 1)
                ApplyAutomaton(1, new int[] { 5, 6, 7, 8 }, new int[] { 5, 6, 7, 8 }, true);

            TileWidth = tileWidth;
            TileHeight = tileHeight;

            // Create the rows.
            Tiles = new GenTile[height][];

            // Create the columns.
            for (int i = 0; i < Tiles.Length; i++)
                Tiles[i] = new GenTile[width];

            // Get the number of rows and columns of the tilemap.
            _rows = height;
            _columns = width;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (_cells[x, y] != 0)
                    {
                        Tiles[y][x] = new GenTile(x * _tileWidth, y * _tileHeight, _tileWidth, _tileHeight);

                        // Check for a tile to the left of the current tile, and flag internal edges as closed.
                        if ((x > 0) && (Tiles[y][x - 1] != null))
                        {
                            Tiles[y][x].OpenEdges &= ~GenObject.Direction.Left;
                            Tiles[y][x - 1].OpenEdges &= ~GenObject.Direction.Right;
                        }

                        // Check for a tile on top of the current tile, and flag internal edges as closed.
                        if ((y > 0) && (Tiles[y - 1][x] != null))
                        {
                            Tiles[y][x].OpenEdges &= ~GenObject.Direction.Up;
                            Tiles[y - 1][x].OpenEdges &= ~GenObject.Direction.Down;
                        }
                    }
                    else
                        Tiles[y][x] = null;
                }
            }

            LoadTileSheet(ImageAuto);
        }