示例#1
0
 public override void OnAddedToScene()
 {
     base.OnAddedToScene();
     if (!MyMultiplayerModApi.Static.IsServer)
     {
         return;
     }
     _powerObserver.OnAddedToContainer(Container);
     _inventoryObserver.OnAddedToContainer(Container);
     _positionComponent.OnPositionChanged += OnPositionChanged;
     if (_modelComponent != null)
     {
         _modelComponent.ModelChanged += OnModelChanged;
     }
     if (_modelAttachmentComponent != null)
     {
         _modelAttachmentComponent.OnEntityAttached += OnEntityAttached;
         _modelAttachmentComponent.OnEntityDetached += OnEntityDetached;
         foreach (var entry in Definition.Dummies)
         {
             foreach (var attached in _modelAttachmentComponent.GetAttachedEntities(entry.Key))
             {
                 OnEntityAttached(_modelAttachmentComponent, attached);
             }
         }
     }
 }
示例#2
0
        public override void OnAddedToScene()
        {
            base.OnAddedToScene();
            _graph = MySession.Static.Components.Get <BendyController>()?.GetOrCreateLayer(Definition.Layer);
            if (_attacher != null)
            {
                _attacher.OnEntityAttached += FixupSkinEntity;
                foreach (var e in _attacher.GetAttachedEntities(SkinHash))
                {
                    FixupSkinEntity(_attacher, e);
                }
            }

            _blockComponent.Move += BlockMoved;
            BlockMoved(_blockComponent, _blockComponent.GridData);
            _powerObserver.RequiredPower = Definition.NeedsPower;
        }
        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 SetAnimVar(MyStringId key, float val)
        {
            if (_attacher != null)
            {
                foreach (var e in _attacher.GetAttachedEntities(SkinHash))
                {
                    e.Components.Get <MyAnimationControllerComponent>()?.Variables.SetValue(key, val);
                }
            }
            Entity.Components.Get <MyAnimationControllerComponent>()?.Variables.SetValue(key, val);

            MyAnimationControllerComponent acc;

            foreach (var k in _activeControllers)
            {
                if (k.Components.TryGet(out acc))
                {
                    acc.Variables.SetValue(key, val);
                }
            }
        }
        private void Animate()
        {
            const float eps = 1e-3f;

            if (_controller?.Target == null || _modelAttachment == null)
            {
                return;
            }

            var warp = MySession.Static.ElapsedGameTime < _animationWarpEndTime;

            var cotangent = Vector3.Cross(_controller.Junction.Up, _controller.Junction.Tangent);

            cotangent.Normalize();

            var canAmplify = true;

            if (_controller.Candidates.Count > 3)
            {
                canAmplify = false;
            }
            else
            {
                var hasPositive = false;
                var hasNegative = false;
                foreach (var candidate in _controller.Candidates)
                {
                    var dest     = candidate.Opposition(_controller.Junction);
                    var coTanDot = (dest.Position - _controller.Junction.Position).Dot(cotangent);
                    if (coTanDot < -eps)
                    {
                        if (hasNegative)
                        {
                            canAmplify = false;
                            break;
                        }

                        hasNegative = true;
                    }
                    else if (coTanDot > eps)
                    {
                        if (hasPositive)
                        {
                            canAmplify = false;
                            break;
                        }

                        hasPositive = true;
                    }
                }
            }

            var direction = _controller.Target.Position - _controller.Junction.Position;

            direction.Normalize();

            var dirty = false;

            foreach (var animator in Definition.Animators)
            {
                var animatorDirection = direction;

                if (canAmplify)
                {
                    var proj = animatorDirection.Dot(cotangent);
                    animatorDirection += proj * animator.Amplify * Vector3D.Distance(_controller.Target.Position, _controller.Junction.Position) *
                                         (Vector3D)cotangent;
                    animatorDirection.Normalize();
                }

                _modelAttachment.SetAttachmentPointManagedState(animator.Attachment, false);
                foreach (var attached in _modelAttachment.GetAttachedEntities(animator.Attachment))
                {
                    var localDirection = Vector3.TransformNormal(animatorDirection, attached.PositionComp.WorldMatrixNormalizedInv);
                    localDirection = Vector3.Reject(localDirection, animator.Axis);
                    if (localDirection.Normalize() <= eps)
                    {
                        continue;
                    }
                    var cross       = Vector3.Cross(animator.Arrow, localDirection);
                    var crossLength = cross.Normalize();
                    if (crossLength <= eps)
                    {
                        continue;
                    }
                    var modAngle = Math.Min(warp ? float.MaxValue : animator.RadiansPerSec * MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS,
                                            (float)Math.Abs(Math.Asin(crossLength)));
                    if (modAngle <= eps)
                    {
                        continue;
                    }
                    dirty = true;
                    var modMatrix = Matrix.CreateFromAxisAngle(cross, modAngle);
                    attached.PositionComp.LocalMatrix = modMatrix * attached.PositionComp.LocalMatrix;
                }
            }

            if (!dirty)
            {
                StopAnimating();
            }
        }
        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();
        }