Exemple #1
0
        protected void GoTowardsTarget()
        {
            if (target == null)
            {
                Wander();
                return;
            }
            int       xinverse = target.getLocation().Left > location.Left ? 1 : -1;
            int       yinverse = target.getLocation().Top > location.Top ? 1 : -1;
            int       dx, dy;
            Rectangle r;
            bool      good;

            do
            {
                dx   = rnd.Next(MoveDistance + 1);
                dy   = rnd.Next(MoveDistance - dx + 1);
                dx   = xinverse > 0 ? dx : -dx;
                dy   = yinverse > 0 ? dy : -dy;
                good = true /*MoveDistance >= Math.Sqrt(dx * dx + dy * dy)*/;
                r    = new Rectangle(location.Left + dx, location.Top + dy, DrawWidth, DrawWidth);
                good = (r.Left >= 0) && (r.Right <= DrawArea.Width);
                good = good & (r.Top >= 0) && (r.Bottom <= DrawArea.Height);

                LinkedListNode <Building> bn = Buildings.First;
                while (good && bn != null)
                {
                    if (r.IntersectsWith(bn.Value.getSurface()))
                    {
                        good = false;
                    }
                    bn = bn.Next;
                }
            } while (!good);

            location = r;
        }