Пример #1
0
        public void Render(IScreenBuffer screen, PlayerInfo player)
        {
            screen.Clear();

            // The screen has an origin in the top left.  Positive Y is DOWN
            // Maps have an origin in the bottom left.  Positive Y is UP

            var screenDimensionsV      = screen.Dimensions.ToVector2();
            var desiredMapScreenBounds = screenDimensionsV * DefaultMapToScreenRatio;

            var gameToScreenFactor = Math.Min(desiredMapScreenBounds.X / _map.Area.X, desiredMapScreenBounds.Y / _map.Area.Y);

            var screenAreaInMapCoords = screenDimensionsV / gameToScreenFactor;
            var mapCenteringOffset    = (screenAreaInMapCoords - _map.Area) / 2 - _map.BottomLeftCorner;

            // Transform all vertices
            for (int v = 0; v < _map.Vertices.Length; v++)
            {
                _verticesInScreenCoords[v] = ToScreenCoords(_map.Vertices[v]);
            }

            Point ToScreenCoords(Vector2 worldCoordinate)
            {
                var shiftedWorldCoordinate = worldCoordinate + mapCenteringOffset;

                // This fixes jittering
                var pixelOffset = new Vector2(0.5f, 0.5f);

                return(((shiftedWorldCoordinate * gameToScreenFactor) + pixelOffset).ToPoint().InvertY(screen.Height));
            }

            void DrawLineFromVertices(int v1, int v2, Color c) =>
            DrawLineFromScreenCoordinates(_verticesInScreenCoords[v1], _verticesInScreenCoords[v2], c);

            void DrawLineFromWorldCoordinates(Vector2 wc1, Vector2 wc2, Color c)
            {
                var sc1 = ToScreenCoords(wc1);
                var sc2 = ToScreenCoords(wc2);

                DrawLineFromScreenCoordinates(sc1, sc2, c);
            }

            void DrawLineFromScreenCoordinates(Point sc1, Point sc2, Color c)
            {
                var result = LineClipping.ClipToScreen(screen, sc1, sc2);

                if (result.shouldDraw)
                {
                    screen.PlotLineSmooth(result.p0, result.p1, c);
                }
            }

            foreach (var lineDef in _map.Map.LineDefs.Take((int)_linesToDraw))
            {
                ref Vector2 vertex1 = ref _map.Vertices[lineDef.V1];
                ref Vector2 vertex2 = ref _map.Vertices[lineDef.V2];