Esempio n. 1
0
        public static List <Vector2> GetPath(this SceneViewModel scene, Vector2 startPoint, Vector2 endPoint)
        {
            var points = new List <Vector2>();

            try
            {
                var mapBuilder = new MapBuilder();

                foreach (var component in scene.Components.Where(c => c.CollisionDetectionEnabled).ToList())
                {
                    mapBuilder.AddObstacle(component);
                }

                mapBuilder.SetStart(startPoint);
                mapBuilder.SetEnd(endPoint);
                mapBuilder.SetDimensions(scene.Size);

                var mapResult = mapBuilder.Build();
                if (mapResult.Success && mapResult.Map != null)
                {
                    var pathFinder =
                        new PathFinder.Algorithm.PathFinder(mapResult.Map,
                                                            DefaultNeighbourFinder.Straight(0.5f));

                    points.AddRange(pathFinder.FindPath());
                }
                else
                {
                    Console.WriteLine(mapResult.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(points);
        }
        public static List <Vector2> GetPath(this DesignerViewModel designer, Vector2 startPoint, Vector2 endPoint,
                                             INeighbourFinder neighbourFinder = null)
        {
            var points = new List <Vector2>();

            try
            {
                var mapBuilder = new MapBuilder();

                foreach (var item in designer.ConnectedComponents.ToList())
                {
                    mapBuilder.AddObstacle(item.Position - Vector2.One, item.Size + (Vector2.One * 2));
                }

                mapBuilder.SetStart(startPoint);
                mapBuilder.SetEnd(endPoint);
                mapBuilder.SetDimensions(designer.Width, designer.Height);
                var mapResult = mapBuilder.Build();
                if (!mapResult.Success)
                {
                    throw new Exception(mapResult.Message);
                }

                var pathFinder =
                    new PathFinder.Algorithm.PathFinder(mapResult.Map,
                                                        neighbourFinder ?? DefaultNeighbourFinder.Straight(0.5f));

                points.AddRange(pathFinder.FindPath());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(points);
        }