Example #1
0
        /// <summary>
        /// Asynchronously loads the specified region.
        /// </summary>
        /// <param name="regionX">The X coordinate of the region.</param>
        /// <param name="regionY">The Y coordinate of the region.</param>
        /// <param name="callback">The callback to call once the region is loaded.</param>
        public override void LoadTiles(int regionX, int regionY, Action <FiniteGrid> callback)
        {
            if (gridSerializer.IsRegionSaved(regionX, regionY))
            {
                gridSerializer.LoadGrid(regionX, regionY, callback);
                return;
            }

            callback(null);
        }
Example #2
0
        /// <summary>
        /// Asynchronously returns the data for a region through the callback. The tiles are either loaded if loading is enabled or generated if loading
        /// is not used or that region has not yet been loaded.
        /// </summary>
        /// <param name="regionX">The X coordinate of the region.</param>
        /// <param name="regionY">The Y coordinate of the region.</param>
        /// <param name="callback">The callback to call once the region is loaded.</param>
        public override void LoadTiles(int regionX, int regionY, Action <FiniteGrid> callback)
        {
            if (useSave && gridSerializer.IsRegionSaved(regionX, regionY))
            {
                gridSerializer.LoadGrid(regionX, regionY, callback);
                return;
            }

            LargeRegion largeRegion = GetRegions(regionX, regionY);

            if (largeRegion == null)
            {
                int largeRegionX = Mathf.FloorToInt(regionX / (float)generationRegionWidth) * generationRegionWidth;
                int largeRegionY = Mathf.FloorToInt(regionY / (float)generationRegionHeight) * generationRegionHeight;

                largeRegion = new LargeRegion {
                    regionX      = largeRegionX,
                    regionY      = largeRegionY,
                    regionWidth  = generationRegionWidth,
                    regionHeight = generationRegionHeight,
                    tiles        = algorithm.GenerateTiles(
                        largeRegionX * Grid.REGION_SIZE,
                        largeRegionY * Grid.REGION_SIZE,
                        generationRegionWidth * Grid.REGION_SIZE,
                        generationRegionHeight * Grid.REGION_SIZE
                        )
                };

                largeRegions.Add(largeRegion);
                if (largeRegions.Count > 15)
                {
                    largeRegions.RemoveRange(0, 7);
                }
            }

            int xOffset = (regionX - largeRegion.regionX) * Grid.REGION_SIZE;
            int yOffset = (regionY - largeRegion.regionY) * Grid.REGION_SIZE;

            FiniteGrid region2 = new FiniteGrid(regionX, regionY, Grid.REGION_SIZE);

            bool empty = true;

            for (int i = 0; i < Grid.REGION_SIZE; i++)
            {
                for (int j = 0; j < Grid.REGION_SIZE; j++)
                {
                    Tile tile = largeRegion.tiles [xOffset + i, yOffset + j];

                    if (tile.id > 0)
                    {
                        empty = false;
                    }

                    region2.Set(i, j, tile);
                }
            }

            if (empty)
            {
                callback(null);
            }
            else
            {
                callback(region2);
            }
        }