Beispiel #1
0
 public Human(ViewGraphic vg)
 {
     Graphic = vg;
     Graphic.SetBitmap(vg.ImagePath);
 }
        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.UserPaint |
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.ContainerControl |
                          ControlStyles.OptimizedDoubleBuffer |
                          ControlStyles.SupportsTransparentBackColor
                          , true);

            ViewGraphic vg = new ViewGraphic();

            vg.ImagePath = Environment.CurrentDirectory + @"\..\..\Human.png";
            vg.Size      = new PointF(32, 32);

            One          = new Human(vg);
            One.Position = new PointF(400, 300);
            One.Target   = new PointF(400, 300);

            Wall wl   = new Wall(new PointF(100, 100), new PointF(400, 100));
            Door door = new Door(new PointF(200, 100), new PointF(300, 100));

            List <Wall> walls = new List <Wall>();
            List <Door> doors = new List <Door>();

            walls.Add(wl);
            doors.Add(door);

            Task.Run(() => {
                var screenWidth  = pictureBox1.Width;
                var screenHeight = pictureBox1.Height;

                Stopwatch sw = new Stopwatch();
                sw.Start();
                while (true)
                {
                    Bitmap buffer = new Bitmap(screenWidth, screenHeight);
                    System.Drawing.Graphics gfx = Graphics.FromImage(buffer);//set the graphics to draw on the image

                    One.Graphic.MainScale = Scale;

                    One.Calculate(walls, doors);

                    if (wl.getDistance(One.NextPosition) < 10 && wl.getDistance(One.NextPosition) != door.getDistance(One.NextPosition))
                    {
                        var i            = wl.getDistance(One.NextPosition);
                        One.NextPosition = One.Position;
                    }

                    One.Draw(gfx);
                    wl.Draw(gfx);
                    door.Draw(gfx);

                    if (sw.ElapsedMilliseconds > 50)
                    {
                        WriteStats(); // Very slow
                        sw.Restart();
                    }
                    ChangeScreen(buffer);//set the PictureBox's image to be the buffer
                }
            });
        }