Example #1
0
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.transform.tag == "Player")
        {
            int id = col.transform.GetInstanceID();
            if (!_colSet.Contains(id))
            {
                _colSet.Add(id);
                CueBall other = col.gameObject.GetComponent <CueBall>();
                other.AddToColSet(transform.GetInstanceID());
                var oldSprite = GetComponent <SpriteRenderer>().sprite;

                var thisVel  = GetComponent <Rigidbody2D>().velocity.magnitude;
                var otherVel = other.GetComponent <Rigidbody2D>().velocity.magnitude;

                _audioSource.volume = Mathf.Max(Mathf.Sqrt(thisVel + otherVel) / 5, 0.2f);
                Debug.Log(thisVel + otherVel);
                _audioSource.PlayOneShot(collideSound);

                BallType  oldType  = Type;
                BallState oldState = _state;
                int       oldLayer = gameObject.layer;
                ChangeBalls(other.Type, other.GetState(), other.GetComponent <SpriteRenderer>().sprite, other.gameObject.layer);

                other.ChangeBalls(oldType, oldState, oldSprite, oldLayer);
            }
        }
        else
        {
            _audioSource.volume = 0.1f;
            _audioSource.PlayOneShot(cueSound);
        }
    }
Example #2
0
    public static Vector2 GetTableSize()
    {
        Constraint c = CueBall.GetComponent <Constraint>();
        Vector2    v = new Vector2(c.max.x - c.min.x, c.max.z - c.min.z);

        return(v);
    }
Example #3
0
 private void Start()
 {
     if (_cueBall == null)
     {
         _cueBall = FindObjectOfType <CueBall>();
         Assert.IsNotNull(_cueBall, "Failed to get reference to the CueBall script");
     }
 }
Example #4
0
    public static void GetTableMinAndMaxPoints(out Vector3 min, out Vector3 max)
    {
        Constraint c = CueBall.GetComponent <Constraint>();

        min   = c.min;
        min.y = CueBall.transform.position.y;
        max   = c.max;
        max.y = CueBall.transform.position.y;
    }
    public void BallPocketed(GameObject ball)
    {
        Debug.Log("HERE " + ball.tag);
        Debug.Log(ball.name);
        //Check for a match with the specific tag on any GameObject that collides with your GameObject
        if (ball.tag == "Ball")
        {
            Debug.Log(ball.name + " is pocketed.");
            ball.SetActive(false);
            ball.transform.position = positions[ball.name];
            ball.transform.rotation = rotations[ball.name];
            Rigidbody rb = ball.GetComponent <Rigidbody>();
            rb.velocity           = Vector3.zero;
            rb.angularVelocity    = Vector3.zero;
            ball.transform.parent = Pocketed.transform;
        }
        else if (ball.name == "CueBall")
        {
            Rigidbody rb = CueBall.GetComponent <Rigidbody>();
            rb.velocity                = Vector3.zero;
            rb.angularVelocity         = Vector3.zero;
            CueBall.transform.position = initCuePos;
        }

        if (Unpocketed.transform.childCount == 0)
        {
            Rigidbody rb = CueBall.GetComponent <Rigidbody>();
            rb.velocity                = Vector3.zero;
            rb.angularVelocity         = Vector3.zero;
            CueBall.transform.position = initCuePos;
            while (Pocketed.transform.childCount != 0)
            {
                Transform child = Pocketed.transform.GetChild(0);
                child.parent = Unpocketed.transform;
                child.gameObject.SetActive(true);
            }
        }
    }
Example #6
0
 public void AddBall(CueBall b)
 {
     b.transform.localScale = new Vector2(3, 3);
     b.transform.position   = new Vector2(-12 + _reds++, -8.3f);
 }
Example #7
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;
        }
    }