예제 #1
0
        public List<Vector2> FoundCellWay(Point startCell, Point endCell)
        {
            pointMap = new byte[StrategyManager.GameField.Width, StrategyManager.GameField.Height];
            OpenList = new BinaryHeap();

            WayPoint startPoint = new WayPoint(startCell, null, true);
            startPoint.CalculateCost(Creature, endCell, ImprovedPathFinding);

            OpenList.Add(startPoint);

            while (OpenList.Count != 0)
            {
                WayPoint node = OpenList.Get();
                if (node.PositionX == endCell.X && node.PositionY == endCell.Y)
                {
                    WayPoint nodeCurrent = node;
                    List<Vector2> points = new List<Vector2>();

                    while (nodeCurrent != null)
                    {
                        points.Insert(0, new Vector2(nodeCurrent.PositionX, nodeCurrent.PositionY));
                        nodeCurrent = nodeCurrent.Parent;
                    }
                    return points;
                }

                OpenList.Remove();
                Point temp = new Point(node.PositionX, node.PositionY);
                if (DiagonalMovesAllowed)
                {
                    if (CheckPassability(temp.X - 1, temp.Y) && CheckPassability(temp.X, temp.Y - 1))
                        AddNode(node, -1, -1, false, endCell);
                    if (CheckPassability(temp.X + 1, temp.Y) && CheckPassability(temp.X, temp.Y - 1))
                        AddNode(node, 1, -1, false, endCell);
                    if (CheckPassability(temp.X - 1, temp.Y) && CheckPassability(temp.X, temp.Y + 1))
                        AddNode(node, -1, 1, false, endCell);
                    if (CheckPassability(temp.X + 1, temp.Y) && CheckPassability(temp.X, temp.Y + 1))
                        AddNode(node, 1, 1, false, endCell);
                }

                AddNode(node, -1, 0, true, endCell);
                AddNode(node, 0, -1, true, endCell);
                AddNode(node, 1, 0, true, endCell);
                AddNode(node, 0, 1, true, endCell);
            }
            return null;
        }
예제 #2
0
        private void AddNode(WayPoint node, sbyte offSetX, sbyte offSetY, bool type, Point endCell)
        {
            Point pos = new Point(node.PositionX + offSetX, node.PositionY + offSetY);

            if (!CheckPassability(pos.X, pos.Y))
                return;
            if(!StrategyManager.GameField.InRange(pos.X, pos.Y))
                return;
            if (pointMap[pos.X, pos.Y] != 1)
            {
                WayPoint temp = new WayPoint(pos, node, type);
                temp.CalculateCost(Creature, endCell, ImprovedPathFinding);
                OpenList.Add(temp);
                pointMap[pos.X, pos.Y] = 1;
            }
        }