Exemplo n.º 1
0
        public void Draw()
        {
            try
            {
                Rectanglef logicViewRect;

                Vector2 upLeft    = BaseGame.CoordinMgr.LogicPos(new Vector2(screenViewRect.X, screenViewRect.Y));
                Vector2 upRight   = BaseGame.CoordinMgr.LogicPos(new Vector2(screenViewRect.X + screenViewRect.Width, screenViewRect.Y));
                Vector2 downLeft  = BaseGame.CoordinMgr.LogicPos(new Vector2(screenViewRect.X, screenViewRect.Y + screenViewRect.Height));
                Vector2 downRight = BaseGame.CoordinMgr.LogicPos(new Vector2(screenViewRect.X + screenViewRect.Width, screenViewRect.Y + screenViewRect.Height));

                logicViewRect = MathTools.BoundBox(upLeft, upRight, downLeft, downRight);

                int minX, minY, maxX, maxY;

                minX = Math.Max(0, (int)((logicViewRect.X - mapSize.X) / gridScale) - 1);
                minY = Math.Max(0, (int)((logicViewRect.Y - mapSize.Y) / gridScale) - 1);

                maxX = (int)((logicViewRect.X + logicViewRect.Width - mapSize.X) / gridScale) + 1;
                maxY = (int)((logicViewRect.Y + logicViewRect.Height - mapSize.Y) / gridScale) + 1;

                foreach (SpriteBatch batch in batches)
                {
                    batch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.None);
                }

                float gridWidthInScrn  = BaseGame.CoordinMgr.ScrnLengthf(gridScale);
                float gridHeightInScrn = BaseGame.CoordinMgr.ScrnLengthf(gridScale);

                Vector2 startPosInScrn = BaseGame.CoordinMgr.ScreenPos(new Vector2(minX * gridScale + mapSize.X, minY * gridScale + mapSize.Y));

                Vector2 UnitX = Vector2.Transform(new Vector2(gridWidthInScrn, 0), BaseGame.CoordinMgr.RotaMatrixFromLogicToScrn);
                Vector2 UnitY = Vector2.Transform(new Vector2(0, gridHeightInScrn), BaseGame.CoordinMgr.RotaMatrixFromLogicToScrn);

                float drawScale = gridWidthInScrn / sourceWidth;


                for (int y = minY; y <= maxY && y < gridHeightSum && y >= 0; y++)
                {
                    for (int x = minX; x <= maxX && x < gridWidthSum && x >= 0; x++)
                    {
                        Vector2 drawPos = startPosInScrn + (x - minX) * UnitX + (y - minY) * UnitY;

                        foreach (gridData data in gridTexData[x + gridWidthSum * y])
                        {
                            batches[data.texIndex].Draw(TexList[data.texIndex], drawPos, data.sourceRect, Color.White, -BaseGame.CoordinMgr.Rota, new Vector2(0, 0), drawScale, SpriteEffects.None, 1f);
                        }
                    }
                }

                foreach (SpriteBatch batch in batches)
                {
                    batch.End();
                }
            }
            catch (Exception)
            {
                CreateSpriteBatches();
            }
        }