Exemplo n.º 1
0
        public static void DisplayLoop()
        {
            Stopwatch sw = Stopwatch.StartNew();

            while (!_linkDisplay.Cancelled)
            {
                _linkDisplay.LoopsCount++;

                // Limitation à 20FPS
                long sleep = 50 - sw.ElapsedMilliseconds - 1;
                if (sleep > 0)
                {
                    Thread.Sleep((int)sleep);
                }

                sw.Restart();

                try
                {
                    Bitmap bmp;

                    if (AfficheTable)
                    {
                        bmp = new Bitmap(Properties.Resources.TablePlan);
                    }
                    else
                    {
                        bmp = new Bitmap(Properties.Resources.TablePlan.Width, Properties.Resources.TablePlan.Height);
                    }

                    {
                        Graphics g = Graphics.FromImage(bmp);
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.SetClip(Scale.RealToScreenRect(new RectangleF(0, 0, 3000, 2000)));
                        g.DrawEllipse(Pens.DimGray, Scale.RealToScreenRect(new RectangleF(1000, -500, 1000, 1000)));
                        g.ResetClip();

                        if (AfficheGraph || AfficheGraphArretes)
                        {
                            DessineGraph(Robots.MainRobot, g, AfficheGraph, AfficheGraphArretes);
                        }

                        if (AfficheHistoriqueCoordonnees && Robots.MainRobot.PositionsHistorical != null)
                        {
                            DessineHistoriqueTrajectoire(Robots.MainRobot, g);
                        }

                        if (AfficheObstacles)
                        {
                            DessineObstacles(g, Robots.MainRobot);
                        }

                        if (true)
                        {
                            DrawOpponents(g, GameBoard.ObstaclesOpponents);
                        }

                        if (AfficheElementsJeu)
                        {
                            DessineElementsJeu(g, GameBoard.Elements);
                        }

                        if (Robots.MainRobot != null)
                        {
                            DessineRobot(Robots.MainRobot, g);
                        }

                        DessinePathFinding(g);

                        DessinePositionEnnemis(g);

                        DessineDetections(g);

                        Robots.MainRobot.PositionTarget?.Paint(g, Color.Red, 5, Color.Red, Scale);

                        if (AfficheCoutsMouvements)
                        {
                            if (GameBoard.Strategy != null && GameBoard.Strategy.Movements != null)
                            {
                                GameBoard.Strategy.Movements.ForEach(mouv => mouv.DisplayCostFactor = GameBoard.Strategy.Movements.Min(m => m.GlobalCost));
                                GameBoard.Strategy.Movements.ForEach(mouv => mouv.Paint(g, Scale));
                            }
                        }

                        if ((modeCourant == MouseMode.PositionCentre || modeCourant == MouseMode.TeleportCentre) && positionDepart != null)
                        {
                            Point positionFin = positionCurseur;

                            Bitmap   bmpGrosRobot = new Bitmap(Scale.RealToScreenDistance(Robots.MainRobot.Diameter * 3), Scale.RealToScreenDistance(Robots.MainRobot.Diameter * 3));
                            Graphics gGros        = Graphics.FromImage(bmpGrosRobot);
                            gGros.Clear(Color.Transparent);

                            Direction traj = Maths.GetDirection(positionDepart, PositionCurseurTable);

                            Rectangle r = new Rectangle(bmpGrosRobot.Width / 2 - Scale.RealToScreenDistance(Robots.MainRobot.Width / 2),
                                                        bmpGrosRobot.Height / 2 - Scale.RealToScreenDistance(Robots.MainRobot.LenghtFront),
                                                        Scale.RealToScreenDistance(Robots.MainRobot.Width),
                                                        Scale.RealToScreenDistance(Robots.MainRobot.LenghtTotal));

                            gGros.FillRectangle(brushNoirTresTransparent, r);
                            gGros.DrawRectangle(GameBoard.MyColor == GameBoard.ColorLeftBlue ? penCouleurGauche : penCouleurDroite, r);
                            gGros.DrawLine(GameBoard.MyColor == GameBoard.ColorLeftBlue ? penCouleurGauche : penCouleurDroite, bmpGrosRobot.Width / 2, bmpGrosRobot.Height / 2, bmpGrosRobot.Width / 2, bmpGrosRobot.Height / 2 - Scale.RealToScreenDistance(Robots.MainRobot.LenghtFront));

                            Point pointOrigine = Scale.RealToScreenPosition(positionDepart);
                            g.DrawImage(RotateImage(bmpGrosRobot, 360 - traj.angle.InDegrees + 90), pointOrigine.X - bmpGrosRobot.Width / 2, pointOrigine.Y - bmpGrosRobot.Height / 2);

                            g.DrawLine(penBlancFleche, (Point)Scale.RealToScreenPosition(positionDepart), positionFin);
                        }

                        else if ((modeCourant == MouseMode.PositionFace || modeCourant == MouseMode.TeleportFace) && positionDepart != null)
                        {
                            Point positionFin = positionCurseur;

                            Bitmap   bmpGrosRobot = new Bitmap(Scale.RealToScreenDistance(Robots.MainRobot.Diameter * 3), Scale.RealToScreenDistance(Robots.MainRobot.Diameter * 3));
                            Graphics gGros        = Graphics.FromImage(bmpGrosRobot);
                            gGros.Clear(Color.Transparent);

                            Direction traj = Maths.GetDirection(positionDepart, PositionCurseurTable);

                            Point    pointOrigine = Scale.RealToScreenPosition(positionDepart);
                            Position departRecule = new Position(-traj.angle, pointOrigine);
                            departRecule.Move(Scale.RealToScreenDistance(-Robots.MainRobot.LenghtFront));

                            Rectangle r = new Rectangle(bmpGrosRobot.Width / 2 - Scale.RealToScreenDistance(Robots.MainRobot.Width / 2),
                                                        bmpGrosRobot.Height / 2 - Scale.RealToScreenDistance(Robots.MainRobot.LenghtFront),
                                                        Scale.RealToScreenDistance(Robots.MainRobot.Width),
                                                        Scale.RealToScreenDistance(Robots.MainRobot.LenghtTotal));

                            gGros.FillRectangle(brushNoirTresTransparent, r);
                            gGros.DrawRectangle(GameBoard.MyColor == GameBoard.ColorLeftBlue ? penCouleurGauche : penCouleurDroite, r);
                            gGros.DrawLine(GameBoard.MyColor == GameBoard.ColorLeftBlue ? penCouleurGauche : penCouleurDroite, bmpGrosRobot.Width / 2, bmpGrosRobot.Height / 2, bmpGrosRobot.Width / 2, bmpGrosRobot.Height / 2 - Scale.RealToScreenDistance(Robots.MainRobot.LenghtFront));

                            g.DrawImage(RotateImage(bmpGrosRobot, 360 - traj.angle.InDegrees + 90), (int)(departRecule.Coordinates.X) - bmpGrosRobot.Width / 2, (int)(departRecule.Coordinates.Y) - bmpGrosRobot.Height / 2);

                            g.DrawLine(penBlancFleche, (Point)Scale.RealToScreenPosition(positionDepart), positionFin);
                        }

                        if (Robots.MainRobot.TrajectoryRunning != null)
                        {
                            Trajectory traj = new Trajectory();
                            traj.AddPoint(Robots.MainRobot.Position.Coordinates);

                            for (int iPoint = 1; iPoint < Robots.MainRobot.TrajectoryRunning.Points.Count; iPoint++)
                            {
                                traj.AddPoint(Robots.MainRobot.TrajectoryRunning.Points[iPoint]);
                            }

                            traj.Paint(g, Scale);
                        }

                        // Trajectoire polaire

                        //if (modeCourant == Mode.TrajectoirePolaire)
                        {
                            if (trajectoirePolaireScreen != null)
                            {
                                foreach (Point p in trajectoirePolaireScreen)
                                {
                                    g.FillEllipse(Brushes.Red, p.X - 1, p.Y - 1, 2, 2);
                                }
                            }
                            if (pointsPolaireScreen != null)
                            {
                                foreach (Point p in pointsPolaireScreen)
                                {
                                    g.FillEllipse(Brushes.White, p.X - 3, p.Y - 3, 6, 6);
                                    g.DrawEllipse(Pens.Black, p.X - 3, p.Y - 3, 6, 6);
                                }
                            }
                        }

                        TableDessinee?.Invoke(bmp);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Erreur pendant le dessin de la table " + ex.Message);
                }
            }
        }