// privates
        private void RecalculateBounds()
        {
            if (Pieces.Count > 0)
            {
                AxisBounds b = new AxisBounds(Pieces[0].Bounds.center, Pieces[0].Bounds.size, () => {
                    // return local pos
                    return(Pieces[0].Bounds.center);
                }, () => {
                    // return local rotation
                    return(this.transform.rotation);
                }, () => {
                    // return local scale
                    return(this.transform.localScale);
                }, false);

                for (int i = 1; i < Pieces.Count; i++)
                {
                    if (Pieces[i].gameObject.activeSelf)
                    {
                        b.LocalEncapsulate(Pieces[i].Bounds);
                    }
                }
                Bounds = b;                                                                                                                                                                                       // set bounds temp
                UpdatePivot(true);                                                                                                                                                                                // update set pivot
                Bounds = new AxisBounds(Bounds.LocalCenter, b.size, () => { return(this.transform.position); }, () => { return(this.transform.rotation); }, () => { return(this.transform.localScale); }, false); // set bounds to updated
            }
            else
            {
                Bounds = new AxisBounds(Vector3.zero, Vector3.zero, this.transform, false);
            }
        }
        public static ModularPlacableObject StackTest(this ModularPlacableObject Test, ModularPlacableObject Target, float?range, bool CheckDefine)
        {
            AxisBounds b = Test.Bounds;

            if (Target.gameObject.activeSelf)
            {
                if (range != null)
                {
                    if (Target != Test && Target.Bounds.LocalIntersects(b) && Vector3.Distance(Target.Position, Test.Position) < range)
                    {
                        if (CheckDefine)
                        {
                            if (Test.DefinesBoundarys && Test.DefinesBoundarys == Target.DefinesBoundarys || !Test.DefinesBoundarys)                               // only stack if is stackable on this piece
                            {
                                return(Target);
                            }
                        }
                        else
                        {
                            return(Target);
                        }
                    }
                }
                else if (Target != Test && Target.Bounds.LocalIntersects(b))
                {
                    return(Target);
                }
            }
            return(null);            // if didnt pass the tests return null
        }
 public AxisBounds GetBoundaryIncludingPiece(ModularPiece Piece)
 {
     if (OriginPiece == null)
     {
         return(Piece.Bounds);
     }
     TempBounds         = new AxisBounds(Bounds, false);
     TempBounds.LastMax = Bounds.LastMax;
     TempBounds.LastMin = Bounds.LastMin;
     TempBounds.LocalEncapsulate(Piece.Bounds);
     return(TempBounds);
 }
        public ModularPlacableObject[] OverlappingPieces(ModularPlacableObject piece, float?range = null, bool CheckDefine = false)
        {
            List <ModularPlacableObject> Overlapping = new List <ModularPlacableObject> ();
            AxisBounds b = piece.Bounds;

            for (int i = 0; i < Pieces.Count; i++)
            {
                ModularPlacableObject Add = piece.StackTest(Pieces [i], range, CheckDefine);
                if (Add != null)
                {
                    Overlapping.Add(Add);
                }
            }

            return(Overlapping.ToArray());
        }
 protected override void OnAwake()
 {
     base.OnAwake();
     Bounds = new AxisBounds(Position, BoundSize, transform);
     this.gameObject.name = this.gameObject.name.Replace("(Clone)", "");             // remove clone part from name
 }
        public override void OnUpdateGrid()
        {
            var TempBounds = new AxisBounds(Bounds, true);             // set bounds position to new changed position;

            UpdateGrid(TempBounds.WorldCenter, TempBounds.WorldExtends);
        }