Пример #1
0
 public VelocityFunctionPredator(Predator current, Prey best, double clamp, double alphax, double betax, double alphay, double betay, Bitmap bmp, double tired)
 {
     this.current   = current;
     this.best      = best;
     this.clamp     = clamp;
     this.alphax    = alphax + huntBest;
     this.betax     = betax * fogetPast;
     this.alphay    = alphay + huntBest;
     this.betay     = betay * fogetPast;
     this.bmp       = bmp;
     this.maxheight = bmp.Height - 1;
     this.maxwidth  = bmp.Width - 1;
     this.tired     = tired;
 }
        public Prey newPrey()
        {
            //update Velocity
            current.Velocity.y += (clamp * alphay * (current.Posbest.y - current.CurrentPostion.y) + clamp * betay * (best.CurrentPostion.y - current.CurrentPostion.y)) * tired;
            current.Velocity.x += (clamp * alphax * (current.Posbest.x - current.CurrentPostion.x) + clamp * betax * (best.CurrentPostion.x - current.CurrentPostion.x)) * tired;
            // Add fear of predator
            int  newx      = ((int)current.Velocity.x + current.CurrentPostion.x);
            int  newy      = ((int)current.Velocity.y + current.CurrentPostion.y);
            bool changeDir = false;

            if (predatorConsider)
            {
                // we will look around the destimation point if there is predator
                //alter x or y velocity
                foreach (Predator p in predatorsBeforeMoveList)
                {
                    if (Math.Abs(newx - p.CurrentPostion.x) < fearRadius && Math.Abs(newy - p.CurrentPostion.y) < fearRadius)
                    {
                        changeDir = true;
                        Scary     = p;

                        break;
                    }
                }
                if (changeDir)
                {
                    switch (rnd.Next(3))
                    {
                    case 0:
                        current.Velocity.y = -current.Velocity.y;
                        break;

                    case 1:
                        current.Velocity.y = -current.Velocity.y;
                        current.Velocity.x = -current.Velocity.x;
                        break;

                    case 2:
                        current.Velocity.x = -current.Velocity.x;
                        break;
                    }
                }
            }
            newx = ((int)current.Velocity.x + current.CurrentPostion.x);
            newy = ((int)current.Velocity.y + current.CurrentPostion.y);

            if ((current.CurrentPostion.x == best.CurrentPostion.x) && (current.CurrentPostion.y == best.CurrentPostion.y))
            {
                // dont touch it
                if (changeDir)
                {
                    //***********************************************************************
                    newx = ((int)Scary.Velocity.x + Scary.CurrentPostion.x);
                    newy = ((int)Scary.Velocity.y + Scary.CurrentPostion.y);
                    if (newx > maxwidth)
                    {
                        newx = maxwidth;
                        current.Velocity.x = 0;
                    }


                    if (newx < 0)
                    {
                        newx = 0;
                    }

                    if (newy > maxheight)
                    {
                        newy = maxheight;
                        current.Velocity.y = 0;
                    }

                    if (newy < 0)
                    {
                        newy = 0;
                    }

                    current.CurrentPostion.x     = newx;
                    current.CurrentPostion.y     = newy;
                    current.CurrentPostion.score = bmp.GetPixel(newx, newy).B;
                    //**********************************************************************
                }
                else
                {
                    current.Velocity.x = 0;
                    current.Velocity.y = 0;
                }
            }
            else
            {
                if (newx > maxwidth)
                {
                    newx = maxwidth;
                    current.Velocity.x = 0;
                }


                if (newx < 0)
                {
                    newx = 0;
                }

                if (newy > maxheight)
                {
                    newy = maxheight;
                    current.Velocity.y = 0;
                }

                if (newy < 0)
                {
                    newy = 0;
                }

                current.CurrentPostion.score = bmp.GetPixel(newx, newy).B;
                current.CurrentPostion.x     = newx;
                current.CurrentPostion.y     = newy;
            }

            return(current);
        }