Example #1
0
 private void RefillSuitGassesFromBottles()
 {
     foreach (GasData data in this.m_storedGases)
     {
         if (data.FillLevel >= GAS_REFILL_RATION)
         {
             data.NextGasRefill = -1;
         }
         else
         {
             if (data.NextGasRefill == -1)
             {
                 data.NextGasRefill = MySandboxGame.TotalGamePlayTimeInMilliseconds + 0x1388;
             }
             if (MySandboxGame.TotalGamePlayTimeInMilliseconds >= data.NextGasRefill)
             {
                 data.NextGasRefill = -1;
                 bool flag = false;
                 using (List <MyPhysicalInventoryItem> .Enumerator enumerator = base.Character.GetInventory(0).GetItems().GetEnumerator())
                 {
                     while (enumerator.MoveNext())
                     {
                         MyObjectBuilder_GasContainerObject content = enumerator.Current.Content as MyObjectBuilder_GasContainerObject;
                         if ((content != null) && (content.GasLevel != 0f))
                         {
                             MyOxygenContainerDefinition physicalItemDefinition = MyDefinitionManager.Static.GetPhysicalItemDefinition(content) as MyOxygenContainerDefinition;
                             if (physicalItemDefinition.StoredGasId == data.Id)
                             {
                                 float num2     = content.GasLevel * physicalItemDefinition.Capacity;
                                 float gasInput = Math.Min(num2, (1f - data.FillLevel) * data.MaxCapacity);
                                 content.GasLevel = Math.Max((float)((num2 - gasInput) / physicalItemDefinition.Capacity), (float)0f);
                                 float gasLevel = content.GasLevel;
                                 base.Character.GetInventory(0).UpdateGasAmount();
                                 flag = true;
                                 this.TransferSuitGas(ref data.Id, gasInput, 0f);
                                 if (data.FillLevel == 1f)
                                 {
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 if (flag && !ReferenceEquals(MySession.Static.LocalCharacter, base.Character))
                 {
                     base.Character.SendRefillFromBottle(data.Id);
                 }
                 MyCharacterJetpackComponent jetpackComp = base.Character.JetpackComp;
                 if (((jetpackComp != null) && (jetpackComp.TurnedOn && ((jetpackComp.FuelDefinition != null) && ((jetpackComp.FuelDefinition.Id == data.Id) && (data.FillLevel <= 0f))))) && (((base.Character.ControllerInfo.Controller != null) && !MySession.Static.CreativeToolsEnabled(base.Character.ControllerInfo.Controller.Player.Id.SteamId)) || (!ReferenceEquals(MySession.Static.LocalCharacter, base.Character) && !Sync.IsServer)))
                 {
                     if (Sync.IsServer && !ReferenceEquals(MySession.Static.LocalCharacter, base.Character))
                     {
                         MyMultiplayer.RaiseEvent <MyCharacter>(base.Character, x => new Action(x.SwitchJetpack), new EndpointId(base.Character.ControllerInfo.Controller.Player.Id.SteamId));
                     }
                     jetpackComp.SwitchThrusts();
                 }
             }
         }
     }
 }
        public override void Init(MyObjectBuilder_EntityBase objectBuilder)
        {
            SyncFlag = true;

            /// Wee need to get the character subtype, before passing init to base classes so the components can be properly initialized..
            MyObjectBuilder_Character characterOb = (MyObjectBuilder_Character)objectBuilder;

            Render.ColorMaskHsv = characterOb.ColorMaskHSV;

            Vector3 colorMask = Render.ColorMaskHsv;

            /// This will retrieve definition and set the subtype for the character
            GetModelAndDefinition(characterOb, out m_characterModel, out m_characterDefinition, ref colorMask);

            base.UseNewAnimationSystem = m_characterDefinition.UseNewAnimationSystem;
            if (UseNewAnimationSystem)
            {
                //// Create default layer.
                //AnimationController.Controller.DeleteAllLayers();
                //var animationLayer = AnimationController.Controller.CreateLayer("Body");
                //// Build an animation node for each animation subtype.
                //// VRAGE TODO: this is just temporary for testing the new animation system
                //foreach (var animationNameSubType in m_characterDefinition.AnimationNameToSubtypeName)
                //{
                //    string animSubType = animationNameSubType.Value;
                //    MyAnimationDefinition animationDefinition = null;
                //    if (animationLayer.FindNode(animSubType) == null && TryGetAnimationDefinition(animSubType, out animationDefinition))
                //    {
                //        MyModel modelAnimation = VRage.Game.Models.MyModels.GetModelOnlyAnimationData(animationDefinition.AnimationModel);
                //        if (modelAnimation != null && animationDefinition.ClipIndex < modelAnimation.Animations.Clips.Count)
                //        {
                //            VRage.Animations.MyAnimationClip clip = modelAnimation.Animations.Clips[animationDefinition.ClipIndex];
                //            var animationState = new VRage.Animations.MyAnimationStateMachineNode(animSubType, clip);
                //            animationLayer.AddNode(animationState);
                //        }
                //    }
                //}
                AnimationController.Clear();
                MyStringHash animSubtypeNameHash = MyStringHash.GetOrCompute(m_characterDefinition.AnimationController);
                MyAnimationControllerDefinition animControllerDef =
                    MyDefinitionManager.Static.GetDefinition<MyAnimationControllerDefinition>(animSubtypeNameHash);
                if (animControllerDef != null)
                {
                    AnimationController.InitFromDefinition(animControllerDef);
                }
            }

            if (Render.ColorMaskHsv != colorMask)
                // color mask is set by definition of model
                Render.ColorMaskHsv = colorMask;

            /// Set the subtype from the definition
            characterOb.SubtypeName = m_characterDefinition.Id.SubtypeName;

            base.Init(objectBuilder);

            SyncObject.MarkPhysicsDirty();

            m_currentAnimationChangeDelay = 0;

            SoundComp = new MyCharacterSoundComponent();

            RadioBroadcaster.WantsToBeEnabled = characterOb.EnableBroadcasting && Definition.VisibleOnHud;

            Init(new StringBuilder(characterOb.DisplayName), m_characterDefinition.Model, null, null);

            NeedsUpdate = MyEntityUpdateEnum.EACH_FRAME | MyEntityUpdateEnum.EACH_10TH_FRAME | MyEntityUpdateEnum.EACH_100TH_FRAME;

            PositionComp.LocalAABB = new BoundingBox(-new Vector3(0.3f, 0.0f, 0.3f), new Vector3(0.3f, 1.8f, 0.3f));

            m_currentLootingCounter = characterOb.LootingCounter;

            if (m_currentLootingCounter <= 0)
                UpdateCharacterPhysics(!characterOb.AIMode);

            m_currentMovementState = characterOb.MovementState;
            if (Physics != null && Physics.CharacterProxy != null)
            {
                switch (m_currentMovementState)
                {
                    case MyCharacterMovementEnum.Falling:
                    case MyCharacterMovementEnum.Flying:
                        Physics.CharacterProxy.SetState(HkCharacterStateType.HK_CHARACTER_IN_AIR);
                        break;
                    case MyCharacterMovementEnum.Jump:
                        Physics.CharacterProxy.SetState(HkCharacterStateType.HK_CHARACTER_JUMPING);
                        break;
                    case MyCharacterMovementEnum.Ladder:
                    case MyCharacterMovementEnum.LadderDown:
                    case MyCharacterMovementEnum.LadderUp:
                        Physics.CharacterProxy.SetState(HkCharacterStateType.HK_CHARACTER_CLIMBING);
                        break;
                    default:
                        Physics.CharacterProxy.SetState(HkCharacterStateType.HK_CHARACTER_ON_GROUND);
                        break;
                }
            }

            InitAnimations();
            ValidateBonesProperties();
            CalculateTransforms(0);

            if (m_currentLootingCounter > 0)
            {
                InitDeadBodyPhysics();
                if (m_currentMovementState != MyCharacterMovementEnum.Died) SetCurrentMovementState(MyCharacterMovementEnum.Died);
                SwitchAnimation(MyCharacterMovementEnum.Died, false);
            }

            InitInventory(characterOb);

            Physics.Enabled = true;

            SetHeadLocalXAngle(characterOb.HeadAngle.X);
            SetHeadLocalYAngle(characterOb.HeadAngle.Y);

            Render.InitLight(m_characterDefinition);
            Render.InitJetpackThrusts(m_characterDefinition);

            m_useAnimationForWeapon = MyPerGameSettings.CheckUseAnimationInsteadOfIK();

            m_lightEnabled = characterOb.LightEnabled;

            Physics.LinearVelocity = characterOb.LinearVelocity;

            if (Physics.CharacterProxy != null)
            {
                Physics.CharacterProxy.ContactPointCallbackEnabled = true;
                Physics.CharacterProxy.ContactPointCallback += RigidBody_ContactPointCallback;
            }

            Render.UpdateLightProperties(m_currentLightPower);

            // Setup first person view for local player from previous state before die.
            IsInFirstPersonView = MySession.Static.Settings.Enable3rdPersonView == false
                || (m_localCharacterWasInThirdPerson != null
                ? characterOb.IsInFirstPersonView && !m_localCharacterWasInThirdPerson.Value : characterOb.IsInFirstPersonView);

            m_breath = new MyCharacterBreath(this);

            Debug.Assert(m_currentLootingCounter <= 0 || m_currentLootingCounter > 0);

            m_broadcastingNotification = new MyHudNotification();

            m_notEnoughStatNotification = new MyHudNotification(MyCommonTexts.NotificationStatNotEnough, disappearTimeMs: 1000, font: MyFontEnum.Red, level: MyNotificationLevel.Important);

            if (InventoryAggregate != null) InventoryAggregate.Init();

            UseDamageSystem = true;

            if (characterOb.EnabledComponents == null)
            {
                characterOb.EnabledComponents = new List<string>();
            }

            foreach (var componentName in m_characterDefinition.EnabledComponents)
            {
                if (characterOb.EnabledComponents.All(x => x != componentName))
                    characterOb.EnabledComponents.Add(componentName);
            }

            foreach (var componentName in characterOb.EnabledComponents)
            {
                Tuple<Type, Type> componentType;
                if (MyCharacterComponentTypes.CharacterComponents.TryGetValue(MyStringId.GetOrCompute(componentName), out componentType))
                {
                    MyEntityComponentBase component = Activator.CreateInstance(componentType.Item1) as MyEntityComponentBase;
                    Components.Add(componentType.Item2, component);
                }
            }

            if (m_characterDefinition.UsesAtmosphereDetector)
            {
                AtmosphereDetectorComp = new MyAtmosphereDetectorComponent();
                AtmosphereDetectorComp.InitComponent(true, this);
            }

            bool hasGases = Definition.SuitResourceStorage.Count > 0;
            var sinkData = new List<MyResourceSinkInfo>();
            var sourceData = new List<MyResourceSourceInfo>();

            if (hasGases)
            {
                OxygenComponent = new MyCharacterOxygenComponent();
                Components.Add(OxygenComponent);
                OxygenComponent.Init(characterOb);
                OxygenComponent.AppendSinkData(sinkData);
                OxygenComponent.AppendSourceData(sourceData);
            }

            m_suitBattery = new MyBattery(this);
            m_suitBattery.Init(characterOb.Battery, sinkData, sourceData);

            if (hasGases)
            {
                OxygenComponent.CharacterGasSink = m_suitBattery.ResourceSink;
                OxygenComponent.CharacterGasSource = m_suitBattery.ResourceSource;
            }

            sinkData.Clear();

            sinkData.Add(
                new MyResourceSinkInfo
                {
                    ResourceTypeId = MyResourceDistributorComponent.ElectricityId,
                    MaxRequiredInput = MyEnergyConstants.REQUIRED_INPUT_LIFE_SUPPORT + MyEnergyConstants.REQUIRED_INPUT_CHARACTER_LIGHT,
                    RequiredInputFunc = ComputeRequiredPower
                });

            if (hasGases)
            {
                sinkData.Add(new MyResourceSinkInfo
                    {
                        ResourceTypeId = MyCharacterOxygenComponent.OxygenId,
                        MaxRequiredInput = (OxygenComponent.OxygenCapacity + (!OxygenComponent.NeedsOxygenFromSuit ? Definition.OxygenConsumption : 0f)) * Definition.OxygenConsumptionMultiplier * VRage.Game.MyEngineConstants.UPDATE_STEPS_PER_SECOND / 100f,
                        RequiredInputFunc = () => (OxygenComponent.HelmetEnabled ? Definition.OxygenConsumption : 0f) * Definition.OxygenConsumptionMultiplier * VRage.Game.MyEngineConstants.UPDATE_STEPS_PER_SECOND / 100f
                    });
            }

            SinkComp.Init(
                MyStringHash.GetOrCompute("Utility"),
                sinkData);
            SinkComp.CurrentInputChanged += delegate
            {
                SetPowerInput(SinkComp.CurrentInputByType(MyResourceDistributorComponent.ElectricityId));
            };
            SinkComp.TemporaryConnectedEntity = this;

            SuitRechargeDistributor = new MyResourceDistributorComponent();
            SuitRechargeDistributor.AddSource(m_suitBattery.ResourceSource);
            SuitRechargeDistributor.AddSink(SinkComp);
            SinkComp.Update();

            bool isJetpackAvailable = !MySession.Static.Battle;
            isJetpackAvailable = isJetpackAvailable && (m_characterDefinition.Jetpack != null);

            if (isJetpackAvailable)
            {
                JetpackComp = new MyCharacterJetpackComponent();
                JetpackComp.Init(characterOb);
            }

            WeaponPosition = new MyCharacterWeaponPositionComponent();
            Components.Add(WeaponPosition);
            WeaponPosition.Init(characterOb);

            InitWeapon(characterOb.HandWeapon);

            if (Definition.RagdollBonesMappings.Count > 0)
                CreateBodyCapsulesForHits(Definition.RagdollBonesMappings);
            else
                m_bodyCapsuleInfo.Clear();

            PlayCharacterAnimation(Definition.InitialAnimation, MyBlendOption.Immediate, MyFrameOption.JustFirstFrame, 0.0f);

            m_savedHealth = characterOb.Health;

            m_savedPlayer = new Sandbox.Game.World.MyPlayer.PlayerId(characterOb.PlayerSteamId, characterOb.PlayerSerialId);

            NeedsUpdate |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME; // TODO: Get rid of after after the character will be initialized properly from objectBuilder

            IsPromoted = characterOb.IsPromoted;

            m_previousLinearVelocity = characterOb.LinearVelocity;

            if (ControllerInfo.IsLocallyControlled())
            {
                m_localHeadTransform.Value = new MyTransform(Vector3.Zero);
                m_localHeadTransformTool.Value = new MyTransform(Vector3.Zero);
            }

            CheckExistingStatComponent();
        }
Example #3
0
        private void CheckChangesOnCharacter()
        {
            MyCharacter character = base.Character;

            if (MyPerGameSettings.EnableRagdollInJetpack)
            {
                if (!ReferenceEquals(character.Physics, this.m_previousPhysics))
                {
                    this.UpdateCharacterPhysics();
                    this.m_previousPhysics = character.Physics;
                }
                if (Sync.IsServer)
                {
                    goto TR_001B;
                }
                else if (character.ClosestParentId == 0)
                {
                    goto TR_001B;
                }
                else
                {
                    this.DeactivateJetpackRagdoll();
                }
            }
TR_0004:
            if ((character.IsDead && !this.IsRagdollActivated) && character.Physics.Enabled)
            {
                this.InitDeadBodyPhysics();
            }
            return;

TR_001B:
            if (!ReferenceEquals(character.CurrentWeapon, this.m_previousWeapon))
            {
                this.DeactivateJetpackRagdoll();
                this.ActivateJetpackRagdoll();
                this.m_previousWeapon = character.CurrentWeapon;
            }
            MyCharacterJetpackComponent jetpackComp          = character.JetpackComp;
            MyCharacterMovementEnum     currentMovementState = character.GetCurrentMovementState();

            if ((((jetpackComp == null) || !jetpackComp.TurnedOn) || (currentMovementState != MyCharacterMovementEnum.Flying)) && ((currentMovementState != MyCharacterMovementEnum.Falling) || !character.Physics.Enabled))
            {
                if ((this.RagdollMapper != null) && this.RagdollMapper.IsPartiallySimulated)
                {
                    this.DeactivateJetpackRagdoll();
                }
            }
            else if (!this.IsRagdollActivated || !this.RagdollMapper.IsActive)
            {
                this.DeactivateJetpackRagdoll();
                this.ActivateJetpackRagdoll();
            }
            if (this.IsRagdollActivated && (character.Physics.Ragdoll != null))
            {
                bool isDead = character.IsDead;
                using (List <HkRigidBody> .Enumerator enumerator = character.Physics.Ragdoll.RigidBodies.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        enumerator.Current.EnableDeactivation = isDead;
                    }
                }
            }
            goto TR_0004;
        }