public override void OnBeforeRemovedFromContainer()
 {
     PhysicsNode.RemoveFromContainer();
     _eventBus        = null;
     _modelAttachment = null;
     base.OnBeforeRemovedFromContainer();
 }
 public override void OnAddedToContainer()
 {
     base.OnAddedToContainer();
     _eventBus        = Container.Get <MyComponentEventBus>();
     _modelAttachment = Container.Get <MyModelAttachmentComponent>();
     PhysicsNode.AddToContainer();
 }
 public override void OnRemovedFromScene()
 {
     base.OnRemovedFromScene();
     if (_attacher != null)
     {
         _attacher.OnEntityAttached -= FixupSkinEntity;
         _attacher = null;
     }
 }
示例#4
0
        private void OnEntityDetached(MyModelAttachmentComponent component, MyEntity entity)
        {
            entity.PositionComp.OnPositionChanged -= OnPositionChanged;
            var model = entity.Get <MyModelComponent>();

            if (model != null)
            {
                model.ModelChanged -= OnModelChanged;
            }
        }
示例#5
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;
            }
        }
        public override void OnAddedToScene()
        {
            Graph     = MySession.Static.Components.Get <BendyController>()?.GetOrCreateLayer(Definition.Layer);
            _attacher = Container.Get <MyModelAttachmentComponent>();
            if (_attacher != null)
            {
                _attacher.OnEntityAttached += FixupSkinEntity;
                foreach (var e in _attacher.GetAttachedEntities(SkinHash))
                {
                    FixupSkinEntity(_attacher, e);
                }
            }

            base.OnAddedToScene();
            _powerObserver.RequiredPower = Definition.NeedsPower;
        }
        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;
        }
        private void Update(long dt)
        {
            _updateScheduled = false;
            if (_isFilling)
            {
                foreach (var ent in ContainedEntities)
                {
                    ConsumeEntity(ent);
                }
            }

            // rate limit floating object dumping
            var maxFloating = (MyAPIGateway.Session?.MaxFloatingObjects ?? 0) * 2 / 3;

            if (maxFloating == 0)
            {
                maxFloating = int.MaxValue;
            }
            if (_isDumping && _floatingObjects.FloatingItemCount < maxFloating)
            {
                var itemSpawned = false;
                foreach (var inv in _inventories.Components)
                {
                    var outputPosition = Definition.SpawnOffset * _positionComponent.WorldMatrix;
                    MyVisualInventoryComponent           visual    = null;
                    MyModelAttachmentComponent           attacher  = null;
                    MyVisualInventoryComponentDefinition visualDef = null;
                    if (Definition.UsePositionFromVisualInventory || Definition.UseOrientationFromVisualInventory)
                    {
                        visual    = inv.Container.Get <MyVisualInventoryComponent>();
                        attacher  = inv.Container.Get <MyModelAttachmentComponent>();
                        visualDef = ComponentDefinition <MyVisualInventoryComponentDefinition>(visual);
                    }

                    for (var slot = inv.Items.Count - 1; slot >= 0; slot--)
                    {
                        var item = inv.Items[slot];
                        if (item.Amount <= 0)
                        {
                            continue;
                        }

                        var slotOutputPosition = outputPosition;
                        if (visualDef != null && attacher != null)
                        {
#if VIS_INV_ACCESS
                            foreach (var mapping in visualDef.Mappings)
                            {
                                if (mapping.TrackedInventory == inv.InventoryId && mapping.Slot == slot)
                                {
                                    foreach (var attached in attacher.GetAttachedEntities(mapping.AttachmentPointName))
                                    {
                                        var wm = attached.WorldMatrix;
                                        if (Definition.UsePositionFromVisualInventory)
                                        {
                                            slotOutputPosition.Translation = wm.Translation;
                                        }
                                        if (Definition.UseOrientationFromVisualInventory)
                                        {
                                            var tra = slotOutputPosition.Translation;
                                            slotOutputPosition             = attached.WorldMatrix;
                                            slotOutputPosition.Translation = tra;
                                        }
                                        break;
                                    }
                                    break;
                                }
                            }
#else
                            var attacherDef = ComponentDefinition <MyModelAttachmentComponentDefinition>(attacher);
                            var found       = false;
                            var fixedKey    = $"_{slot:00}";
                            foreach (var point in attacherDef.AttachmentPoints)
                            {
                                if (point.Key.String.IndexOf("Inventory", StringComparison.OrdinalIgnoreCase) < 0 &&
                                    point.Key.String.IndexOf("Slot", StringComparison.OrdinalIgnoreCase) < 0)
                                {
                                    continue;
                                }
                                var certain = point.Key.String.EndsWith(fixedKey);
                                foreach (var attached in attacher.GetAttachedEntities(point.Key))
                                {
                                    if (attached.InScene && attached.Parent != null)
                                    {
                                        var wm = attached.WorldMatrix;
                                        if (Definition.UsePositionFromVisualInventory)
                                        {
                                            slotOutputPosition.Translation = wm.Translation;
                                        }

                                        if (Definition.UseOrientationFromVisualInventory)
                                        {
                                            var tra = slotOutputPosition.Translation;
                                            slotOutputPosition             = attached.WorldMatrix;
                                            slotOutputPosition.Translation = tra;
                                        }

                                        found = true;
                                        break;
                                    }
                                }

                                if (found && certain)
                                {
                                    break;
                                }
                            }
#endif
                        }

                        var resultPos = FindFreePlace(slotOutputPosition.Translation, 0.1f, .1f);
                        if (!resultPos.HasValue)
                        {
                            MyAPIGateway.Utilities?.ShowNotification("Blocked");
                            continue;
                        }

                        slotOutputPosition.Translation = resultPos.Value;
                        _floatingObjects.Spawn(item, slotOutputPosition, inv.Container.Get <MyPhysicsComponentBase>() ??
                                               inv.Container.Get <MyHierarchyComponent>()?.Parent?.Container
                                               .Get <MyPhysicsComponentBase>());
                        inv.Remove(item);
                        itemSpawned = true;
                        break;
                    }

                    if (itemSpawned)
                    {
                        break;
                    }
                }
            }

            CheckNeedsUpdate();
        }
 private void OnEntityAttached(MyModelAttachmentComponent modelattachmentcomponent, MyEntity entity)
 {
     WatchEntity(entity);
 }