void GetMostEffectiveToolAction(List <MyToolActionDefinition> toolActions, out MyToolActionDefinition?bestAction, out MyToolHitCondition bestCondition) { MyCharacterDetectorComponent detectorComponent = m_owner.Components.Get <MyCharacterDetectorComponent>(); IMyEntity hitEntity = null; uint shapeKey = 0; if (detectorComponent != null) { hitEntity = detectorComponent.DetectedEntity; shapeKey = detectorComponent.ShapeKey; float hitDistance = Vector3.Distance(detectorComponent.HitPosition, detectorComponent.StartPosition); if (hitDistance > m_toolItemDef.HitDistance) { hitEntity = null; } } bestAction = null; bestCondition = new MyToolHitCondition(); //Get most effective action foreach (var action in toolActions) { if (action.HitConditions != null) { foreach (var condition in action.HitConditions) { if (condition.EntityType != null) { if (hitEntity != null) { string availableState = GetStateForTarget((MyEntity)hitEntity, shapeKey, condition.Component); if (condition.EntityType.Contains(availableState)) { bestAction = action; bestCondition = condition; return; } } else { continue; } } else { bestAction = action; bestCondition = condition; return; } } } } }
void GetPreferredToolAction(List <MyToolActionDefinition> toolActions, string name, out MyToolActionDefinition?bestAction, out MyToolHitCondition bestCondition) { bestAction = null; bestCondition = new MyToolHitCondition(); MyStringId nameId = MyStringId.GetOrCompute(name); foreach (var action in toolActions) { if (action.HitConditions.Length > 0) { if (action.Name == nameId) { bestAction = action; bestCondition = action.HitConditions[0]; return; } } } }
public virtual void Shoot(MyShootActionEnum shootAction, VRageMath.Vector3 direction, string gunAction) { m_shotToolAction = null; m_wasShooting = false; m_swingSoundPlayed = false; m_isHit = false; if (!string.IsNullOrEmpty(gunAction)) { switch (shootAction) { case MyShootActionEnum.PrimaryAction: GetPreferredToolAction(m_toolItemDef.PrimaryActions, gunAction, out m_primaryToolAction, out m_primaryHitCondition); break; case MyShootActionEnum.SecondaryAction: GetPreferredToolAction(m_toolItemDef.SecondaryActions, gunAction, out m_secondaryToolAction, out m_secondaryHitCondition); break; } } switch (shootAction) { case MyShootActionEnum.PrimaryAction: m_shotToolAction = m_primaryToolAction; m_shotHitCondition = m_primaryHitCondition; break; case MyShootActionEnum.SecondaryAction: m_shotToolAction = m_secondaryToolAction; m_shotHitCondition = m_secondaryHitCondition; break; default: System.Diagnostics.Debug.Fail("Unknown shooting state!"); break; } MyTuple <ushort, MyStringHash> message; if (!string.IsNullOrEmpty(m_shotHitCondition.StatsAction) && m_owner.StatComp != null && !m_owner.StatComp.CanDoAction(m_shotHitCondition.StatsAction, out message)) { if (MySession.Static != null && MySession.Static.LocalCharacter == m_owner && message.Item1 == MyStatLogic.STAT_VALUE_TOO_LOW && message.Item2.String.CompareTo("Stamina") == 0) { m_notEnoughStatNotification.SetTextFormatArguments(message.Item2); MyHud.Notifications.Add(m_notEnoughStatNotification); } return; } if (m_shotToolAction.HasValue) { IMyHandToolComponent toolComponent; if (m_toolComponents.TryGetValue(m_shotHitCondition.Component, out toolComponent)) { toolComponent.Shoot(); } MyFrameOption frameOption = MyFrameOption.StayOnLastFrame; if (m_shotToolAction.Value.HitDuration == 0) { frameOption = MyFrameOption.JustFirstFrame; } // Stop upper character animation called because character can have some animation set (blocking, ...). m_owner.StopUpperCharacterAnimation(0.1f); m_owner.PlayCharacterAnimation(m_shotHitCondition.Animation, MyBlendOption.Immediate, frameOption, 0.2f, m_shotHitCondition.AnimationTimeScale, false, null, true); if (m_owner.StatComp != null) { if (!string.IsNullOrEmpty(m_shotHitCondition.StatsAction)) { m_owner.StatComp.DoAction(m_shotHitCondition.StatsAction); } if (!string.IsNullOrEmpty(m_shotHitCondition.StatsModifier)) { m_owner.StatComp.ApplyModifier(m_shotHitCondition.StatsModifier); } } Physics.Enabled = m_shotToolAction.Value.Name == BlockId; m_lastShot = MySandboxGame.Static.UpdateTime; } }