Example #1
0
        private void OnEntityAttached(MyModelAttachmentComponent component, MyEntity entity)
        {
            if (!Definition.Dummies.ContainsKey(component.GetEntityAttachmentPoint(entity)))
            {
                return;
            }
            entity.PositionComp.OnPositionChanged += OnPositionChanged;
            var model = entity.Get <MyModelComponent>();

            if (model != null)
            {
                model.ModelChanged += OnModelChanged;
            }
        }
        private void FixupSkinEntity(MyModelAttachmentComponent attacher, MyEntity entity)
        {
            var pt = attacher.GetEntityAttachmentPoint(entity);

            if (pt != SkinHash)
            {
                return;
            }

            if (!entity.Components.Contains <MyAnimationControllerComponent>())
            {
                return;
            }

            // Inflate local AABB to fix bad lodding.
            var pos = entity.PositionComp;

            pos.LocalAABB = BoundingBox.CreateFromHalfExtent(pos.LocalAABB.Center, pos.LocalAABB.HalfExtents * 5);
        }
        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);
                            }
                        }
                    }
                }
            }
        }
        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;
        }