Exemple #1
0
    bool SliceObject(ref Plane slicePlane, GameObject obj, List <Transform> positiveObjects, List <Transform> negativeObjects)
    {
        var mesh = obj.GetComponent <MeshFilter>().mesh;

        if (!meshCutter.SliceMesh(mesh, ref slicePlane))
        {
            // Put object in the respective list
            if (slicePlane.GetDistanceToPoint(meshCutter.GetFirstVertex()) >= 0)
            {
                positiveObjects.Add(obj.transform);
            }
            else
            {
                negativeObjects.Add(obj.transform);
            }

            return(false);
        }

        // TODO: Update center of mass

        // Silly condition that labels which mesh is bigger to keep the bigger mesh in the original gameobject
        bool posBigger = meshCutter.PositiveMesh.surfacearea > meshCutter.NegativeMesh.surfacearea;

        if (posBigger)
        {
            biggerMesh  = meshCutter.PositiveMesh;
            smallerMesh = meshCutter.NegativeMesh;
        }
        else
        {
            biggerMesh  = meshCutter.NegativeMesh;
            smallerMesh = meshCutter.PositiveMesh;
        }

        // Create new Sliced object with the other mesh
        var newObject = Instantiate(obj, objectContainer);

        spawner.NewFruitCut(newObject);
        newObject.transform.SetPositionAndRotation(obj.transform.position, obj.transform.rotation);
        var newObjMesh = newObject.GetComponent <MeshFilter>().mesh;

        // Put the bigger mesh in the original object
        // TODO: Enable collider generation (either the exact mesh or compute smallest enclosing sphere)
        ReplaceMesh(mesh, biggerMesh);
        ReplaceMesh(newObjMesh, smallerMesh);

        (posBigger ? positiveObjects : negativeObjects).Add(obj.transform);
        (posBigger ? negativeObjects : positiveObjects).Add(newObject.transform);

        if (obj.layer != /*UI*/ 5)
        {
            sController?.AddScore();
        }
        else
        {
            uiSliceCount += 1;
            if (uiSliceCount == 5)
            {
                gameStarter.GoToGameMode();
                for (int i = 0; i < fruitsChild.transform.childCount; i++)
                {
                    fruitsChild.transform.GetChild(i).gameObject.SetActive(false);
                }
            }
        }
        //audio
        sliceSample.Play();
        //slice effect
        if (fruitSliceEffect != null)
        {
            Instantiate(fruitSliceEffect, obj.transform);
        }

        obj.tag       = "nonSlisable";
        newObject.tag = "nonSlisable";
        return(true);
    }