public void RemoveTrigger(MyEntity entity, MyTriggerComponent trigger)
 {
     if (m_updateLock)
         m_removeCacheTrigger.Add(new MyTuple<MyEntity, MyTriggerComponent>(entity, trigger));
     else
         RemoveTriggerCached(entity, trigger);
 }
 public void AddTrigger(MyTriggerComponent trigger)
 {
     if (m_updateLock)
         m_addCache.Add(trigger);
     else
         AddTriggerCached(trigger);
 }
 private void AddTriggerCached(MyTriggerComponent trigger)
 {
     if (!m_triggers.ContainsKey((MyEntity)trigger.Entity))
     {
         m_triggers.Add((MyEntity)trigger.Entity, new List<MyTriggerComponent> { trigger });
     }
     else
     {
         Debug.Assert(!m_triggers[(MyEntity)trigger.Entity].Contains(trigger));
         m_triggers[(MyEntity)trigger.Entity].Add(trigger);
     }
 }
 private void RemoveTriggerCached(MyEntity entity, MyTriggerComponent trigger)
 {
     if (m_triggers.ContainsKey(entity) && m_triggers[entity].Contains(trigger))
     {
         if (m_triggers[entity].Count == 1)
         {
             m_triggers[entity].Clear();
             m_triggers.Remove(entity);
         }
         else
             m_triggers[entity].Remove(trigger);
     }
 }
 public MyEntity GetTriggerEntity(string entityName, out MyTriggerComponent foundTrigger)
 {
     foundTrigger = null;
     bool orig = m_updateLock;
     m_updateLock = true;
     foreach (var triggerList in m_triggers.Values)
     {
         foreach (var trigger in triggerList)
         {
             if (trigger is MyAreaTriggerComponent && ((MyAreaTriggerComponent)trigger).Name.Equals(entityName))
             {
                 foundTrigger = trigger;
                 return (MyEntity)trigger.Entity;
             }
                 
         }
     }
     m_updateLock = orig;
     return null;
 }
        /// <summary>
        /// Selects the closest trigger to provided position.
        /// </summary>
        /// <param name="position">Considered position.</param>
        public void SelectClosest(Vector3D position)
        {
            var minLength = double.MaxValue;

            // Change the debug draw color from previously selected to default color.
            if (SelectedTrigger != null)
                SelectedTrigger.CustomDebugColor = Color.Red;

            // Find the min length
            foreach (var trigger in m_currentQuery)
            {
                var length = (trigger.Center - position).LengthSquared();
                if (length < minLength)
                {
                    minLength = length;
                    SelectedTrigger = trigger;
                }
            }

            if (Math.Abs(minLength - double.MaxValue) < double.Epsilon)
                SelectedTrigger = null;
            
            // Change the trigger debug draw color to selected.
            if (SelectedTrigger != null)
                SelectedTrigger.CustomDebugColor = Color.Yellow;
        }
        public bool Contains(MyTriggerComponent trigger)
        {
            m_updateLock = true;

            foreach (var tuple in m_removeCacheTrigger)
            {
                if (tuple.Item2 == trigger)
                {
                    return false;
                }
            }

            if(m_addCache.Contains(trigger))
                return true;

            foreach (var triggerList in m_triggers.Values)
            {
                if (triggerList.Contains(trigger))
                    return true;
            }
            return false;
        }