예제 #1
0
    public static bool OnAnchorSlice(Slicer2D slicer, Slice2D sliceResult)
    {
        if (slicer.eventHandler.AnchorSliceEvent(sliceResult) == false)
        {
            return(false);
        }

        if (Slicer2DEventHandling.GlobalSliceEvent(sliceResult) == false)
        {
            return(false);
        }

        switch (slicer.anchorType)
        {
        case Slicer2D.AnchorType.CancelSlice:
            foreach (Polygon2D polyA in new List <Polygon2D>(sliceResult.GetPolygons()))
            {
                bool perform = true;
                foreach (Polygon2D polyB in slicer.anchorPolygons)
                {
                    if (Math2D.PolyCollidePoly(polyA, GetPolygonInWorldSpace(slicer, polyB)))
                    {
                        perform = false;
                    }
                }
                if (perform)
                {
                    return(false);
                }
            }
            break;

        /*
         * case AnchorType.DestroySlice:
         *      foreach (Polygon2D polyA in new List<Polygon2D>(sliceResult.polygons)) {
         *              bool perform = true;
         *              foreach(Polygon2D polyB in polygons) {
         *                      if (Math2D.PolyCollidePoly (polyA.pointsList, GetPolygonInWorldSpace(polyB).pointsList) ) {
         *                              sliceResult.polygons.Remove(polyB);
         *                      }
         *              }
         *
         *      }
         *      break;
         */

        default:
            break;
        }
        return(true);
    }
예제 #2
0
        public List <GameObject> PerformResult(List <Polygon2D> result, Slice2D slice)
        {
            List <GameObject> resultGameObjects = new List <GameObject> ();

            if (result.Count < 1)
            {
                return(resultGameObjects);
            }

            slice.SetPolygons(result);

            slice.originGameObject = gameObject;

            if (eventHandler.SliceEvent(slice) == false)
            {
                return(resultGameObjects);
            }

            if (Slicer2DEventHandling.GlobalSliceEvent(slice) == false)
            {
                return(resultGameObjects);
            }

            double originArea = 1f;

            if (recalculateMass)
            {
                originArea = shape.GetLocal().GetArea();
            }

            Rigidbody2D originalRigidBody = gameObject.GetComponent <Rigidbody2D>();

            Collider2D        collider2D = gameObject.GetComponent <Collider2D>();
            PhysicsMaterial2D material   = collider2D.sharedMaterial;
            bool isTrigger = collider2D.isTrigger;

            switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
            {
            case InstantiationMethod.Performance:

                switch (textureType)
                {
                case TextureType.Sprite:
                case TextureType.Sprite3D:
                    if (spriteRendererComponent)
                    {
                        DestroyImmediate(spriteRendererComponent);
                        spriteRendererComponent = null;
                    }
                    break;
                }

                BoxCollider2D c1 = gameObject.GetComponent <BoxCollider2D>();
                if (c1 != null)
                {
                    DestroyImmediate(c1);
                }

                CircleCollider2D c2 = gameObject.GetComponent <CircleCollider2D>();
                if (c2 != null)
                {
                    DestroyImmediate(c2);
                }

                CapsuleCollider2D c3 = gameObject.GetComponent <CapsuleCollider2D>();
                if (c3 != null)
                {
                    DestroyImmediate(c3);
                }
                break;
            }

            int name_id = 1;

            foreach (Polygon2D id in result)
            {
                GameObject gObject = null;

                switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
                {
                case InstantiationMethod.Performance:
                    Slicer2DProfiler.IncSlicesCreatedWithPerformance();

                    gObject = Instantiate(gameObject, transform.position, transform.rotation, transform.parent);

                    break;

                case InstantiationMethod.Quality:
                    Slicer2DProfiler.IncSlicesCreatedWithQuality();

                    gObject = new GameObject();
                    gObject.transform.parent   = transform.parent;
                    gObject.transform.position = transform.position;
                    gObject.transform.rotation = transform.rotation;

                    break;
                }

                gObject.name = name + " (" + name_id + ")";
                gObject.transform.localScale = transform.localScale;

                gObject.layer = gameObject.layer;
                gObject.tag   = gameObject.tag;

                resultGameObjects.Add(gObject);

                switch (Slicer2DSettings.GetComponentsCopy(instantiateMethod))
                {
                case InstantiationMethod.Quality:
                    Slicer2DComponents.Copy(this, gObject);
                    break;
                }

                Slicer2D slicer = gObject.GetComponent <Slicer2D> ();

                //ここがPlay時に情報を書き込んでいるところ
                slicer.limit           = new Slicer2DLimit();
                slicer.limit.counter   = limit.counter + 1;
                slicer.limit.maxSlices = limit.maxSlices;
                slicer.limit.enabled   = limit.enabled;


                slicer.eventHandler = new Slicer2DEventHandling();

                slicer.shape = new Slicer2DShape();
                slicer.shape.SetSlicer2D(slicer);
                slicer.shape.ForceUpdate();

                slicer.materialSettings = materialSettings.Copy();

                slicer.anchor = anchor.Copy();

                Slicer2DComponents.CopyRigidbody2D(originalRigidBody, slicer, id, originArea);

                if (Slicer2DSettings.GetCenterOfSliceTransform(centerOfSlice) == CenterOfSliceTransform.ColliderCenter)
                {
                    Polygon2D localPoly = id.ToLocalSpace(gObject.transform);
                    Rect      bounds    = localPoly.GetBounds();
                    Vector2   center    = bounds.center;

                    Vector2 centerWorld = new Vector2(center.x * transform.lossyScale.x, center.y * transform.lossyScale.y);
                    gObject.transform.Translate(centerWorld);

                    slicer.materialSettings.offset += center;
                }

                Collider2D collider = null;
                switch (colliderType)
                {
                case ColliderType.PolygonCollider2D:
                    collider = (Collider2D)id.ToLocalSpace(gObject.transform).CreatePolygonCollider(gObject);
                    break;

                case ColliderType.EdgeCollider2D:
                    collider = (Collider2D)id.ToLocalSpace(gObject.transform).CreateEdgeCollider(gObject);
                    break;
                }

                collider.sharedMaterial = material;
                collider.isTrigger      = isTrigger;

                Vector2D uvOffset;

                switch (textureType)
                {
                case TextureType.Sprite:
                    slicer.spriteRenderer = spriteRenderer;
                    uvOffset = new Vector2D(slicer.materialSettings.offset);
                    Polygon2D.SpriteToMesh(gObject, spriteRenderer, materialSettings.GetTriangulation(), uvOffset);
                    break;

                case TextureType.Sprite3D:
                    slicer.spriteRenderer = spriteRenderer;
                    uvOffset = new Vector2D(slicer.materialSettings.offset);
                    Polygon2D.SpriteToMesh3D(gObject, spriteRenderer, materialSettings.depth, materialSettings.GetTriangulation(), uvOffset);

                    MeshRenderer meshRenderer = slicer.GetComponent <MeshRenderer>();

                    Material[] sharedMaterials = new Material[2];
                    sharedMaterials[1] = spriteRenderer.material;
                    sharedMaterials[0] = materialSettings.sideMaterial;

                    meshRenderer.sharedMaterials = sharedMaterials;

                    break;

                case TextureType.SpriteAnimation:
                    slicer.textureType = TextureType.Sprite;
                    Polygon2D.SpriteToMesh(gObject, spriteRenderer, materialSettings.GetTriangulation());
                    break;

                default:
                    break;
                }

                name_id += 1;
            }

            if (afterSliceRemoveOrigin)
            {
                Destroy(gameObject);
            }

            if (resultGameObjects.Count > 0)
            {
                slice.originGameObject = gameObject;

                slice.SetGameObjects(resultGameObjects);

                if (supportJoints == true)
                {
                    SliceJointEvent(slice);
                }

                eventHandler.Result(slice);
            }

            return(resultGameObjects);
        }