Example #1
0
        public Map(string MapID)
        {
            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();


            Console.WriteLine("Initializing Map: {0}", MapID);
            this.MapID = MapID;

            var mapData = (JObject)(Loader.data.geometry)[MapID];
            var mapDef  = (JObject)(Loader.data.maps)[MapID];

            if (mapData != null)
            {
                var MinX = (int)(((dynamic)mapData["min_x"]) ?? -1000);
                var MinY = (int)(((dynamic)mapData["min_y"]) ?? -1000);

                var MaxX = (int)(((dynamic)mapData["max_x"]) ?? 1000);
                var MaxY = (int)(((dynamic)mapData["max_y"]) ?? 1000);

                if ((dynamic)mapDef["no_bounds"] == true)
                {
                    MinX = -1000;
                    MaxX = 1000;

                    MinY = -1000;
                    MaxY = 1000;
                }

                Width  = Math.Abs(MaxX) + Math.Abs(MinX);
                Height = Math.Abs(MaxY) + Math.Abs(MinY);

                OffsetX = Math.Abs(MinX);
                OffsetY = Math.Abs(MinY);


                List <Line> lines = new List <Line>();

                if (mapData.Properties().Where(p => p.Name == "x_lines").FirstOrDefault() != null)
                {
                    foreach (var xLine in mapData["x_lines"])
                    {
                        var line = new Line(new Point((int)xLine[0], (int)xLine[1]), new Point((int)xLine[0], (int)xLine[2]));

                        if (line.Points.Length > 1)
                        {
                            lines.Add(line);
                        }
                    }
                }

                if (mapData.Properties().Where(p => p.Name == "y_lines").FirstOrDefault() != null)
                {
                    foreach (var yLine in mapData["y_lines"])
                    {
                        var line = new Line(new Point((int)yLine[1], (int)yLine[0]), new Point((int)yLine[2], (int)yLine[0]));
                        if (line.Points.Length > 1)
                        {
                            lines.Add(line);
                        }
                    }
                }

                PointMap = new PointMap(Width, Height, OffsetX, OffsetY);

                int xBufferMin = 9;
                int xBufferMax = 9;
                int yBufferMin = 4;
                int yBufferMax = 8;

                foreach (var line in lines)
                {
                    PointMap.DrawWall(line, xBufferMin, xBufferMax, yBufferMin, yBufferMax);
                }

                var spawns = ((JObject)(Loader.data.maps))[MapID]["spawns"];

                List <Point> spawnPoints = new List <Point>();
                foreach (var spawn in spawns)
                {
                    var spawnPoint = new Point((int)spawn[0], (int)spawn[1]);
                    spawnPoints.Add(spawnPoint);
                }

                PointMap.FillInterior(spawnPoints);

                PointMap.FillExterior();

                var p1 = new Point(MinX, MinY);
                var p2 = new Point(MinX, MaxY);
                var p3 = new Point(MaxX, MaxY);
                var p4 = new Point(MaxX, MinY);

                PointMap.DrawWall(new Line(p1, p2), xBufferMin, xBufferMax, yBufferMin, yBufferMax);
                PointMap.DrawWall(new Line(p2, p3), xBufferMin, xBufferMax, yBufferMin, yBufferMax);
                PointMap.DrawWall(new Line(p3, p4), xBufferMin, xBufferMax, yBufferMin, yBufferMax);
                PointMap.DrawWall(new Line(p4, p1), xBufferMin, xBufferMax, yBufferMin, yBufferMax);

                System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();

                poly = PointMap.BuildPolygon();

                Mesh = BuildMesh();

                this.edges = PointMap.GetEdges();

                PointMap.FillMeshEdges(Mesh);

                graph = new MapGraph(Mesh, PointMap, OffsetX, OffsetY);

                GetMapConnections();

                stopwatch.Stop();

                Console.WriteLine("Initialized Map: {0} in {1}ms", MapID, stopwatch.ElapsedMilliseconds);
            }
            else
            {
                throw new Exception("Map Doesn't Exist.");
            }
        }