예제 #1
0
        public List <PointF> FindPath(GameField field, PointF b)
        {
            PointF roundedPosition = new PointF(
                (float)Math.Round(WorldPosition.X),
                (float)Math.Round(WorldPosition.Y));
            PointF roundedTarget = new PointF(
                (float)Math.Round(b.X),
                (float)Math.Round(b.Y));

            int startCellNum  = field.cells.IndexOf(roundedPosition);
            int targetCellNum = field.cells.IndexOf(roundedTarget);

            if (startCellNum == -1 || targetCellNum == -1)
            {
                return(new List <PointF>());
            }

            // Queue<List<int>> open = new Queue<List<int>>();
            List <int> p = new List <int> {
                startCellNum
            };

            List <PathA> open = new List <PathA>();

            open.Add(new PathA(p, 0));

            bool[] visited   = new bool[field.cells.Count];
            bool   pathFound = false;

            while (open.Count != 0)
            {
                PathA current = open.Min();
                open.Remove(current);
                p = current.P;
                int v = current.P[p.Count - 1];

                if (visited[v])
                {
                    continue;
                }

                visited[v] = true;


                if (v == targetCellNum)
                {
                    pathFound = true;
                    break;
                }


                List <int> acident = field.GetAllAccidentCells(v);

                foreach (var nextV in acident)
                {
                    var        gCost    = field.GCost(v, nextV);
                    var        hCost    = field.HCost(nextV, targetCellNum);
                    int        f        = gCost + current.Cost + hCost;
                    List <int> nextPath = new List <int>(p);
                    nextPath.Add(nextV);
                    open.Add(new PathA(nextPath, f));
                }
            }

            if (!pathFound)
            {
                return(new List <PointF> {
                    field.cells[field.cells.Count - 1]
                });
            }
            foreach (var node in p)
            {
                path.Add(field.cells[node]);
            }
            path.RemoveRange(0, 1);
            return(path);
        }
예제 #2
0
 public MegaBoy(GameField field, double health = 100) : base(field, health)
 {
     Sprite = Form1.MyImageList.Images["MegaBoy.png"];
     Reward = Configs.MegaBoyReward;
     scale  = 2;
 }