예제 #1
0
 public override void OnAddedToScene()
 {
     base.OnAddedToScene();
     _hierarchy = Entity.Hierarchy;
     _hierarchy.ParentChanged += OnParentChanged;
     OnParentChanged(_hierarchy, null, _hierarchy.Parent);
 }
        private void ParentChanged(MyHierarchyComponent target, MyHierarchyComponent oldParent, MyHierarchyComponent newParent)
        {
            if (_parent == newParent)
            {
                return;
            }
            if (_parent != null)
            {
                var phys = _parent.Container.Get <MyPhysicsComponentBase>();
                if (phys != null)
                {
                    phys.OnPhysicsChanged -= OnPhysicsChanged;
                    phys.LinearDamping     = DefaultLinearDamping;
                    phys.AngularDamping    = DefaultAngularDamping;
                }
            }

            _parent = newParent;
            if (_parent != null)
            {
                var phys = _parent.Container.Get <MyPhysicsComponentBase>();
                if (phys != null)
                {
                    phys.OnPhysicsChanged += OnPhysicsChanged;
                    OnPhysicsChanged(phys);
                }
            }
        }
예제 #3
0
 public override void OnRemovedFromScene()
 {
     base.OnRemovedFromScene();
     OnParentChanged(_hierarchy, _hierarchy.Parent, null);
     _hierarchy.ParentChanged -= OnParentChanged;
     _hierarchy = null;
 }
예제 #4
0
        public override void Close()
        {
            ((IMyGameLogicComponent)this.GameLogic).Close();
            MyHierarchyComponent <MyEntity> hierarchy = this.m_entity.Hierarchy;

            while (true)
            {
                if (hierarchy != null)
                {
                    ListReader <MyHierarchyComponentBase> children = hierarchy.Children;
                    if (children.Count > 0)
                    {
                        MyHierarchyComponentBase childHierarchy = hierarchy.Children[hierarchy.Children.Count - 1];
                        childHierarchy.Container.Entity.Close();
                        hierarchy.RemoveByJN(childHierarchy);
                        continue;
                    }
                }
                this.CallAndClearOnClosing();
                MyEntities.RemoveName(this.m_entity);
                MyEntities.RemoveFromClosedEntities(this.m_entity);
                if (this.m_entity.Physics != null)
                {
                    this.m_entity.Physics.Close();
                    this.m_entity.Physics = null;
                    this.m_entity.RaisePhysicsChanged();
                }
                MyEntities.UnregisterForUpdate(this.m_entity, true);
                MyEntities.UnregisterForDraw(this.m_entity);
                if ((hierarchy == null) || (hierarchy.Parent == null))
                {
                    MyEntities.Remove(this.m_entity);
                }
                else
                {
                    this.m_entity.Parent.Hierarchy.RemoveByJN(hierarchy);
                    if (this.m_entity.Parent.InScene)
                    {
                        this.m_entity.OnRemovedFromScene(this.m_entity);
                    }
                    MyEntities.RaiseEntityRemove(this.m_entity);
                }
                if (this.m_entity.EntityId != 0)
                {
                    MyEntityIdentifier.RemoveEntity(this.m_entity.EntityId);
                }
                this.CallAndClearOnClose();
                base.Closed = true;
                return;
            }
        }
        private void ParentChanged(MyHierarchyComponent target, MyHierarchyComponent oldParent, MyHierarchyComponent newParentH)
        {
            var newParent = newParentH?.Entity;

            if (newParent == _activeParent)
            {
                return;
            }
            if (_activeParent != null)
            {
                UnwatchEntity(_activeParent);
            }

            _activeParent = newParent;
            if (_activeParent != null)
            {
                WatchEntity(newParent);
            }
        }
예제 #6
0
        private void OnParentChanged(MyHierarchyComponent target, MyHierarchyComponent oldParent, MyHierarchyComponent newParent)
        {
            var parent = _hierarchy?.Parent?.Entity?.Components.Get <MyEntityOwnershipComponent>();

            if (_parent == parent)
            {
                return;
            }
            if (_parent != null)
            {
                _parent.OwnerChanged -= ParentOwnerChanged;
            }
            if (parent != null)
            {
                parent.OwnerChanged += ParentOwnerChanged;
            }
            _parent = parent;
            ParentOwnerChanged(parent, 0, 0);
        }
        public void AddToContainer(MyEntityComponentContainer container, bool includeParent, bool includeChildren)
        {
            _includeParent   = includeParent;
            _includeChildren = includeChildren;

            _container = container;
            _container.ComponentAdded   += OnComponentAdded;
            _container.ComponentRemoved += OnComponentAdded;
            foreach (var c in _container.GetComponents <TComp>())
            {
                OnComponentAdded(c);
            }

            if (_includeParent)
            {
                _hierarchy = container.Get <MyHierarchyComponent>();
                _hierarchy.ParentChanged += ParentChanged;
                ParentChanged(_hierarchy, null, _hierarchy.Parent);
            }

            if (_includeChildren)
            {
                _modelAttachment = container.Get <MyModelAttachmentComponent>();
                if (_modelAttachment != null)
                {
                    _modelAttachment.OnEntityAttached += OnEntityAttached;
                    _modelAttachment.OnEntityDetached += OnEntityDetached;
                    var h = container.Get <MyHierarchyComponent>();
                    if (h != null)
                    {
                        foreach (var e in h.Children)
                        {
                            if (e.Entity != null && _modelAttachment.GetEntityAttachmentPoint(e.Entity) != MyStringHash.NullOrEmpty)
                            {
                                OnEntityAttached(_modelAttachment, e.Entity);
                            }
                        }
                    }
                }
            }
        }
예제 #8
0
        private void RootRefChanged(MyHierarchyComponent target, MyHierarchyComponent oldParent, MyHierarchyComponent newParent)
        {
            if (!IsAuthority)
            {
                return;
            }
            using (PoolManager.Get(out List <MyTuple <MyEntity, RailwayPhysicsLinkData> > moving))
            {
                var oldRoot = oldParent?.Entity;
                if (oldRoot != null)
                {
                    var oldGroup = _target.Scene.GetGroup <RailwayPhysicsGroup, MyEntity>(oldRoot);
                    if (oldGroup != null)
                    {
                        foreach (var neighbor in oldGroup.GetNeighbors(oldRoot))
                        {
                            foreach (var link in oldGroup.GetLinks(oldRoot, neighbor))
                            {
                                moving.Add(MyTuple.Create(neighbor, link));
                            }
                        }

                        foreach (var link in moving)
                        {
                            _target.Scene.RemoveLink <RailwayPhysicsGroup, MyEntity, RailwayPhysicsLinkData>(oldRoot, link.Item1, link.Item2);
                        }
                    }
                }

                var newRoot = newParent?.Entity;
                if (newRoot == null)
                {
                    return;
                }
                foreach (var link in moving)
                {
                    _target.Scene.AddLink <RailwayPhysicsGroup, MyEntity, RailwayPhysicsLinkData>(newRoot, link.Item1,
                                                                                                  link.Item2);
                }
            }
        }
예제 #9
0
        private void ParentChanged(MyHierarchyComponent target, MyHierarchyComponent oldParent, MyHierarchyComponent newParent)
        {
            var obj = newParent?.Entity;

            if (obj == _parentCache)
            {
                return;
            }
            if (_parentCache != null)
            {
                _parentCache.Components.ComponentAdded   -= CheckSkeleton;
                _parentCache.Components.ComponentRemoved -= CheckSkeleton;
            }

            _parentCache = obj;
            if (_parentCache != null)
            {
                _parentCache.Components.ComponentAdded   += CheckSkeleton;
                _parentCache.Components.ComponentRemoved += CheckSkeleton;
            }

            CheckSkeleton();
        }
        public void RemoveFromContainer()
        {
            if (_includeParent)
            {
                _hierarchy.ParentChanged -= ParentChanged;
                ParentChanged(_hierarchy, _hierarchy.Parent, null);
                _hierarchy = null;
            }

            if (_modelAttachment != null)
            {
                _modelAttachment.OnEntityAttached -= OnEntityAttached;
                _modelAttachment.OnEntityDetached -= OnEntityDetached;
                var h = _container.Get <MyHierarchyComponent>();
                if (h != null)
                {
                    foreach (var e in h.Children)
                    {
                        if (e.Entity != null && _modelAttachment.GetEntityAttachmentPoint(e.Entity) != MyStringHash.NullOrEmpty)
                        {
                            OnEntityDetached(_modelAttachment, e.Entity);
                        }
                    }
                }

                _modelAttachment = null;
            }

            _container.ComponentAdded   -= OnComponentAdded;
            _container.ComponentRemoved -= OnComponentAdded;
            foreach (var c in _components)
            {
                ComponentRemoved?.Invoke(c);
            }
            _components.Clear();
            _container = null;
        }
예제 #11
0
 void Components_ComponentRemoved(Type t, MyEntityComponentBase c)
 {
     // TODO: see comment at Components_ComponentAdded
     if ((typeof(MyPhysicsComponentBase)).IsAssignableFrom(t))
         m_physics = null;
     else if ((typeof(MySyncComponentBase)).IsAssignableFrom(t))
         m_syncObject = null;
     else if ((typeof(MyGameLogicComponent)).IsAssignableFrom(t))
         m_gameLogic = null;
     else if ((typeof(MyPositionComponentBase)).IsAssignableFrom(t))
         PositionComp = new VRage.Game.Components.MyNullPositionComponent();
     else if ((typeof(MyHierarchyComponentBase)).IsAssignableFrom(t))
         m_hierarchy = null;
     else if ((typeof(MyRenderComponentBase)).IsAssignableFrom(t))
     {
         Render = new VRage.Game.Components.MyNullRenderComponent();
     }
     else if ((typeof(MyInventoryBase)).IsAssignableFrom(t))
     {
         OnInventoryComponentRemoved(c as MyInventoryBase);
     }
 }
예제 #12
0
 void Components_ComponentAdded(Type t, MyEntityComponentBase c)
 {
     // TODO: this has to be refactored because it is very dangerous - each component member set here can be overwritten with new one of the same base type
     // (assume more than one GameLogicComponent), components should support aggregates (parameter can be added to MyComponentTypeAttribute).
     if ((typeof(MyPhysicsComponentBase)).IsAssignableFrom(t))
         m_physics = c as MyPhysicsComponentBase;
     else if ((typeof(MySyncComponentBase)).IsAssignableFrom(t))
         m_syncObject = c as MySyncComponentBase;
     else if ((typeof(MyGameLogicComponent)).IsAssignableFrom(t))
         m_gameLogic = c as MyGameLogicComponent;
     else if ((typeof(MyPositionComponentBase)).IsAssignableFrom(t))
     {
         m_position = c as MyPositionComponentBase;
         if (m_position == null)
             PositionComp = new VRage.Game.Components.MyNullPositionComponent();
     }
     else if ((typeof(MyHierarchyComponentBase)).IsAssignableFrom(t))
         m_hierarchy = c as MyHierarchyComponent<MyEntity>;
     else if ((typeof(MyRenderComponentBase)).IsAssignableFrom(t))
     {
         m_render = c as MyRenderComponentBase;
         if (m_render == null)
             Render = new VRage.Game.Components.MyNullRenderComponent();
     }
     else if ((typeof(MyInventoryBase)).IsAssignableFrom(t))
     {
         OnInventoryComponentAdded(c as MyInventoryBase);
     }
 }
 public RootEntityRef(MyHierarchyComponent self)
 {
     Self       = self;
     _markDirty = (target, oldParent, newParent) => RecomputeRootParent();
     RecomputeRootParent();
 }