Exemplo n.º 1
0
    void OnCollisionEnter2D(Collision2D col)
    {
        if (!FlagsHelper.IsSet <BallStatus>(status, BallStatus.GLOOPED))
        {
            ContactPoint2D[] cps           = col.contacts;
            Transform        parent        = GetCorrectParent(cps);
            GameObject       playerPart    = parent.gameObject;
            GameObject       collidingBall = col.gameObject;
            if (BallCollect != null)
            {
                BallCollect(this.gameObject, playerPart, collidingBall);
            }
            if (col.gameObject.layer == LayerMask.NameToLayer("GoodBall"))
            {
                PointBall pb;
                if (pb = col.gameObject.GetComponent <PointBall>())
                {
                    if (pb.HasSound)
                    {
                        SoundEffectManager.PlayClipOnce(pb.CollectSound, Vector3.zero, 1);
                    }
                    Vector3    pos    = pb.transform.position;
                    Quaternion rot    = pb.transform.rotation;
                    Vector3    scale  = pb.transform.localScale;
                    Material   mat    = pb.GetComponent <SpriteRenderer>().sharedMaterial;
                    Sprite     sprite = pb.GetComponent <SpriteRenderer>().sprite;
                    pb.Destroy();
                    AttachedBall ball = ballPool.GetPooled().GetComponent <AttachedBall>();

                    ball.transform.position = pos;
                    ball.transform.rotation = rot;
                    ball.GetComponent <SpriteRenderer>().sprite = sprite;
                    //ball.transform.localScale = scale;
                    ball.GetComponent <SpriteRenderer>().sharedMaterial = mat;
                    ball.gameObject.SetActive(true);

                    ball.transform.parent = parent;
                    if (parent == null)
                    {
                        Debug.LogError("No correct parent could be found");
                    }
                    ball.SetPointValue(pb.pointValue);
                    ball.SetType(pb.gameObject.name);

                    //if we current have any number of statuses, apply them to the new ball
                    if (status != BallStatus.NONE)
                    {
                        //apply all current status to new ball
                        ball.status = status;
                        //include all materials (except the original material)
                        for (int i = 1; i < matQueue.Count; i++)
                        {
                            ball.matQueue.Add(matQueue[i]);
                        }
                        ball.CheckStatus();
                    }
                    //Debug.Log ("Creating Attached Ball at " + pos + " parent: " + ball.transform.parent.GetInstanceID());

                    ScoreCalculator.Instance.ComboUp();
                    //recalculate score
                    ScoreCalculator.Instance.SetScorePrediction();
                }
            }
            else if (col.gameObject.layer == LayerMask.NameToLayer("BadBall"))
            {
                //Debug.Log ("BadBall Collision");
                BadBall bb = col.gameObject.GetComponent <BadBall>();
                bb.ApplyEffect(cps[0].otherCollider.transform);
                if (bb.HasSound)
                {
                    SoundEffectManager.PlayClipOnce(bb.HitSound, Vector3.zero, 1);
                }

                //recalculate score
                ScoreCalculator.Instance.SetScorePrediction();
            }
        }
    }