Example #1
0
        public void LoadTiles()
        {
            if (_mapSectionData.Sprites == null || _mapSectionData.Sprites.Length == 0)
            {
                _logger.LogError(LoggedFeature.Map, "Map Data has no sprites");
                return;
            }

            uint y         = 0;
            int  i         = 0;
            uint yIndex    = 0;
            uint numTilesX = _mapSectionData.GridData.NumTilesX;
            uint numTilesY = _mapSectionData.GridData.NumTilesY;

            Sprite firstSprite = _mapSectionData.Sprites[0];

            // Calculate how many units in X this unit generates
            // For now, we assume that all sprites are the same size.
            uint numTilesInSpriteX = (uint)Mathf.CeilToInt(firstSprite.bounds.size.x / _mapSectionData.PixelsPerUnit);
            uint numTilesInSpriteY = (uint)Mathf.CeilToInt(firstSprite.bounds.size.y / _mapSectionData.PixelsPerUnit);

            uint numSpritesX = (uint)Mathf.CeilToInt(numTilesX / (float)numTilesInSpriteX);
            uint numSpritesY = (uint)Mathf.CeilToInt(numTilesY / (float)numTilesInSpriteY);

            while (i < _mapSectionData.Sprites.Length && y < numTilesY)
            {
                uint x      = 0;
                uint xIndex = 0;
                uint miny   = UInt32.MaxValue;
                while (i < _mapSectionData.Sprites.Length && x < numTilesX)
                {
                    uint   index  = (numSpritesY - 1 - yIndex) * numSpritesX + xIndex;
                    Sprite sprite = _mapSectionData.Sprites[index];
                    TileRendererBehaviour tileRendererBehaviour = _tileRendererPool.Spawn(sprite);
                    tileRendererBehaviour.transform.position =
                        _positionCalculator.GetTileOriginWorldPosition(IntVector2.Of(x, y)) +
                        new Vector2(sprite.bounds.extents.x, sprite.bounds.extents.y);

                    x += numTilesInSpriteX;
                    xIndex++;
                    miny = System.Math.Min(numTilesInSpriteY, miny);
                    i++;
                }

                y += miny;
                yIndex++;
            }
        }
        public void LoadTiles()
        {
            if (_mapSectionData.Sprites == null || _mapSectionData.Sprites.Length == 0)
            {
                _logger.LogError(LoggedFeature.Map, "Map Data has no sprites");
                return;
            }

            uint x = 0;
            uint y = 0;

            while (y < _mapSectionData.GridData.NumTilesY)
            {
                x = 0;
                uint miny = UInt32.MaxValue;
                while (x < _mapSectionData.GridData.NumTilesX)
                {
                    Sprite sprite =
                        _mapSectionData.Sprites[_randomProvider.GetRandomIntegerInRange(0, _mapSectionData.Sprites.Length)];
                    TileRendererBehaviour tileRendererBehaviour = _tileRendererPool.Spawn(sprite);
                    tileRendererBehaviour.SpriteRenderer.flipX = _randomProvider.GetRandomIntegerInRange(0, 2) == 0;
                    tileRendererBehaviour.SpriteRenderer.flipY = _randomProvider.GetRandomIntegerInRange(0, 2) == 0;
                    tileRendererBehaviour.transform.position   =
                        _positionCalculator.GetTileOriginWorldPosition(IntVector2.Of(x, y)) +
                        new Vector2(sprite.bounds.extents.x, sprite.bounds.extents.y);


                    // Calculate how many units in X this unit generates (assume square for now).
                    uint numXTiles = (uint)Mathf.CeilToInt(sprite.bounds.size.x / _mapSectionData.PixelsPerUnit);
                    uint numYTiles = (uint)Mathf.CeilToInt(sprite.bounds.size.y / _mapSectionData.PixelsPerUnit);
                    x   += numXTiles;
                    miny = System.Math.Min(numYTiles, miny);
                }

                y += miny;
            }
        }