public void render(Astar astar, Graphics g) //【方法】路径着色及机器人编号标记 { if (Paths != null) { for (int i = PathIndex; i < Paths.Count; ++i) { g.FillRectangle(new SolidBrush(PathColor), new Rectangle(Paths[i].X, Paths[i].Y, astar.PixelFormat, astar.PixelFormat)); //路径着色 } } g.FillRectangle(new SolidBrush(ShowColor), new Rectangle(X, Y, astar.PixelFormat, astar.PixelFormat)); //当前位置(X,Y)着色 g.DrawString(ID.ToString(), new Font("楷体", 14, FontStyle.Bold), Brushes.Black, X, Y); }
public void update(Astar astar) //【方法】寻路及再次自更新 { if (Paths != null) //有路 { if (PathIndex < Paths.Count) //如果没走完。(PathIndex初始为0) { if (astar.checkAround(X, Y) <= 0) //周围无机器人(0),或者有编号小于自己的机器人(-1),则自己正常走if(astar.checkAround(X, Y) <= 0) { int tx = Paths[PathIndex].X; int ty = Paths[PathIndex].Y; if (astar.canWalkPixel(tx, ty)) //判断下一步点为可走 { astar.enableMapDataPixel(X, Y, -2); //走离当前点,当前点置为可用 X = tx; //走到下一点 Y = ty; astar.enableMapDataPixel(X, Y, ID); //新走到的点置为不可用.编号为自己的编号 PathIndex++; if (astar.canWalkPixel(TargetX, TargetY)) //只在终点可用时进行动态刷新,防止因重点不可用带来的崩溃。 { Paths = astar.findPathPixel(X, Y, TargetX, TargetY); //【动态规划】每一步都重新刷新轨迹。 PathIndex = 1; } } else //无可走点则刷新 { Paths = astar.findPathPixel(X, Y, TargetX, TargetY); PathIndex = 1; } } else //周围有编号比自己大的机器人,则暂停行动,且重新刷新路径 { Paths = astar.findPathPixel(X, Y, TargetX, TargetY); PathIndex = 1; } } else //列表计数指标到了(当前目标全走完了)则刷新 { astar.enableMapDataPixel(X, Y, -1); //原为true Paths = null; PathIndex = 0; } } else //已经停止行进(Paths==null) { astar.enableMapDataPixel(X, Y, -1); //机器人静止则赋予当前位置优先级最低编号,保证其他优先级低于当前位置的机器人能正常在周围移动 } }
public void delete(Astar astar) //【方法】删除机器人后使能当前点 { astar.enableMapDataPixel(X, Y, -2); }