Exemple #1
0
    public static void PutBallToThePoint(PoolBall ball, ref Vector3 p)
    {
        float r = ball.GetRadius();

        while (Physics.OverlapSphere(p, r, 1 << LayerMask.NameToLayer("Ball") | 1 << LayerMask.NameToLayer("WhiteBall")).Length != 0)
        {
            p.x -= r;
        }
        SupportTools.SetPosition(ball.gameObject, p, SupportTools.AxisIgnore.IgnoreY, true);
        p.x -= r;
    }
Exemple #2
0
    public static void ResetAllBalls(bool pottedOnly, bool black8Origin)
    {
        CheckAnyBallInTheResetBallArea();

        IDictionary <int, PoolBall> t = new Dictionary <int, PoolBall>();
        //whether put the black 8 to the origin ,1 means true, 0 means false
        int origin = 0;
        //square root of 3
        float squr3 = 1.732f;

        for (int i = 1; i <= 15; i++)
        {
            if (Balls[i].BallState != PoolBall.State.HIDE)
            {
                if ((pottedOnly && Balls[i].BallState == PoolBall.State.POTTED) || !pottedOnly)
                {
                    t.Add(i, Balls[i]);
                }
            }
        }

        if (t.Count == 0)
        {
            return;
        }

        if (black8Origin && t.ContainsKey(8))
        {
            t[8].Reset();
            SupportTools.SetPosition(t[8].gameObject, Black8Origin.position, SupportTools.AxisIgnore.IgnoreY);
            origin = 1;
            t.Remove(8);
        }
        //the x axis space can calculated with Pythagorean theorem, the z axis space is diameter of the ball
        float      R = (CueBall.GetRadius() + 0.0001f) * 2, x = Black8Origin.position.x - squr3 * R, z;
        Vector3    p    = Vector3.one;
        List <int> list = new List <int>(t.Keys);

        for (int i = 1, k = 1; i <= 5; i++)
        {
            z = Black8Origin.position.z - (i - 1) * .5f * R;
            for (int j = 1; j <= i; j++, k++)
            {
                if (t.Count == 0)
                {
                    break;
                }
                if (k == 5 && origin == 1) //if we already put the black 8 ball , continue
                {
                    z += R;
                    continue;
                }
                if (k > 15 - t.Count - origin)
                {
                    int      l   = Random.Range(0, t.Count);
                    int      key = list[l];
                    PoolBall b   = t[key];
                    b.Reset();
                    p.z = z; p.x = x;
                    SupportTools.SetPosition(b.gameObject, p, SupportTools.AxisIgnore.IgnoreY);
                    t.Remove(key);
                    list.RemoveAt(l);
                }
                z += R;
            }
            x += .5f * R * squr3;
        }
    }