// Remove Position
        static public List <Slice2D> PolygonSliceAll(Vector2D position, Polygon2D slicePolygon, bool destroy, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            Polygon2D slicePolygonDestroy = null;

            if (destroy)
            {
                slicePolygonDestroy = slicePolygon.ToScale(new Vector2(1.1f, 1.1f));
                slicePolygonDestroy = slicePolygonDestroy.ToOffset(position);
            }

            slicePolygon = slicePolygon.ToOffset(position);

            foreach (Slicer2D id in GetListLayer(layer))
            {
                result.Add(id.PolygonSlice(slicePolygon, slicePolygonDestroy));
            }

            return(result);
        }
        static public List <Slice2D> LinearSliceAll(Pair2D slice, Slice2DLayer layer = null, bool perform = true)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.LinearSlice(slice, perform);

                if (perform)
                {
                    if (sliceResult.GetGameObjects().Count > 0)
                    {
                        result.Add(sliceResult);
                    }
                }
                else
                {
                    if (sliceResult.GetPolygons().Count > 0)
                    {
                        result.Add(sliceResult);
                    }
                }
            }

            return(result);
        }
 public void SetController(GameObject gameObject, Slicer2DInputController inputController, Slicer2DVisuals visualsSettings, Slice2DLayer layerObject, Slicer2DControllerEventHandling eventHandling)
 {
     input        = inputController;
     visuals      = visualsSettings;
     sliceLayer   = layerObject;
     eventHandler = eventHandling;
     visuals.SetGameObject(gameObject);
 }
        static public List <Slicer2D> GetListLayer(Slice2DLayer layer)
        {
            getLayerList.Clear();

            foreach (Slicer2D id in slicer2DList)
            {
                if (id.MatchLayers(layer))
                {
                    getLayerList.Add(id);
                }
            }

            return(getLayerList);
        }
        public void Slice()
        {
            float timer = Time.realtimeSinceStartup;

            foreach (Transform t in transform)
            {
                Polygon2D poly = Polygon2DList.CreateFromGameObject(t.gameObject)[0].ToWorldSpace(t);

                Slicer2D.ComplexSliceAll(poly.pointsList, Slice2DLayer.Create());
            }

            Destroy(gameObject);

            Debug.Log(name + " in " + (Time.realtimeSinceStartup - timer) * 1000 + "ms");
        }
        static public List <Merge2D> PolygonMergeAll(Polygon2D slicePolygon, Slice2DLayer layer = null)
        {
            List <Merge2D> result = new List <Merge2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                result.Add(id.PolygonMerge(slicePolygon));
            }

            return(result);
        }
        static public List <Slice2D> ComplexSliceAll(List <Vector2D> slice, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.ComplexSlice(slice);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        static public List <Slice2D> LinearCutSliceAll(LinearCut linearCut, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.LinearCutSlice(linearCut);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        ///// Merger2D ALL
        static public List <Merge2D> ComplexMergeAll(List <Vector2D> slice, Slice2DLayer layer = null)
        {
            List <Merge2D> result = new List <Merge2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Merge2D sliceResult = id.ComplexMerge(slice);
                if (sliceResult.polygons.Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        static public List <Slice2D> ExplodeAll(Slice2DLayer layer = null, int explosionSlices = 0)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.Explode(explosionSlices);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        static public List <Slice2D> ExplodeInPointAll(Vector2D point, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.ExplodeInPoint(point);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        static public List <Slice2D> PointSliceAll(Vector2D slice, float rotation, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.PointSlice(slice, rotation);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
        public List <Slice2D> Update(Vector2D position, float timer, Slice2DLayer layer)
        {
            List <Slice2D> result = new List <Slice2D>();

            foreach (Slicer2D slicer in Slicer2D.GetListCopy())
            {
                if (slicer.MatchLayers(layer) == false)
                {
                    continue;
                }

                SlicerTrailObject trail = null;
                trailList.TryGetValue(slicer, out trail);

                if (trail == null)
                {
                    trail = new SlicerTrailObject();
                    trailList.Add(slicer, trail);
                }

                if (trail.lastPosition != null)
                {
                    if (Vector2D.Distance(trail.lastPosition, position) > 0.05f)
                    {
                        trail.pointsList.Add(new TrailPoint(position, timer));
                    }
                }
                else
                {
                    trail.pointsList.Add(new TrailPoint(position, timer));
                }

                if (trail.pointsList.Count > 1)
                {
                    foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList))
                    {
                        if (trailPoint.Update() == false)
                        {
                            trail.pointsList.Remove(trailPoint);
                        }
                    }

                    List <Vector2D> points = new List <Vector2D>();
                    foreach (TrailPoint trailPoint in trail.pointsList)
                    {
                        points.Add(trailPoint.position);
                    }

                    Slicer2D.complexSliceType = Slicer2D.SliceType.Regular;
                    Slice2D slice = slicer.ComplexSlice(points);
                    if (slice.GetGameObjects().Count > 0)
                    {
                        trailList.Remove(slicer);

                        result.Add(slice);
                    }
                    ;
                }

                trail.lastPosition = position;
            }

            return(result);
        }
        public List <Slice2D> Update(Vector2D position, float timer, Slice2DLayer layer)
        {
            List <Slice2D> result = new List <Slice2D>();

            foreach (Slicer2D slicer in Slicer2D.GetListCopy())
            {
                if (slicer.MatchLayers(layer) == false)
                {
                    continue;
                }

                SlicerTrailObject trail = null;
                trailList.TryGetValue(slicer, out trail);

                if (trail == null)
                {
                    trail = new SlicerTrailObject();
                    trailList.Add(slicer, trail);
                }

                if (trail.lastPosition != null)
                {
                    if (Vector2D.Distance(trail.lastPosition, position) > 0.05f)
                    {
                        trail.pointsList.Insert(0, new TrailPoint(position, timer));
                    }
                }
                else
                {
                    trail.pointsList.Insert(0, new TrailPoint(position, timer));
                }

                foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList))
                {
                    if (trailPoint.Update() == false)
                    {
                        trail.pointsList.Remove(trailPoint);
                    }
                }

                if (trail.pointsList.Count > 1)
                {
                    Vector2D firstPoint  = null;
                    Vector2D lastPoint   = null;
                    bool     insideState = false;

                    foreach (TrailPoint trailPoint in trail.pointsList)
                    {
                        bool inside = false;
                        if (slicer.shape.GetLocal().PointInPoly(trailPoint.position.InverseTransformPoint(slicer.transform)))
                        {
                            inside = true;
                        }

                        switch (insideState)
                        {
                        case true:
                            // Slice!
                            if (inside == false)
                            {
                                lastPoint = trailPoint.position;

                                insideState = false;
                                break;
                            }
                            break;

                        case false:
                            if (inside == false)
                            {
                                // Searching For Start of Slice
                                firstPoint  = trailPoint.position;
                                insideState = true;
                            }
                            break;
                        }

                        if (lastPoint != null)
                        {
                            break;
                        }
                    }

                    if (firstPoint != null && lastPoint != null)
                    {
                        Slicer2D.complexSliceType = Slicer2D.SliceType.Regular;
                        Slice2D slice = slicer.LinearSlice(new Pair2D(firstPoint, lastPoint));
                        if (slice.GetGameObjects().Count > 0)
                        {
                            trailList.Remove(slicer);

                            result.Add(slice);
                        }
                        ;
                    }
                }

                trail.lastPosition = position;
            }
            return(result);
        }
 public bool MatchLayers(Slice2DLayer sliceLayer)
 {
     return((sliceLayer == null || sliceLayer.GetLayerType() == Slice2DLayerType.All) || sliceLayer.GetLayerState(GetLayerID()));
 }