public static global::FilsDeBerger.SDL.MoveDirection FleeCharacters(SDL.Character curChar, int fleeDistance, SDL.Character[] charsToFlee)
        {
            SortTableOnNearestDistance(curChar, charsToFlee);

            if (charsToFlee.GetLength(0) > 0)
            {
                if (curChar.GetDistance(charsToFlee[0]) < fleeDistance)
                {
                    // We will think of which direction to take as we fear player controlled Character
                    if (Math.Abs(curChar.Position.X - charsToFlee[0].Position.X) >
                        Math.Abs(curChar.Position.Y - charsToFlee[0].Position.Y))
                    {
                        // Then it will be right or left
                        if (curChar.Position.X < charsToFlee[0].Position.X)
                        {
                            // We are at the left of player controlled character, continue to the left
                            return global::FilsDeBerger.SDL.MoveDirection.Left;
                        }
                        else
                        {
                            // We are at the right of player controlled character, continue to the right
                            return global::FilsDeBerger.SDL.MoveDirection.Right;
                        }
                    }
                    else
                    {
                        // It will be up or down
                        if (curChar.Position.Y < charsToFlee[0].Position.Y)
                        {
                            // We are upper to player controlled character, continue to move up
                            return global::FilsDeBerger.SDL.MoveDirection.Up;
                        }
                        else
                        {
                            // We are bottom to player controlled character, continue to move down
                            return global::FilsDeBerger.SDL.MoveDirection.Down;
                        }
                    }
                }
                else
                {
                    // We are not in the danger zone, let's just try to eat something
                    return global::FilsDeBerger.SDL.MoveDirection.Stop;
                }
            }
            else
            {
                // There is a problem, stop the IA
                return global::FilsDeBerger.SDL.MoveDirection.Stop;
            }
        }
 public static void SortTableOnNearestDistance(SDL.Character curChar, SDL.Character[] charsToFlee)
 {
     // 2. Find the nearest player
     Array.Sort<SDL.Character>(
         charsToFlee,
         delegate(SDL.Character a, SDL.Character b)
         {
             if (a != b)
             {
                 return curChar.GetDistance(a).CompareTo(curChar.GetDistance(b));
             }
             else
             {
                 return 0;
             }
         });
 }
Пример #3
0
        /// <summary>
        /// Method responsible of sheep movement
        /// </summary>
        /// <param name="curChar">Current sheep to move</param>
        /// <param name="allChar">Table of all characters of the game</param>
        /// <returns>Direction in which the sheep will move</returns>
        public static SDL.MoveDirection Think(SDL.Character curChar, SDL.Character[] allChar)
        {
            if (!curChar.Disposed)
            {
                // Is sheep safe or not, if safe, run out of the screen
                SDL.Sheep curSheep = curChar as SDL.Sheep;
                if (curSheep.Safe)
                {
                    if (curSheep.X < ScreenSize.Width)
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Right;
                    }
                    else
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Stop;
                    }
                }
                else
                {
                    // First check we are in screen
                    if (curChar.X <= 1)
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Right;
                    }
                    else if (curChar.X >= ScreenSize.Width - curChar.Width)
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Left;
                    }
                    else if (curChar.Y <= 1)
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Down;
                    }
                    else if (curChar.Y >= ScreenSize.Height - curChar.Height)
                    {
                        return global::FilsDeBerger.SDL.MoveDirection.Up;
                    }
                    else
                    {
                        // Is sheep near one of the player controlled objects
                        // 1. Find the player controlled characters
                        SDL.Character[] charactersToFlee = Array.FindAll(
                            allChar,
                            delegate(SDL.Character toCheck)
                            {
                                return toCheck.Control == global::FilsDeBerger.SDL.Controller.Player || toCheck.Control == global::FilsDeBerger.SDL.Controller.AltPlayer || toCheck.Eatable == false;
                            });
                        SDL.MoveDirection result = CommonIABehaviors.FleeCharacters(curChar, 100, charactersToFlee);
                        if (result == global::FilsDeBerger.SDL.MoveDirection.Stop)
                        {
                            // We are not in the danger zone, let's just do something stupid
                            if (t.NextDouble() > .9)
                            {
                                // Choose a new direction
                                result = (SDL.MoveDirection)t.Next(5);
                            }
                            else
                            {
                                if (t.NextDouble() > .7)
                                {
                                    // Just stop the sheep
                                    result = global::FilsDeBerger.SDL.MoveDirection.Stop;
                                }
                                else
                                {
                                    // We just continue in the same direction
                                    switch (curChar.CurrentAnimation)
                                    {
                                        case "WalkUp":
                                            result = global::FilsDeBerger.SDL.MoveDirection.Up;
                                            break;
                                        case "WalkDown":
                                            result = global::FilsDeBerger.SDL.MoveDirection.Down;
                                            break;
                                        case "WalkLeft":
                                            result = global::FilsDeBerger.SDL.MoveDirection.Left;
                                            break;
                                        case "WalkRight":
                                            result = global::FilsDeBerger.SDL.MoveDirection.Right;
                                            break;
                                        default:
                                            result = global::FilsDeBerger.SDL.MoveDirection.Stop;
                                            break;
                                    }
                                }
                            }
                        }

                        return result;
                    }
                }
            }
            else
            {
                return global::FilsDeBerger.SDL.MoveDirection.Stop;
            }
        }