void Container_ComponentAdded(Type arg1, MyEntityComponentBase arg2)
 {
     if (typeof(MyHierarchyComponentBase).IsAssignableFrom(arg1))
     {
         m_parent = arg2 as MyHierarchyComponentBase;
     }
 }
        /// <summary>
        /// When component is removed, clean it's records
        /// </summary>
        /// <param name="component">component being removed from its container (entity) </param>
        private static void RegisteredComponentBeforeRemovedFromContainer(MyEntityComponentBase component)
        {
            component.BeforeRemovedFromContainer -= RegisteredComponentBeforeRemovedFromContainer;

            if (component.Entity == null)
            {
                return;
            }

            if (RegisteredListeners.ContainsKey(component.Entity.EntityId))
            {
                m_tmpList.Clear();

                foreach (var entry in RegisteredListeners[component.Entity.EntityId])
                {
                    entry.Value.RemoveAll(x => x.Component == component);
                }
            }

            if (ExternalListeners.ContainsKey(component))
            {
                foreach (var externalEntityId in ExternalListeners[component])
                {
                    if (RegisteredListeners.ContainsKey(externalEntityId))
                    {
                        foreach (var entry in RegisteredListeners[externalEntityId])
                        {
                            entry.Value.RemoveAll(x => x.Component == component);
                        }
                    }
                }

                ExternalListeners.Remove(component);
            }
        }
        /// <summary>
        /// This will register the component to listen to some events on entity that is other than entity containing this component
        /// </summary>
        /// <param name="entity">Entity on which we listen to events</param>
        /// <param name="component">Component that is being registered</param>
        /// <param name="eventType">type of event</param>
        /// <param name="handler">handler to be called</param>
        public static void RegisterForEntityEvent(this MyEntityComponentBase component, MyEntity entity, MyStringHash eventType, EntityEventHandler handler)
        {
            if (ProcessingEvents)
            {
                AddPostponedRegistration(component, entity, eventType, handler);
                return;
            }

            if (component.Entity == entity)
            {
                RegisterForEntityEvent(component, eventType, handler);
                return;
            }

            if (entity == null)
            {
                System.Diagnostics.Debug.Fail("You can't register this component for events, when you don't know on which entity it should be listening to..");
                return;
            }

            component.BeforeRemovedFromContainer += RegisteredComponentBeforeRemovedFromContainer;
            entity.OnClose += RegisteredEntityOnClose;

            if (RegisteredListeners.ContainsKey(entity.EntityId))
            {
                var registeredForEntity = RegisteredListeners[entity.EntityId];
                if (registeredForEntity.ContainsKey(eventType))
                {
                    var entry = registeredForEntity[eventType].Find(x => x.Handler == handler);
                    if (entry == null)
                    {
                        registeredForEntity[eventType].Add(new RegisteredComponent(component, handler));
                    }
                }
                else
                {
                    registeredForEntity[eventType] = new List <RegisteredComponent>();
                    registeredForEntity[eventType].Add(new RegisteredComponent(component, handler));
                }
            }
            else
            {
                RegisteredListeners[entity.EntityId] = new RegisteredEvents(eventType, component, handler);
            }


            if (ExternalListeners.ContainsKey(component) && !ExternalListeners[component].Contains(entity.EntityId))
            {
                ExternalListeners[component].Add(entity.EntityId);
            }
            else
            {
                ExternalListeners[component] = new List <long>()
                {
                    entity.EntityId
                };
            }

            ExternalyListenedEntities.Add(entity.EntityId);
        }
Exemplo n.º 4
0
 void Container_ComponentRemoved(Type compType, MyEntityComponentBase component)
 {
     if (compType == typeof(MyUseObjectsComponentBase))
     {
         m_useObjectComp = null;
     }
 }
Exemplo n.º 5
0
 void Container_ComponentAdded(Type compType, MyEntityComponentBase component)
 {
     if (compType == typeof(MyUseObjectsComponentBase))
     {
         m_useObjectComp = component as MyUseObjectsComponentBase;
     }
 }
 void Container_ComponentAdded(Type arg1, MyEntityComponentBase arg2)
 {
     if (typeof(MyHierarchyComponentBase).IsAssignableFrom(arg1))
     {
         m_parent = arg2 as MyHierarchyComponentBase;
     }
 }
 void Container_ComponentRemoved(Type arg1, MyEntityComponentBase arg2)
 {
     if (arg2 == m_parent)
     {
         m_parent = null;
     }
 }
 void container_ComponentAdded(Type type, MyEntityComponentBase comp)
 {
     if (type == typeof(MySyncComponentBase))
         m_syncObject = comp as MySyncComponentBase;
     else if (type == typeof(MyPhysicsComponentBase))
         m_physics = comp as MyPhysicsComponentBase;
     else if (type == typeof(MyHierarchyComponentBase))
         m_hierarchy = comp as MyHierarchyComponentBase;
 }
 void container_ComponentRemoved(Type type, MyEntityComponentBase comp)
 {
     if (type == typeof(MySyncComponentBase))
         m_syncObject = null;
     else if (type == typeof(MyPhysicsComponentBase))
         m_physics = null;
     else if (type == typeof(MyHierarchyComponentBase))
         m_hierarchy = null;
 }
        /// <summary>
        /// This will unregister the component to listen to some events on entity that is other than entity containing this component
        /// </summary>
        /// <param name="entity">Entity on which we listen to events</param>
        /// <param name="component">Component that is being registered</param>
        /// <param name="eventType">type of event</param>
        /// <param name="handler">handler to be called</param>
        public static void UnregisterForEntityEvent(this MyEntityComponentBase component, MyEntity entity, MyStringHash eventType)
        {
            if (ProcessingEvents)
            {
                AddPostponedUnregistration(component, entity, eventType);
                return;
            }

            if (entity == null)
            {
                System.Diagnostics.Debug.Fail("You can't register this component for events, when you don't know on which entity it should be listening to..");
                return;
            }

            // TODO: Unregister events for components, or even bettter rework these EntityEvents to be used without event handlers..
            //bool componentIsRegistered = true;
            bool entityIsRegistered = true;

            if (RegisteredListeners.ContainsKey(entity.EntityId))
            {
                if (RegisteredListeners[entity.EntityId].ContainsKey(eventType))
                {
                    RegisteredListeners[entity.EntityId][eventType].RemoveAll(x => x.Component == component);
                    if (RegisteredListeners[entity.EntityId][eventType].Count == 0)
                    {
                        RegisteredListeners[entity.EntityId].Remove(eventType);
                    }
                }

                if (RegisteredListeners[entity.EntityId].Count == 0)
                {
                    RegisteredListeners.Remove(entity.EntityId);
                    ExternalyListenedEntities.Remove(entity.EntityId);
                    entityIsRegistered = false;
                }
            }


            if (ExternalListeners.ContainsKey(component) && ExternalListeners[component].Contains(entity.EntityId))
            {
                ExternalListeners[component].Remove(entity.EntityId);
                if (ExternalListeners[component].Count == 0)
                {
                    ExternalListeners.Remove(component);
                }
            }

            if (!entityIsRegistered)
            {
                entity.OnClose -= RegisteredEntityOnClose;
            }

            //if (!componentIsRegistered)
            //{
            //    component.BeforeRemovedFromContainer -= RegisteredComponentBeforeRemovedFromContainer;
            //}
        }
        /// <summary>
        /// Call this to raise event on entity, that will be processed by registered components
        /// </summary>
        /// <param name="component">component upon which container this is going to be invoke</param>
        /// <param name="eventType">type of event</param>
        /// <param name="eventParams">event params or derived type</param>
        public static void RaiseEntityEvent(this MyEntityComponentBase component, MyStringHash eventType, EntityEventParams eventParams)
        {
            if (component.Entity == null)   // this component is raising event, but it's entity don't exists..
            {
                return;
            }

            long entityId = component.Entity.EntityId;

            InvokeEventOnListeners(entityId, eventType, eventParams);
        }
Exemplo n.º 12
0
 void container_ComponentRemoved(Type type, MyEntityComponentBase comp)
 {
     if (type == typeof(MySyncComponentBase))
     {
         m_syncObject = null;
     }
     else if (type == typeof(MyPhysicsComponentBase))
     {
         m_physics = null;
     }
     else if (type == typeof(MyHierarchyComponentBase))
     {
         m_hierarchy = null;
     }
 }
Exemplo n.º 13
0
 void container_ComponentAdded(Type type, MyEntityComponentBase comp)
 {
     if (type == typeof(MySyncComponentBase))
     {
         m_syncObject = comp as MySyncComponentBase;
     }
     else if (type == typeof(MyPhysicsComponentBase))
     {
         m_physics = comp as MyPhysicsComponentBase;
     }
     else if (type == typeof(MyHierarchyComponentBase))
     {
         m_hierarchy = comp as MyHierarchyComponentBase;
     }
 }
Exemplo n.º 14
0
        private void OnComponentAdded(Type type, VRage.Game.Components.MyEntityComponentBase component)
        {
            var aggregate = component as MyInventoryAggregate;

            if (aggregate != null)
            {
                m_inventoryAggregate = aggregate;
                m_inventoryAggregate.BeforeRemovedFromContainer += OnInventoryAggregateRemoved;
                m_inventoryAggregate.OnAfterComponentAdd        += OnInventoryAddedToAggregate;
                m_inventoryAggregate.OnBeforeComponentRemove    += OnBeforeInventoryRemovedFromAggregate;

                foreach (var inventory in m_inventoryAggregate.ChildList.Reader)
                {
                    MyInventory inv = inventory as MyInventory;
                    OnInventoryAddedToAggregate(aggregate, inv);
                }
            }
        }
Exemplo n.º 15
0
 private void inventory_OnRemovedFromContainer(MyEntityComponentBase component)
 {
     Debug.Assert(this.GetInventory().Entity == this, "Inventory is not longer owned by this character !");
     this.GetInventory().BeforeRemovedFromContainer -= inventory_OnRemovedFromContainer;
     this.GetInventory().ContentsChanged -= inventory_OnContentsChanged;
     this.GetInventory().BeforeContentsChanged -= inventory_OnBeforeContentsChanged;
 }
 private static void AddPostponedUnregistration(MyEntityComponentBase component, MyEntity entity, MyStringHash eventType)
 {
     PostponedUnregistration.Add(new Tuple <MyEntityComponentBase, MyEntity, MyStringHash>(component, entity, eventType));
     HasPostponedOperations = true;
 }
 private static void AddPostponedRegistration(MyEntityComponentBase component, MyEntity entity, MyStringHash eventType, EntityEventHandler handler)
 {
     PostponedRegistration.Add(new Tuple <MyEntityComponentBase, MyEntity, MyStringHash, EntityEventHandler>(component, entity, eventType, handler));
     HasPostponedOperations = true;
 }
 void Container_ComponentRemoved(Type arg1, MyEntityComponentBase arg2)
 {
     if (arg2 == m_parent)
         m_parent = null;
 }
 void OnNewComponentAdded(Type type, MyEntityComponentBase component)
 {
     if (component is MyInventory)
     {
         InitBlock();
     }
 }
Exemplo n.º 20
0
 void Container_ComponentRemoved(Type compType, MyEntityComponentBase component)
 {
     if (compType == typeof(MyUseObjectsComponentBase))
         m_useObjectComp = null;
 }
Exemplo n.º 21
0
 void Components_ComponentAdded(Type t, MyEntityComponentBase c)
 {
     // TODO: this has to be refactored because it is very dangerous - each component member set here can be overwritten with new one of the same base type
     // (assume more than one GameLogicComponent), components should support aggregates (parameter can be added to MyComponentTypeAttribute).
     if ((typeof(MyPhysicsComponentBase)).IsAssignableFrom(t))
         m_physics = c as MyPhysicsComponentBase;
     else if ((typeof(MySyncComponentBase)).IsAssignableFrom(t))
         m_syncObject = c as MySyncComponentBase;
     else if ((typeof(MyGameLogicComponent)).IsAssignableFrom(t))
         m_gameLogic = c as MyGameLogicComponent;
     else if ((typeof(MyPositionComponentBase)).IsAssignableFrom(t))
     {
         m_position = c as MyPositionComponentBase;
         if (m_position == null)
             PositionComp = new VRage.Game.Components.MyNullPositionComponent();
     }
     else if ((typeof(MyHierarchyComponentBase)).IsAssignableFrom(t))
         m_hierarchy = c as MyHierarchyComponent<MyEntity>;
     else if ((typeof(MyRenderComponentBase)).IsAssignableFrom(t))
     {
         m_render = c as MyRenderComponentBase;
         if (m_render == null)
             Render = new VRage.Game.Components.MyNullRenderComponent();
     }
     else if ((typeof(MyInventoryBase)).IsAssignableFrom(t))
     {
         OnInventoryComponentAdded(c as MyInventoryBase);
     }
 }
Exemplo n.º 22
0
 private void OnInventoryAggregateRemoved(MyEntityComponentBase component)
 {
     m_inputInventory = null;
     m_outputInventory = null;
     m_inventoryAggregate.BeforeRemovedFromContainer -= OnInventoryAggregateRemoved;
     m_inventoryAggregate.OnAfterComponentAdd -= OnInventoryAddedToAggregate;
     m_inventoryAggregate.OnBeforeComponentRemove -= OnBeforeInventoryRemovedFromAggregate;
     m_inventoryAggregate = null;
 }   
Exemplo n.º 23
0
 void OnComponentAdded(System.Type arg1, MyEntityComponentBase arg2)
 {
     if (arg1 == typeof(MyCasterComponent))
     {
         raycaster = arg2 as MyCasterComponent;
         this.PositionComp.OnPositionChanged += OnPositionChanged;
     }
 }
Exemplo n.º 24
0
 void OnComponentRemoved(System.Type arg1, MyEntityComponentBase arg2)
 {
     if (arg1 == typeof(MyCasterComponent))
     {
         raycaster = null;
         this.PositionComp.OnPositionChanged -= OnPositionChanged;
     }
 }
Exemplo n.º 25
0
 void Components_ComponentRemoved(Type t, MyEntityComponentBase c)
 {
     // TODO: see comment at Components_ComponentAdded
     if ((typeof(MyPhysicsComponentBase)).IsAssignableFrom(t))
         m_physics = null;
     else if ((typeof(MySyncComponentBase)).IsAssignableFrom(t))
         m_syncObject = null;
     else if ((typeof(MyGameLogicComponent)).IsAssignableFrom(t))
         m_gameLogic = null;
     else if ((typeof(MyPositionComponentBase)).IsAssignableFrom(t))
         PositionComp = new VRage.Game.Components.MyNullPositionComponent();
     else if ((typeof(MyHierarchyComponentBase)).IsAssignableFrom(t))
         m_hierarchy = null;
     else if ((typeof(MyRenderComponentBase)).IsAssignableFrom(t))
     {
         Render = new VRage.Game.Components.MyNullRenderComponent();
     }
     else if ((typeof(MyInventoryBase)).IsAssignableFrom(t))
     {
         OnInventoryComponentRemoved(c as MyInventoryBase);
     }
 }
Exemplo n.º 26
0
 void Container_ComponentAdded(Type compType, MyEntityComponentBase component)
 {
     if (compType == typeof(MyUseObjectsComponentBase))
         m_useObjectComp = component as MyUseObjectsComponentBase;
 }