/// <summary>
        /// Adds the child.
        /// </summary>
        /// <param name="child">The child.</param>
        /// <param name="preserveWorldPos">if set to <c>true</c> [preserve absolute position].</param>
        public void AddChild(IMyEntity child, bool preserveWorldPos = false, bool insertIntoSceneIfNeeded = true)
        {
            //MyEntities.Remove(child);  // if it's already in the world, remove it

            MyHierarchyComponentBase childHierarchy = child.Components.Get <MyHierarchyComponentBase>();

            childHierarchy.Parent = this;

            if (preserveWorldPos)
            {
                var tmpWorldMatrix = child.WorldMatrix;

                this.Children.Add(childHierarchy);

                child.WorldMatrix = tmpWorldMatrix;
            }
            else
            {
                this.Children.Add(childHierarchy);

                MyPositionComponentBase positionComponent      = Container.Get <MyPositionComponentBase>();
                MyPositionComponentBase childPositionComponent = child.Components.Get <MyPositionComponentBase>();

                var m = positionComponent.WorldMatrix;
                childPositionComponent.UpdateWorldMatrix(ref m);
            }

            if (Container.Entity.InScene && !child.InScene && insertIntoSceneIfNeeded)
            {
                child.OnAddedToScene(Container.Entity);
            }
        }
 void Container_ComponentAdded(Type arg1, MyEntityComponentBase arg2)
 {
     if (typeof(MyHierarchyComponentBase).IsAssignableFrom(arg1))
     {
         m_parent = arg2 as MyHierarchyComponentBase;
     }
 }
 void Container_ComponentRemoved(Type arg1, MyEntityComponentBase arg2)
 {
     if (arg2 == m_parent)
     {
         m_parent = null;
     }
 }
        /// <summary>
        /// Return top most parent of this entity
        /// </summary>
        /// <returns></returns>
        public MyHierarchyComponentBase GetTopMostParent(Type type = null)
        {
            MyHierarchyComponentBase parent = this;

            while (parent.Parent != null && (type == null || !parent.Container.Contains(type)))
            {
                parent = parent.Parent;
            }

            return(parent);
        }
        public override void OnBeforeRemovedFromContainer()
        {
            if (m_parentContainer != null)
            {
                m_parentContainer.ComponentAdded   -= Container_ComponentAdded;
                m_parentContainer.ComponentRemoved -= Container_ComponentRemoved;
                m_parentContainer = null;
            }

            m_parent = null;

            base.OnBeforeRemovedFromContainer();
        }
        public void AddChildWithMatrix(IMyEntity child, ref Matrix childLocalMatrix, bool insertIntoSceneIfNeeded = true)
        {
            MyHierarchyComponentBase childHierarchy = child.Components.Get <MyHierarchyComponentBase>();

            childHierarchy.Parent = this;

            Children.Add(childHierarchy);
            child.WorldMatrix = (MatrixD)childLocalMatrix * Container.Get <MyPositionComponentBase>().WorldMatrix;

            if (Container.Entity.InScene && !child.InScene && insertIntoSceneIfNeeded)
            {
                child.OnAddedToScene(this);
            }
        }
        public void UpdateRenderObject(bool visible)
        {
            if (!Container.Entity.InScene && visible)
            {
                return;
            }
            MyHierarchyComponentBase hierarchyComponent = Container.Get <MyHierarchyComponentBase>();

            if (visible)
            {
                if (hierarchyComponent != null)
                {
                    if (Visible && (hierarchyComponent.Parent == null || hierarchyComponent.Parent.Container.Entity.Visible))
                    {
                        if (CanBeAddedToRender())
                        {
                            if (!IsRenderObjectAssigned(0))
                            {
                                AddRenderObjects();
                            }
                            else
                            {
                                UpdateRenderObjectVisibility(visible);
                            }
                        }
                    }
                }
            }
            else
            {
                if (m_renderObjectIDs[0] != VRageRender.MyRenderProxy.RENDER_ID_UNASSIGNED)
                {
                    UpdateRenderObjectVisibility(visible);
                }
                RemoveRenderObjects();
            }

            if (hierarchyComponent != null)
            {
                foreach (var child in hierarchyComponent.Children)
                {
                    MyRenderComponentBase renderComponent = null;
                    if (child.Container.TryGet(out renderComponent))
                    {
                        renderComponent.UpdateRenderObject(visible);
                    }
                }
            }
        }
        private void UpdateRenderObjectVisibilityIncludingChildren(bool visible)
        {
            UpdateRenderObjectVisibility(visible);

            MyHierarchyComponentBase hierarchy = Container.Get <MyHierarchyComponentBase>();

            foreach (var child in hierarchy.Children)
            {
                MyRenderComponentBase renderComponent = null;
                if (child.Container.Entity.InScene && child.Container.TryGet(out renderComponent))
                {
                    renderComponent.UpdateRenderObjectVisibilityIncludingChildren(visible);
                }
            }
        }
        /// <summary>
        /// Adds the child.
        /// </summary>
        /// <param name="child">The child.</param>
        /// <param name="preserveWorldPos">if set to <c>true</c> [preserve absolute position].</param>
        public void RemoveChild(IMyEntity child, bool preserveWorldPos = false)
        {
            MyHierarchyComponentBase childHierarchy = child.Components.Get <MyHierarchyComponentBase>();

            if (preserveWorldPos)
            {
                var tmpWorldMatrix = child.WorldMatrix;

                this.Children.Remove(childHierarchy);

                child.WorldMatrix = tmpWorldMatrix;
            }
            else
            {
                this.Children.Remove(childHierarchy);
            }

            childHierarchy.Parent = null;

            if (child.InScene)
            {
                child.OnRemovedFromScene(this);
            }
        }