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);
                            }
                        }
                    }
                }
            }
        }
        private void characterEntity_MovementStateChanged(IMyCharacter currentcharacter, MyCharacterMovementEnum oldState, MyCharacterMovementEnum newState)
        {
            if (skipNextActivation > 0)
            {
                skipNextActivation--;
            }
            if (oldState == MyCharacterMovementEnum.Sitting)
            {
            }
            if (newState == MyCharacterMovementEnum.Died)
            {
                characterEntity.MovementStateChanged -= characterEntity_MovementStateChanged;
                handlerAdded       = false;
                skipNextActivation = 0;
            }

            if ((oldState == MyCharacterMovementEnum.Flying) || (newState == MyCharacterMovementEnum.Flying))
            {
                MyEntityComponentContainer  playercontainer = characterEntity.Components;
                MyCharacterJetpackComponent JetpackComp     = playercontainer.Get <MyCharacterJetpackComponent>();
                if (JetpackComp != null)
                {
                    if ((oldState == MyCharacterMovementEnum.Flying) && (newState == MyCharacterMovementEnum.Sitting))
                    {
                        skipNextActivation = activationskippingdelay;
                    }
                    else
                    {
                        if (!MyAPIGateway.Input.IsNewGameControlPressed(MyControlsSpace.THRUSTS))
                        {
                            if (JetpackComp.TurnedOn)
                            {
                                if (skipNextActivation > 0)
                                {
                                    skipNextActivation = 0;
                                }
                                else
                                {
                                    JetpackComp.TurnOnJetpack(false); //turn the jetpack off
                                }
                            }
                        }
                    }
                }
            }
            //  if ((newState != MyCharacterMovementEnum.Sitting) || (oldState != MyCharacterMovementEnum.Sitting)) skipNextActivation = false;
        }
        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 characterEntity_OnMovementStateChanged(MyCharacterMovementEnum oldState, MyCharacterMovementEnum newState)
        {
            if (oldState == MyCharacterMovementEnum.Sitting)
            {
            }

            if ((oldState == MyCharacterMovementEnum.Flying) || (newState == MyCharacterMovementEnum.Flying))
            {
                MyEntityComponentContainer  playercontainer = characterEntity.Components;
                MyCharacterJetpackComponent JetpackComp     = playercontainer.Get <MyCharacterJetpackComponent>();
                if (JetpackComp != null)
                {
                    if ((oldState == MyCharacterMovementEnum.Flying) && (newState == MyCharacterMovementEnum.Sitting))
                    {
                        skipNextActivation = true;
                    }
                    else
                    {
                        if (!MyAPIGateway.Input.IsNewGameControlPressed(MyControlsSpace.THRUSTS))
                        {
                            if (JetpackComp.TurnedOn)
                            {
                                if (skipNextActivation)
                                {
                                    skipNextActivation = false;
                                }
                                else
                                {
                                    JetpackComp.TurnOnJetpack(false); //turn the jetpack off
                                }
                            }
                        }
                    }
                }
            }
        }
 public override void UpdateAfterSimulation()
 {
     try
     {
         tick++;
         if (MyAPIGateway.Session == null)
         {
             return;
         }
         if (!init) //reinit just to be save
         {
             Init();
         }
         if (isDedicatedHost)
         {
             return;        //there is no jetpack or even a player on the dedicated host itself
         }
         if (tick % 3 == 0) //better performance if the code is executed less - only do it every 3 ticks will work until you play below 4 FPS
         {
             if ((skipNextActivation > 0) && (skipNextActivation < activationskippingdelay))
             {
                 skipNextActivation--;
             }
             var camera = MyAPIGateway.Session.CameraController;
             if (camera == null)
             {
                 return;
             }
             if (characterEntity != null && (characterEntity.MarkedForClose || characterEntity.Closed))
             {
                 characterEntity = null; //remove the stored character
             }
             if (camera is IMyCharacter)
             {
                 if (camera as IMyCharacter != characterEntity)
                 {
                     if (characterEntity != null)
                     {
                         characterEntity.MovementStateChanged -= characterEntity_MovementStateChanged;
                     }
                     handlerAdded = false;
                 }
                 characterEntity = ((IMyCharacter)camera);
                 MyEntityComponentContainer  playercontainer = ((IMyCharacter)camera).Components;
                 MyCharacterJetpackComponent JetpackComp     = playercontainer.Get <MyCharacterJetpackComponent>();
                 if (JetpackComp != null)
                 {
                     if (JetpackComp.CurrentAutoEnableDelay > 0)  //The coundown is running so deactivate it
                     {
                         JetpackComp.CurrentAutoEnableDelay = -1; //now it is deactivated again
                     }
                 }
             }
         }
         if (!handlerAdded) //only once
         {
             var camera = MyAPIGateway.Session.CameraController;
             if (camera == null)
             {
                 return;
             }
             if (camera is IMyCharacter)
             {
                 characterEntity = ((IMyCharacter)camera);                                     //store the character Entity
                 characterEntity.MovementStateChanged += characterEntity_MovementStateChanged; //add the handler
                 handlerAdded       = true;
                 skipNextActivation = activationskippingdelay;
             }
         }
     }
     catch (Exception e)
     {
         //Lazy lazy... still no logging...
     }
 }
 public static T GetAny <T>(this MyEntityComponentContainer container) where T : MyGameLogicComponent
 {
     return(container.Get <T>() ??
            (container.Get <MyGameLogicComponent>() ?? container.Get <MyCompositeGameLogicComponent>())
            ?.GetAs <T>());
 }