Пример #1
0
        /// <summary>
        /// Finds a path between two points.
        /// </summary>
        /// <param name="start">The start point.</param>
        /// <param name="goal">The goal point.</param>
        /// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the original collision map.</param>
        /// <returns></returns>
        private List <AStar.PathFinderNode> FindPath(Point start, Point goal, List <Point> appendCollisionMap)
        {
            byte[,] mapCollisionMap = Core.Map.GetCollisionMap();

            // Add solid collision component entities
            foreach (var collision in EntityManager.GetComponents <Collision>())
            {
                if (collision.Solid && collision.Parent != this.Parent)
                {
                    var pos = Grid.WorldToGrid(new Vector2(collision.Rectangle.X, collision.Rectangle.Y));

                    for (int x = 0; x < Math.Ceiling(collision.Rectangle.Width / (double)Grid.TileSize); x++)
                    {
                        for (int y = 0; y < Math.Ceiling(collision.Rectangle.Height / (double)Grid.TileSize); y++)
                        {
                            try
                            {
                                mapCollisionMap[pos.X + x, pos.Y + y] = 0;
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }

            if (appendCollisionMap != null)
            {
                foreach (var point in appendCollisionMap)
                {
                    mapCollisionMap[point.X, point.Y] = 0;
                }
            }

            pathFinder                       = new AStar.PathFinderFast(mapCollisionMap);
            pathFinder.Formula               = AStar.HeuristicFormula.Manhattan;
            pathFinder.Diagonals             = false;
            pathFinder.HeavyDiagonals        = false;
            pathFinder.HeuristicEstimate     = 2;
            pathFinder.PunishChangeDirection = false;
            pathFinder.TieBreaker            = false;
            pathFinder.SearchLimit           = 50000;
            pathFinder.DebugProgress         = false;
            pathFinder.DebugFoundPath        = false;

            var _start = new System.Drawing.Point(start.X, start.Y);
            var _goal  = new System.Drawing.Point(goal.X, goal.Y);

            //path = Core.PathFinder.FindPath(_start, _goal);
            path = pathFinder.FindPath(_start, _goal);
            if (path == null)
            {
                Debug.WriteLine(this.Parent.ToString() + ": Path not found!");
            }

            //this.CollisionMap = new byte[100, 100];

            return(path);
        }
Пример #2
0
        /// <summary>
        /// Finds a path between two points.
        /// </summary>
        /// <param name="start">The start point.</param>
        /// <param name="goal">The goal point.</param>
        /// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the original collision map.</param>
        /// <returns></returns>
        private List<AStar.PathFinderNode> FindPath(Point start, Point goal, List<Point> appendCollisionMap)
        {
            byte[,] mapCollisionMap = Core.Map.GetCollisionMap();

            // Add solid collision component entities
            foreach (var collision in EntityManager.GetComponents<Collision>())
            {
                if (collision.Solid && collision.Parent != this.Parent)
                {
                    var pos = Grid.WorldToGrid(new Vector2(collision.Rectangle.X, collision.Rectangle.Y));

                    for (int x = 0; x < Math.Ceiling(collision.Rectangle.Width / (double)Grid.TileSize); x++)
                        for (int y = 0; y < Math.Ceiling(collision.Rectangle.Height / (double)Grid.TileSize); y++)
                        {
                            try
                            {
                                mapCollisionMap[pos.X + x, pos.Y + y] = 0;
                            }
                            catch
                            {
                            }
                        }
                }
            }

            if (appendCollisionMap != null)
                foreach (var point in appendCollisionMap)
                    mapCollisionMap[point.X, point.Y] = 0;

            pathFinder = new AStar.PathFinderFast(mapCollisionMap);
            pathFinder.Formula = AStar.HeuristicFormula.Manhattan;
            pathFinder.Diagonals = false;
            pathFinder.HeavyDiagonals = false;
            pathFinder.HeuristicEstimate = 2;
            pathFinder.PunishChangeDirection = false;
            pathFinder.TieBreaker = false;
            pathFinder.SearchLimit = 50000;
            pathFinder.DebugProgress = false;
            pathFinder.DebugFoundPath = false;

            var _start = new System.Drawing.Point(start.X, start.Y);
            var _goal = new System.Drawing.Point(goal.X, goal.Y);
            //path = Core.PathFinder.FindPath(_start, _goal);
            path = pathFinder.FindPath(_start, _goal);
            if (path == null)
                Debug.WriteLine(this.Parent.ToString() + ": Path not found!");

            //this.CollisionMap = new byte[100, 100];

            return path;
        }