public MapGameObject(Map mapData) : base(false)
        {
            _mapData = mapData;

            _totalWidth  = _mapData.Width * _mapData.TileWidth;
            _totalHeight = _mapData.Height * _mapData.TileHeight;

            _bitMapReuse = new Dictionary <string, Bitmap>();

            var backGroundImages = _mapData.Groups.Where(g => g.name == "background image").SelectMany(g => g.ImageLayers).Where(g => !string.IsNullOrWhiteSpace(g.Image?.FileName)).GroupBy(g => g.Image?.FileName);

            _mapData.ImageLayers = backGroundImages.SelectMany(g => g).ToArray();

            foreach (var groupImage in backGroundImages)
            {
                var bitmap = new Bitmap(groupImage.Key);
                _bitMapReuse.Add(groupImage.Key, bitmap);
            }

            _bitMapReuse = new Dictionary <string, Bitmap>();

            //Load Object Group
            _objectGroups = _mapData.ObjectGroups;

            //Section to create the tiles Image and data to be drawn
            //The TilesSet Images are loaded and than used to draw each layer according with the layer data

            int totalTiles = mapData.TileSets.Sum(t => t.TileCount);

            _tileSetImages      = new Bitmap[_mapData.TileSets.Length];
            _tileSetSpritesData = new SpriteTileSetData[totalTiles];
            int tileCounter = 0;

            _tilesetIndexMap = new Dictionary <string, int>();

            for (int i = 0; i < _mapData.TileSets.Length; i++)
            {
                var watch = StopwatchTool.StartWatch();

                var tileSet = _mapData.TileSets[i];

                _tilesetIndexMap.Add(tileSet.Name, i);

                if (tileSet.TileCount > 0)
                {
                    int tileWidth  = tileSet.TileWidth;
                    int tileHeight = tileSet.TileHeight;
                    int margin     = tileSet.Margin;
                    int spacing    = tileSet.Spacing;
                    int rows       = tileSet.Rows;

                    var bm = new Bitmap("data/Clouds_Border_Tileset.png");
                    _tileSetImages[i] = bm;

                    for (int j = 0; j < tileSet.TileCount; j++)
                    {
                        int row    = j / tileSet.Columns;
                        int column = j % tileSet.Columns;

                        Rectangle cloneRect = new Rectangle(column * tileWidth + margin + spacing * (column),
                                                            row * tileHeight + margin + spacing * (row), tileWidth, tileHeight);

                        //Create a data the keeps the ID and the rectangle where the sprite is in tileset
                        _tileSetSpritesData[tileCounter] = new SpriteTileSetData(i, cloneRect);
                        tileCounter++;
                    }

                    Console.WriteLine("tileSet.TileCount: " + tileSet.TileCount + " | rows: " + tileSet.Rows +
                                      " | columns: " + tileSet.Columns);

                    StopwatchTool.StopWatch(watch, tileSet.Name + " loaded: ");
                }
            }

            var drawWatch = StopwatchTool.StartWatch();

            DrawBackgroundLayers();

            StopwatchTool.StopWatch(drawWatch, "DrawBackgroundLayer: ");

            if (_mapData.Layers != null)
            {
                //Set the the layers data in a multidimensional array
                _tileArrays = new short[_mapData.Layers.Length][, ];
                for (int i = 0; i < mapData.Layers.Length; i++)
                {
                    _tileArrays[i] = _mapData.Layers[i].GetTileArray();
                }

                drawWatch = StopwatchTool.StartWatch();

                DrawLayers();

                StopwatchTool.StopWatch(drawWatch, "DrawLayers: ");
            }
        }
Example #2
0
        //private const int SIZE = 1280;

        public MapGameObject(Map mapData) : base(false)
        {
            _mapData = mapData;

            _boundariesLayerIndex = GetLayerIndex("boundaries");
            _cloudsLayerIndex     = GetLayerIndex("clouds");

            //_totalPixelsWidth = mapData.Width * mapData.TileWidth;
            //_totalPixelsHeight = mapData.Height * mapData.TileHeight;


            //Divide in a full HD width squares
            //_gridWidth = Mathf.Ceiling(_totalPixelsWidth / (float) SIZE);
            //_gridHeight = Mathf.Ceiling(_totalPixelsHeight / (float) SIZE);

            //_tilesPerSquare = SIZE / 32;

            //Determine how much to draw based on screen width
            // _gridsToDraw = 2 + Mathf.Ceiling((float) Game.main.width / SIZE);
            // _gridsToDraw = (_gridsToDraw % 2 == 0) ? _gridsToDraw + 1 : _gridsToDraw;
            // _gridsToDrawStep = Mathf.Floor(_gridsToDraw * 0.5f);
            //
            // _bgCanvases = new Sprite[_gridsToDraw, _gridsToDraw];

            _bitMapReuse = new Dictionary <string, Bitmap>();

            var backGroundImages = _mapData.ImageLayers.GroupBy(g => g.Image.FileName);

            foreach (var groupImage in backGroundImages)
            {
                var bitmap = new Bitmap(groupImage.Key);
                _bitMapReuse.Add(groupImage.Key, bitmap);
            }

            // foreach (var kv in _bitMapReuse)
            // {
            //     _bgSpritesMap.Add(kv.Key, new Sprite(kv.Value, false));
            // }
            //
            // //var bitmap = new Bitmap(bgLayer.Image.FileName);
            //
            // for (int i = 0; i < _bgCanvases.GetLength(0); i++)
            // {
            //     for (int j = 0; j < _bgCanvases.GetLength(1); j++)
            //     {
            //         _bgCanvases[i, j] = _bgSpritesMap.Values.FirstOrDefault();
            //         AddChild(_bgCanvases[i, j]);
            //     }
            // }

            _bitMapReuse = new Dictionary <string, Bitmap>();

            //int canvasPixelsSum = 0;
            //int canvasCounter = 0;

            // for (int i = 0; i < _canvases.GetLength(0); i++)
            // {
            //     for (int j = 0; j < _canvases.GetLength(1); j++)
            //     {
            //         Canvas canvas;
            //
            //         try
            //         {
            //             canvas = new Canvas(SIZE, SIZE, false);
            //             canvasPixelsSum += SIZE * SIZE;
            //
            //             AddChild(canvas);
            //
            //             canvas.x = i * SIZE;
            //             canvas.y = j * SIZE;
            //
            //             _canvases[i, j] = canvas;
            //
            //             canvasCounter++;
            //         }
            //         catch (System.ArgumentException e)
            //         {
            //             Console.WriteLine($"{this}: pixelsSum: {canvasPixelsSum} | {canvasCounter} | {i:000},{j:000} | {int.MaxValue}");
            //             Console.WriteLine(e.Message);
            //             MyGame.ThisInstance.Close();
            //         }
            //     }
            // }

            //Load Object Group
            _objectGroup = _mapData.ObjectGroups[0];

            //Section to create the tiles Image and data to be drawn
            //The TilesSet Images are loaded and than used to draw each layer according with the layer data

            int totalTiles = mapData.TileSets.Sum(t => t.TileCount);

            _tileSetImages      = new Bitmap[_mapData.TileSets.Length];
            _tileSetSpritesData = new SpriteTileSetData[totalTiles];
            int tileCounter = 0;

            _tilesetIndexMap = new Dictionary <string, int>();

            for (int i = 0; i < _mapData.TileSets.Length; i++)
            {
                var watch = StopwatchTool.StartWatch();

                var tileSet = _mapData.TileSets[i];

                _tilesetIndexMap.Add(tileSet.Name, i);

                if (tileSet.TileCount > 0)
                {
                    int tileWidth  = tileSet.TileWidth;
                    int tileHeight = tileSet.TileHeight;
                    int margin     = tileSet.Margin;
                    int spacing    = tileSet.Spacing;
                    int rows       = tileSet.Rows;

                    var bm = new Bitmap(tileSet.Image.FileName);
                    _tileSetImages[i] = bm;

                    for (int j = 0; j < tileSet.TileCount; j++)
                    {
                        int row    = j / tileSet.Columns;
                        int column = j % tileSet.Columns;

                        Rectangle cloneRect = new Rectangle(column * tileWidth + margin + spacing * (column),
                                                            row * tileHeight + margin + spacing * (row), tileWidth, tileHeight);

                        //Create a data the keeps the ID and the rectangle where the sprite is in tileset
                        _tileSetSpritesData[tileCounter] = new SpriteTileSetData(i, cloneRect);
                        tileCounter++;
                    }

                    Console.WriteLine("tileSet.TileCount: " + tileSet.TileCount + " | rows: " + tileSet.Rows +
                                      " | columns: " + tileSet.Columns);

                    StopwatchTool.StopWatch(watch, tileSet.Name + " loaded: ");
                }
            }

            var drawWatch = StopwatchTool.StartWatch();

            DrawBackgroundLayers();

            StopwatchTool.StopWatch(drawWatch, "DrawBackgroundLayer: ");

            if (_mapData.Layers != null)
            {
                //Set the the layers data in a multidimensional array
                _tileArrays = new short[_mapData.Layers.Length][, ];
                for (int i = 0; i < mapData.Layers.Length; i++)
                {
                    _tileArrays[i] = _mapData.Layers[i].GetTileArray();
                }

                drawWatch = StopwatchTool.StartWatch();

                DrawLayers();

                StopwatchTool.StopWatch(drawWatch, "DrawLayers: ");
            }
        }