示例#1
0
文件: SnakeAI.cs 项目: akil03/bx
    public Vector3 GetNewDirection(HashSet <Vector3> directionsToExclude)
    {
        Vector3 up    = transform.up;
        Vector3 down  = -transform.up;
        Vector3 right = transform.right;
        Vector3 left  = -transform.right;

        List <Vector3> directions = new List <Vector3> ();


        directions.Add(up);
        directions.Add(down);
        directions.Add(right);
        directions.Add(left);


        foreach (Vector3 dir in directionsToExclude)
        {
            directions.Remove(dir);
        }


        if (directions.Count == 0)
        {
            directionsToExclude = new HashSet <Vector3> ();
            directionsToExclude.Add(-snake.currentMoveDirection);
            return(GetNewDirection(directionsToExclude));
        }

        int     random    = Random.Range(0, directions.Count);
        Vector3 direction = directions [random];


        if (useImprovedIA)
        {
            bool canGoInDirection = false;
            while (!canGoInDirection)
            {
                if (Poly.CheckIfCanGoInDirection(lastReachedGroundPiece, direction, snake))
                {
                    canGoInDirection = true;
                }
                else
                {
                    directions.Remove(direction);
                    random    = Random.Range(0, directions.Count);
                    direction = directions [random];
                }
            }
        }

        return(direction);
    }