Exemplo n.º 1
0
    static public void ComplexSlice(Slice2D slice, float forceAmount)
    {
        foreach (GameObject gameObject in slice.GetGameObjects())
        {
            Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();

            List <Vector2D> collisions = slice.GetCollisions();

            if (rigidBody2D)
            {
                float forceVal = 2.0f / collisions.Count;

                Vector2 force = new Vector2();
                float   sliceRotation;
                Vector2 centerPos = new Vector2();

                for (int i = 0; i < collisions.Count - 1; i++)
                {
                    pair.A = collisions[i];
                    pair.B = collisions[i + 1];

                    sliceRotation = (float)Vector2D.Atan2(pair.B, pair.A);

                    force.x = Mathf.Cos(sliceRotation) * forceAmount;
                    force.y = Mathf.Sin(sliceRotation) * forceAmount;

                    centerPos.x = (float)(pair.A.x + pair.B.x) / 2f;
                    centerPos.y = (float)(pair.A.y + pair.B.y) / 2f;

                    Physics2DHelper.AddForceAtPosition(rigidBody2D, forceVal * force, centerPos);
                }
            }
        }
    }
Exemplo n.º 2
0
    private void LinearSlice(Pair2D slice)
    {
        List <Slice2D> results = Slicer2D.LinearSliceAll(slice, null);

        if (addForce == false)
        {
            return;
        }

        // Adding Physics Forces
        float sliceRotation = (float)Vector2D.Atan2(slice.B, slice.A);

        foreach (Slice2D id in results)
        {
            foreach (GameObject gameObject in id.GetGameObjects())
            {
                Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                if (rigidBody2D == null)
                {
                    continue;
                }
                foreach (Vector2D p in id.GetCollisions())
                {
                    Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount), p.ToVector2());
                }
            }
        }
    }
Exemplo n.º 3
0
    private void ExplodeAll()
    {
        List <Slice2D> results = Slicer2D.ExplodeAll(sliceLayer);

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        float sliceRotation = Vector2D.Atan2(new Vector2D(0, 0), new Vector2D(gameObject.transform.position));
                        Rect  rect          = Polygon2D.CreateFromCollider(gameObject).GetBounds();
                        Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addForceAmount / 10f, Mathf.Sin(sliceRotation) * addForceAmount / 10f), rect.center);
                    }
                }
                if (sliceResultEvent != null)
                {
                    sliceResultEvent(id);
                }
            }
        }
    }
Exemplo n.º 4
0
    // Polygon Scatter Particles Effect
    void ExplodePolygon(GameObject CutObject)
    {
        Slicer2D.explosionPieces = 5;
        Slice2D explosionResult = CutObject.GetComponent <Slicer2D>().Explode();

        float z = 0f;

        foreach (GameObject b in explosionResult.gameObjects)
        {
            z -= 0.01f;

            Slicer2D slicer = b.GetComponent <Slicer2D>();
            slicer.Initialize();
            Destroy(slicer);

            b.AddComponent <DestroyTimer>();

            Rigidbody2D rigidBody2D = b.AddComponent <Rigidbody2D>();

            b.transform.Translate(0, 0, 1 + z);

            if (rigidBody2D)
            {
                Rect  rect          = Polygon2D.CreateFromCollider(b).GetBounds();
                float sliceRotation = Vector2D.Atan2(new Vector2D(rect.center), new Vector2D(b.transform.position));
                Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * 351f, Mathf.Sin(sliceRotation) * 351f), rect.center);
            }
        }
    }
Exemplo n.º 5
0
    private void Explode(Vector2 position)
    {
        if (fixedJoint != null)
        {
            Destroy(fixedJoint);
        }

        transform.SetParent(null);
        List <Slice2D> results = Slicer2D.ExplodeAll(BreakableManager.Instance.Slice2DLayer);

        if (addedExplosionForceOnBreak > 0)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    var joint = gameObject.GetComponent <FixedJoint2D>();
                    if (joint != null)
                    {
                        Destroy(joint);
                    }
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D>();
                    if (rigidBody2D)
                    {
                        float sliceRotation = Vector2D.Atan2(new Vector2D(position),
                                                             new Vector2D(gameObject.transform.position));
                        Rect rect = Polygon2D.CreateFromCollider(gameObject).GetBounds();
                        Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) *
                                                                                    addedExplosionForceOnBreak, Mathf.Sin(sliceRotation) * addedExplosionForceOnBreak), rect.center);
                    }
                }
            }
        }
    }
Exemplo n.º 6
0
 static public void ExplodeInPoint(Slice2D slice, float forceAmount, Vector2D point)
 {
     foreach (GameObject gameObject in slice.GetGameObjects())
     {
         Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
         if (rigidBody2D)
         {
             float sliceRotation = (float)Vector2D.Atan2(point, new Vector2D(gameObject.transform.position));
             Rect  rect          = Polygon2DList.CreateFromGameObject(gameObject)[0].GetBounds();
             Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * forceAmount, Mathf.Sin(sliceRotation) * forceAmount), rect.center);
         }
     }
 }
Exemplo n.º 7
0
 static public void LinearTrail(Slice2D slice, float forceAmount)
 {
     foreach (GameObject gameObject in slice.gameObjects)
     {
         Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
         if (rigidBody2D)
         {
             float sliceRotation = (float)Vector2D.Atan2(slice.slice[0], slice.slice[1]);
             foreach (Vector2D p in slice.collisions)
             {
                 Vector2 force = new Vector2(Mathf.Cos(sliceRotation) * forceAmount, Mathf.Sin(sliceRotation) * forceAmount);
                 Physics2DHelper.AddForceAtPosition(rigidBody2D, force, p.ToVector2());
             }
         }
     }
 }
Exemplo n.º 8
0
    private bool LinearSlice(Pair2D slice)
    {
        if (sliceJoints)
        {
            LinearSliceJoints(slice);
        }

        List <Slice2D> results = Slicer2D.LinearSliceAll(slice, sliceLayer);

        bool result = false;

        foreach (Slice2D id in results)
        {
            if (id.gameObjects.Count > 0)
            {
                result = true;
            }
        }

        if (addForce == true)
        {
            float sliceRotation = Vector2D.Atan2(slice.B, slice.A);

            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        foreach (Vector2D p in id.collisions)
                        {
                            Vector2 force = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount);
                            Physics2DHelper.AddForceAtPosition(rigidBody2D, force, p.vector);
                        }
                    }
                }
                if (sliceResultEvent != null)
                {
                    sliceResultEvent(id);
                }
            }
        }

        return(result);
    }
Exemplo n.º 9
0
    private bool ComplexSlice(List <Vector2D> slice)
    {
        if (sliceJoints)
        {
            ComplexSliceJoints(slice);
        }

        List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, sliceLayer);
        bool           result  = false;

        foreach (Slice2D id in results)
        {
            if (id.gameObjects.Count > 0)
            {
                result = true;
            }
        }

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        List <Pair2D> list     = Pair2D.GetList(id.collisions);
                        float         forceVal = 1.0f / list.Count;
                        foreach (Pair2D p in list)
                        {
                            float   sliceRotation = -Vector2D.Atan2(p.B, p.A);
                            Vector2 force         = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount);
                            Physics2DHelper.AddForceAtPosition(rigidBody2D, forceVal * force, (p.A.vector + p.B.vector) / 2f);
                        }
                    }
                }
                if (sliceResultEvent != null)
                {
                    sliceResultEvent(id);
                }
            }
        }
        return(result);
    }
Exemplo n.º 10
0
 static public void ComplexTrail(Slice2D slice, float forceAmount)
 {
     foreach (GameObject gameObject in slice.gameObjects)
     {
         Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
         if (rigidBody2D)
         {
             List <Pair2D> list     = Pair2D.GetList(slice.collisions, false);
             float         forceVal = 2.0f / list.Count;
             foreach (Pair2D p in list)
             {
                 float   sliceRotation = (float)Vector2D.Atan2(p.B, p.A);
                 Vector2 force         = new Vector2(Mathf.Cos(sliceRotation) * forceAmount, Mathf.Sin(sliceRotation) * forceAmount);
                 Physics2DHelper.AddForceAtPosition(rigidBody2D, forceVal * force, (p.A + p.B).ToVector2() / 2f);
             }
         }
     }
 }
Exemplo n.º 11
0
    static public void ComplexTrail(Slice2D slice, float forceAmount)
    {
        Rigidbody2D rigidBody2D;
        Vector2D    vec;
        float       sliceRotation;
        float       forceVal;
        Vector2     vecSum = Vector2.zero;

        for (int i = 0; i < slice.GetGameObjects().Count; i++)
        {
            rigidBody2D = slice.GetGameObjects()[i].GetComponent <Rigidbody2D> ();
            if (rigidBody2D)
            {
                List <Vector2D> collisions = slice.GetCollisions();

                forceVal = 2.0f / collisions.Count;
                for (int x = 0; x < collisions.Count; x++)
                {
                    vec = collisions[x];

                    pair.A = vec;

                    if (pair.A != null && pair.B != null)
                    {
                        sliceRotation = (float)Vector2D.Atan2(pair.A, pair.B);

                        force.x = Mathf.Cos(sliceRotation) * forceAmount;
                        force.y = Mathf.Sin(sliceRotation) * forceAmount;

                        vecSum.x = (float)(pair.A.x + pair.B.x) / 2f;
                        vecSum.y = (float)(pair.A.y + pair.B.y) / 2f;

                        Physics2DHelper.AddForceAtPosition(rigidBody2D, forceVal * force, vecSum);
                    }

                    pair.B = pair.A;
                }
            }
        }
    }
Exemplo n.º 12
0
    static public void LinearSlice(Slice2D slice, float forceAmount)
    {
        float       sliceRotation = (float)Vector2D.Atan2(slice.slice[1], slice.slice[0]);
        Rigidbody2D rigidBody2D;
        Vector2     force = new Vector2();

        foreach (GameObject gameObject in slice.GetGameObjects())
        {
            rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
            if (rigidBody2D)
            {
                List <Vector2D> collisions = slice.GetCollisions();

                foreach (Vector2D p in collisions)
                {
                    force.x = Mathf.Cos(sliceRotation) * forceAmount;
                    force.y = Mathf.Sin(sliceRotation) * forceAmount;

                    Physics2DHelper.AddForceAtPosition(rigidBody2D, force, p.ToVector2());
                }
            }
        }
    }
Exemplo n.º 13
0
    private void ComplexSlice(List <Vector2D> slice)
    {
        List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, null);

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        foreach (Pair2D p in Pair2D.GetList(id.collisions))
                        {
                            float sliceRotation = Vector2D.Atan2(p.B, p.A);
                            Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount), (p.A.vector + p.B.vector) / 2f);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 14
0
    private void LinearSlice(Pair2D slice)
    {
        List <Slice2D> results = Slicer2D.LinearSliceAll(slice, null);

        // Adding Physics Forces
        if (addForce == true)
        {
            float sliceRotation = Vector2D.Atan2(slice.B, slice.A);
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        foreach (Vector2D p in id.collisions)
                        {
                            Physics2DHelper.AddForceAtPosition(rigidBody2D, new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount), p.vector);
                        }
                    }
                }
            }
        }
    }