示例#1
0
        public static bool TryAddComponent(long entityId, string instanceTypeStr, string componentTypeStr)
        {
            MyEntity entity;
            Type     type  = null;
            Type     type2 = null;
            MyComponentDefinitionBase base3;

            try
            {
                type = Type.GetType(instanceTypeStr, true);
            }
            catch (Exception)
            {
            }
            try
            {
                type = Type.GetType(componentTypeStr, true);
            }
            catch (Exception)
            {
            }
            if (!MyEntities.TryGetEntityById(entityId, out entity, false) || (type == null))
            {
                return(false);
            }
            MyComponentBase component = MyComponentFactory.CreateInstanceByType(type);

            if ((entity.DefinitionId != null) && TryGetComponentDefinition(component.GetType(), entity.DefinitionId.Value.SubtypeId, out base3))
            {
                component.Init(base3);
            }
            entity.Components.Add(type2, component);
            return(true);
        }
示例#2
0
 void Components_ComponentAdded(Type arg1, MyComponentBase arg2)
 {
     if (arg1 == typeof(MyGameLogicComponent))
     {
         m_logic = arg2 as MyMeteorGameLogic;
     }
 }
 void Components_ComponentAdded(Type arg1, MyComponentBase arg2)
 {
     if (arg1 == typeof(MyGameLogicComponent))
     {
         m_debrisLogic = arg2 as MyDebrisBaseLogic;
     }
 }
示例#4
0
 public void BeforeComponentRemove(MyComponentBase component)
 {
     (component as MyInventoryBase).ContentsChanged -= child_OnContentsChanged;
     if (OnBeforeComponentRemove != null)
     {
         OnBeforeComponentRemove(this, component as MyInventoryBase);
     }
 }
/// <summary>
        /// Removes from list, but doesn't change ownership
        /// </summary>
        public static void DetachComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
        {
            int index = aggregate.ChildList.GetComponentIndex(component);
            if (index != -1)
            {
                aggregate.ChildList.RemoveComponentAt(index);
            }
        }
示例#6
0
 public void AfterComponentAdd(MyComponentBase component)
 {
     (component as MyInventoryBase).ContentsChanged += child_OnContentsChanged;
     if (OnAfterComponentAdd != null)
     {
         OnAfterComponentAdd(this, component as MyInventoryBase);
     }
 }
        protected override void OnComponentRemoved(Type t, MyComponentBase component)
        {
            base.OnComponentRemoved(t, component);

            var entityComponent = component as MyEntityComponentBase;

            var handler = ComponentRemoved;
            if (handler != null && entityComponent != null)
                handler(t, entityComponent);
        }
 public static void RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     int index = aggregate.ChildList.GetComponentIndex(component);
     if (index != -1)
     {
         aggregate.BeforeComponentRemove(component);
         component.SetContainer(null);
         aggregate.ChildList.RemoveComponentAt(index);
     }
 }
        protected override void OnComponentAdded(Type t, MyComponentBase component)
        {
            base.OnComponentAdded(t, component);

            var entityComponent = component as MyEntityComponentBase;
            Debug.Assert(entityComponent != null, "The component added to the entity component container was not derived from MyEntityComponentBase!");

            var handler = ComponentAdded;
            if (handler != null && entityComponent != null)
                handler(t, entityComponent);
        }
 public static void AddComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     Debug.Assert(aggregate != component, "Can not add to itself!");
     if (component.ContainerBase != null)
     {
         component.OnBeforeRemovedFromContainer();
     }
     aggregate.ChildList.AddComponent(component);
     component.SetContainer(aggregate.ContainerBase);
     aggregate.AfterComponentAdd(component);
 }
示例#11
0
 public void AfterComponentAdd(MyComponentBase component)
 {
     if (component is MyTriggerComponent)
     {
         int triggerCount = this.TriggerCount;
         this.TriggerCount = triggerCount + 1;
     }
     else if (component is MyTriggerAggregate)
     {
         (component as MyTriggerAggregate).OnTriggerCountChanged += new Action <MyTriggerAggregate, int>(this.OnChildAggregateCountChanged);
         this.TriggerCount += (component as MyTriggerAggregate).TriggerCount;
     }
 }
示例#12
0
 public void BeforeComponentRemove(MyComponentBase component)
 {
     if (component is MyTriggerComponent)
     {
         int triggerCount = this.TriggerCount;
         this.TriggerCount = triggerCount - 1;
     }
     else if (component is MyTriggerAggregate)
     {
         (component as MyTriggerAggregate).OnTriggerCountChanged -= new Action <MyTriggerAggregate, int>(this.OnChildAggregateCountChanged);
         this.TriggerCount -= (component as MyTriggerAggregate).TriggerCount;
     }
 }
示例#13
0
        private void LoadAsync(long entityId, Action <T> loadingDoneHandler)
        {
            var             savedUnderType = MyComponentTypeFactory.GetComponentType(typeof(T));
            MyEntity        entity         = null;
            MyComponentBase component      = null;

            if (!MyEntities.TryGetEntityById(entityId, out entity) ||
                !entity.Components.TryGet(savedUnderType, out component))
            {
                Debug.Fail("MyEntityComponentReplicableBase - trying to init component on an entity, but either entity or component wasn't found!");
            }

            loadingDoneHandler(component as T);
        }
示例#14
0
        public override void Deserialize(MyObjectBuilder_ComponentBase builder)
        {
            base.Deserialize(builder);
            MyObjectBuilder_InventoryAggregate aggregate = builder as MyObjectBuilder_InventoryAggregate;

            if ((aggregate != null) && (aggregate.Inventories != null))
            {
                foreach (MyObjectBuilder_InventoryBase base2 in aggregate.Inventories)
                {
                    MyComponentBase component = MyComponentFactory.CreateInstanceByTypeId(base2.TypeId);
                    component.Deserialize(base2);
                    this.AddComponent(component);
                }
            }
        }
 void container_ComponentRemoved(Type type, MyComponentBase comp)
 {
     if (type == typeof(MySyncComponentBase))
     {
         m_syncObject = null;
     }
     else if (type == typeof(MyPhysicsComponentBase))
     {
         m_physics = null;
     }
     else if (type == typeof(MyHierarchyComponentBase))
     {
         m_hierarchy = null;
     }
 }
示例#16
0
 void container_ComponentAdded(Type type, MyComponentBase 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;
     }
 }
示例#17
0
        public static MyObjectBuilder_ComponentBase CreateObjectBuilder(MyComponentBase instance)
        {
            var objectBuilder = m_objectFactory.CreateObjectBuilder<MyObjectBuilder_ComponentBase>(instance);

            //if (objectBuilder == null)
            //{
            //    var baseType = instance.GetType().BaseType;
            //    while (baseType != null && baseType != typeof(object) && objectBuilder == null)
            //    {
            //        objectBuilder = m_objectFactory.CreateObjectBuilder<MyObjectBuilder_ComponentBase>(baseType);
            //        baseType = baseType.BaseType;
            //    }
            //}

            return objectBuilder;
        }
示例#18
0
 public void BeforeComponentRemove(MyComponentBase component)
 {
     var trigger = component as MyTriggerComponent;
     /*if (OnBeforeComponentRemove != null)
     {
         OnBeforeComponentRemove(this, trigger);
     }*/
     if (component is MyTriggerComponent)
     {
         TriggerCount--;
     }
     else if (component is MyTriggerAggregate)
     {
         (component as MyTriggerAggregate).OnTriggerCountChanged -= OnChildAggregateCountChanged;
         TriggerCount -= (component as MyTriggerAggregate).TriggerCount;
     }
 }
示例#19
0
 public void AfterComponentAdd(MyComponentBase component)
 {
     var trigger = component as MyTriggerComponent;
     if (component is MyTriggerComponent)
     {
         TriggerCount++;
     }
     else if (component is MyTriggerAggregate)
     {
         (component as MyTriggerAggregate).OnTriggerCountChanged += OnChildAggregateCountChanged;
         TriggerCount += (component as MyTriggerAggregate).TriggerCount;
     }
     /*if (OnAfterComponentAdd != null)
     {
         OnAfterComponentAdd(this, trigger);
     }*/
 }
        public void BeforeComponentRemove(MyComponentBase component)
        {
            var trigger = component as MyTriggerComponent;

            /*if (OnBeforeComponentRemove != null)
             * {
             *  OnBeforeComponentRemove(this, trigger);
             * }*/
            if (component is MyTriggerComponent)
            {
                TriggerCount--;
            }
            else if (component is MyTriggerAggregate)
            {
                (component as MyTriggerAggregate).OnTriggerCountChanged -= OnChildAggregateCountChanged;
                TriggerCount -= (component as MyTriggerAggregate).TriggerCount;
            }
        }
        public void AfterComponentAdd(MyComponentBase component)
        {
            var trigger = component as MyTriggerComponent;

            if (component is MyTriggerComponent)
            {
                TriggerCount++;
            }
            else if (component is MyTriggerAggregate)
            {
                (component as MyTriggerAggregate).OnTriggerCountChanged += OnChildAggregateCountChanged;
                TriggerCount += (component as MyTriggerAggregate).TriggerCount;
            }

            /*if (OnAfterComponentAdd != null)
             * {
             *  OnAfterComponentAdd(this, trigger);
             * }*/
        }
示例#22
0
        public void AfterComponentAdd(MyComponentBase component)
        {
            var inv = component as MyInventoryBase;

            inv.ForcedPriority   = ForcedPriority;
            inv.ContentsChanged += child_OnContentsChanged;
            if (component is MyInventory)
            {
                InventoryCount++;
            }
            else if (component is MyInventoryAggregate)
            {
                (component as MyInventoryAggregate).OnInventoryCountChanged += OnChildAggregateCountChanged;
                InventoryCount += (component as MyInventoryAggregate).InventoryCount;
            }
            if (OnAfterComponentAdd != null)
            {
                OnAfterComponentAdd(this, inv);
            }
        }
示例#23
0
        public void BeforeComponentRemove(MyComponentBase component)
        {
            var inv = component as MyInventoryBase;

            inv.ForcedPriority   = null;
            inv.ContentsChanged -= child_OnContentsChanged;
            if (OnBeforeComponentRemove != null)
            {
                OnBeforeComponentRemove(this, inv);
            }
            if (component is MyInventory)
            {
                InventoryCount--;
            }
            else if (component is MyInventoryAggregate)
            {
                (component as MyInventoryAggregate).OnInventoryCountChanged -= OnChildAggregateCountChanged;
                InventoryCount -= (component as MyInventoryAggregate).InventoryCount;
            }
        }
示例#24
0
        public void BeforeComponentRemove(MyComponentBase component)
        {
            MyInventoryBase base2 = component as MyInventoryBase;

            base2.ForcedPriority   = null;
            base2.ContentsChanged -= new Action <MyInventoryBase>(this.child_OnContentsChanged);
            if (this.OnBeforeComponentRemove != null)
            {
                this.OnBeforeComponentRemove(this, base2);
            }
            if (component is MyInventory)
            {
                int inventoryCount = this.InventoryCount;
                this.InventoryCount = inventoryCount - 1;
            }
            else if (component is MyInventoryAggregate)
            {
                (component as MyInventoryAggregate).OnInventoryCountChanged -= new Action <MyInventoryAggregate, int>(this.OnChildAggregateCountChanged);
                this.InventoryCount -= (component as MyInventoryAggregate).InventoryCount;
            }
        }
示例#25
0
        public void AfterComponentAdd(MyComponentBase component)
        {
            MyInventoryBase base2 = component as MyInventoryBase;

            base2.ForcedPriority   = this.ForcedPriority;
            base2.ContentsChanged += new Action <MyInventoryBase>(this.child_OnContentsChanged);
            if (component is MyInventory)
            {
                int inventoryCount = this.InventoryCount;
                this.InventoryCount = inventoryCount + 1;
            }
            else if (component is MyInventoryAggregate)
            {
                (component as MyInventoryAggregate).OnInventoryCountChanged += new Action <MyInventoryAggregate, int>(this.OnChildAggregateCountChanged);
                this.InventoryCount += (component as MyInventoryAggregate).InventoryCount;
            }
            if (this.OnAfterComponentAdd != null)
            {
                this.OnAfterComponentAdd(this, base2);
            }
        }
示例#26
0
        public static bool TryAddComponent(long entityId, MyDefinitionId componentDefinitionId)
        {
            MyEntity entity;
            MyComponentDefinitionBase base2;

            if (!MyEntities.TryGetEntityById(entityId, out entity, false))
            {
                return(false);
            }
            if (TryGetComponentDefinition(componentDefinitionId.TypeId, componentDefinitionId.SubtypeId, out base2))
            {
                MyComponentBase component     = MyComponentFactory.CreateInstanceByTypeId(base2.Id.TypeId);
                Type            componentType = MyComponentTypeFactory.GetComponentType(component.GetType());
                if (componentType == null)
                {
                    return(false);
                }
                component.Init(base2);
                entity.Components.Add(componentType, component);
            }
            return(true);
        }
        public static bool RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
        {
            int index = aggregate.ChildList.GetComponentIndex(component);
            if (index != -1)
            {
                aggregate.BeforeComponentRemove(component);
                component.SetContainer(null);
                aggregate.ChildList.RemoveComponentAt(index);
                return true;
            }

            foreach (var child in aggregate.ChildList.Reader)
            {
                var childAggregate = child as IMyComponentAggregate;
                if (childAggregate == null) continue;

                bool removed = childAggregate.RemoveComponent(component);
                if (removed) return true;
            }

            return false;
        }
 /// <summary>
 /// Adds to list but doesn't change ownership
 /// </summary>
 public static void AttachComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     aggregate.ChildList.AddComponent(aggregate.ContainerBase, component);         
 }
 public void AfterComponentAdd(MyComponentBase component)
 {
     (component as MyInventoryBase).ContentsChanged += child_OnContentsChanged;
     if (OnAfterComponentAdd != null)
     {
         OnAfterComponentAdd(this, component as MyInventoryBase);
     }                
 }
示例#30
0
 void Components_ComponentAdded(Type arg1, MyComponentBase arg2)
 {
     if (arg1 == typeof(MyGameLogicComponent))
         m_debrisLogic = arg2 as MyDebrisBaseLogic;
 }
示例#31
0
 void Components_ComponentAdded(Type arg1, MyComponentBase arg2)
 {
     if (arg1 == typeof(MyGameLogicComponent))
         m_logic = arg2 as MyMeteorGameLogic;
 }
 public void BeforeComponentRemove(MyComponentBase component)
 {
     var inv = component as MyInventoryBase;
     inv.ForcedPriority = null;
     inv.ContentsChanged -= child_OnContentsChanged;
     if (OnBeforeComponentRemove != null)
     {
         OnBeforeComponentRemove(this, inv);
     }
     if (component is MyInventory)
     {
         InventoryCount--;
     }
     else if (component is MyInventoryAggregate)
     {
         (component as MyInventoryAggregate).OnInventoryCountChanged -= OnChildAggregateCountChanged;
         InventoryCount -= (component as MyInventoryAggregate).InventoryCount;
     }
 }
 public bool Contains(MyComponentBase component)
 {
     if (m_components.Contains(component))
     {               
             return true;               
     }
     foreach (var childComponent in m_components)
     {
         if (childComponent is IMyComponentAggregate)
         {
             var childAggregate = (childComponent as IMyComponentAggregate);
             if (childAggregate.ChildList.Contains(component))
             {
                 return true;
             }
         }
     }
     return false;
 }
        public bool RemoveComponent(MyComponentBase component)
        {
            if (Contains(component))
            {
                component.OnBeforeRemovedFromContainer();                
            }
            else 
            {
                return false;
            }

            if (m_components.Remove(component))
            {
                return true;
            }
            foreach (var childComponent in m_components)
            {
                if (childComponent is IMyComponentAggregate)
                {
                    var childAggregate = (childComponent as IMyComponentAggregate);                    
                    if (childAggregate.ChildList.RemoveComponent(component))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
 public void AddComponent(MyComponentBase component)
 {
     m_components.Add(component);
 }
 private void child_OnContentsChanged(MyComponentBase obj)
 {
     OnContentsChanged();
 }
 public void BeforeComponentRemove(MyComponentBase component)
 {
     (component as MyInventoryBase).ContentsChanged -= child_OnContentsChanged;
     if (OnBeforeComponentRemove != null)
     {
         OnBeforeComponentRemove(this, component as MyInventoryBase);
     }
 }
 public void AddComponent(MyComponentContainer container, MyComponentBase component)
 {
     m_components.Add(component);
 }
 public int GetComponentIndex(MyComponentBase component)
 {
     return m_components.IndexOf(component);
 }
示例#40
0
 public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder)
 {
     if (MyDefinitionManager.Static != null)
     {
         MyContainerDefinition definition = null;
         bool flag = ReferenceEquals(builder, null);
         if (TryGetContainerDefinition(type, subtypeName, out definition))
         {
             container.Init(definition);
             if (definition.DefaultComponents != null)
             {
                 foreach (MyContainerDefinition.DefaultComponent component in definition.DefaultComponents)
                 {
                     MyComponentDefinitionBase     componentDefinition = null;
                     MyObjectBuilder_ComponentBase base3 = FindComponentBuilder(component, builder);
                     bool            flag2 = base3 != null;
                     Type            type2 = null;
                     MyComponentBase base4 = null;
                     MyStringHash    hash  = subtypeName;
                     if (component.SubtypeId != null)
                     {
                         hash = component.SubtypeId.Value;
                     }
                     if (TryGetComponentDefinition(component.BuilderType, hash, out componentDefinition))
                     {
                         base4 = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId);
                         base4.Init(componentDefinition);
                     }
                     else if (component.IsValid())
                     {
                         base4 = component.BuilderType.IsNull ? MyComponentFactory.CreateInstanceByType(component.InstanceType) : MyComponentFactory.CreateInstanceByTypeId(component.BuilderType);
                     }
                     if (base4 != null)
                     {
                         Type componentType = MyComponentTypeFactory.GetComponentType(base4.GetType());
                         if (componentType != null)
                         {
                             type2 = componentType;
                         }
                         else
                         {
                             MyComponentDefinitionBase base1 = componentDefinition;
                         }
                     }
                     if ((type2 == null) && (base4 != null))
                     {
                         type2 = base4.GetType();
                     }
                     if (((base4 != null) && (type2 != null)) && ((flag | flag2) || component.ForceCreate))
                     {
                         if (base3 != null)
                         {
                             base4.Deserialize(base3);
                         }
                         container.Add(type2, base4);
                     }
                 }
             }
         }
         container.Deserialize(builder);
     }
 }
 public void AfterComponentAdd(MyComponentBase component)
 {
     var inv = component as MyInventoryBase;
     inv.ForcedPriority = ForcedPriority;
     inv.ContentsChanged += child_OnContentsChanged;
     if (component is MyInventory)
     {
         InventoryCount++;
     }
     else if (component is MyInventoryAggregate)
     {
         (component as MyInventoryAggregate).OnInventoryCountChanged += OnChildAggregateCountChanged;
         InventoryCount += (component as MyInventoryAggregate).InventoryCount;
     }
     if (OnAfterComponentAdd != null)
     {
         OnAfterComponentAdd(this, inv);
     }
 }
 /// <summary>
 /// Adds to list but doesn't change ownership
 /// </summary>
 public static void AttachComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     Debug.Assert(aggregate != component, "Can not add to itself!");
     aggregate.ChildList.AddComponent(component);         
 }
 public bool RemoveComponent(MyComponentBase component)
 {
     if (m_components.Remove(component))
     {              
         return true;
     }
     foreach (var childComponent in m_components)
     {
         if (childComponent is IMyComponentAggregate)
         {
             if ((childComponent as IMyComponentAggregate).ChildList.RemoveComponent(component))
             {
                 return true;
             }
         }
     }
     return false;
 }        
 public static void AddComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     aggregate.ChildList.AddComponent(aggregate.ContainerBase, component);
     component.SetContainer(aggregate.ContainerBase);
     aggregate.AfterComponentAdd(component);
 }
示例#45
0
 private void child_OnContentsChanged(MyComponentBase obj)
 {
     OnContentsChanged();
 }
        /// <summary>
        /// Tries to retrieve entity definition of the entity owning this container, check if the definition has some DefaultComponents,
        /// tries to retrieve these components definitions, create these components instances and add them
        ///
        /// TODO: This should be ideally a behavior of the MyEntityComponentContainer when it is initialized (deserialized).. or by the factory, for now, this is an extension method
        /// </summary>
        public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder)
        {
            if (MyDefinitionManager.Static != null)
            {
                MyContainerDefinition definition = null;

                bool IsFirstInit = builder == null;

                if (TryGetContainerDefinition(type, subtypeName, out definition))
                {
                    container.Init(definition);

                    if (definition.DefaultComponents != null)
                    {
                        foreach (var component in definition.DefaultComponents)
                        {
                            MyComponentDefinitionBase     componentDefinition = null;
                            MyObjectBuilder_ComponentBase componentBuilder    = FindComponentBuilder(component, builder);
                            bool            IsComponentSerialized             = componentBuilder != null;
                            Type            componentType     = null;
                            MyComponentBase componentInstance = null;

                            var componentSubtype = subtypeName;
                            if (component.SubtypeId.HasValue)
                            {
                                componentSubtype = component.SubtypeId.Value;
                            }

                            // Create component instance
                            if (TryGetComponentDefinition(component.BuilderType, componentSubtype, out componentDefinition))
                            {
                                componentInstance = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId);
                                componentInstance.Init(componentDefinition);
                            }
                            else if (component.IsValid())
                            {
                                if (!component.BuilderType.IsNull)
                                {
                                    componentInstance = MyComponentFactory.CreateInstanceByTypeId(component.BuilderType);
                                }
                                else
                                {
                                    componentInstance = MyComponentFactory.CreateInstanceByType(component.InstanceType);
                                }
                            }

                            // Check component type from attributes.
                            if (componentInstance != null)
                            {
                                var componentTypeFromAttr = MyComponentTypeFactory.GetComponentType(componentInstance.GetType());
                                if (componentTypeFromAttr != null)
                                {
                                    componentType = componentTypeFromAttr;
                                }
                                else
                                {
                                    if (componentDefinition != null)
                                    {
                                        System.Diagnostics.Debug.Fail("Unknown component type - component type attribute not specified for component class: " + componentInstance.GetType());
                                    }
                                }
                            }

                            //TODO: this should be avoided! Component type MUST be set via MyComponentType attribute.
                            if (componentType == null && componentInstance != null)
                            {
                                componentType = componentInstance.GetType();
                            }

                            // If everything passed, go on..
                            if (componentInstance != null && componentType != null)
                            {
                                bool componentShouldBeAdded = IsFirstInit || IsComponentSerialized || component.ForceCreate;

                                if (componentShouldBeAdded)
                                {
                                    if (componentBuilder != null)
                                    {
                                        componentInstance.Deserialize(componentBuilder);
                                    }

                                    // Add only fully initialized component..
                                    container.Add(componentType, componentInstance);
                                }
                            }
                            else
                            {
                                System.Diagnostics.Debug.Fail("Component instance wasn't created or it's base type couldn't been determined!");
                            }
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.Fail("Got definition for container, but DefaultComponents is null!");
                    }
                }

                // This may rewrite already once deserialized data, but will also add missing components in definition
                container.Deserialize(builder);
            }
            else
            {
                System.Diagnostics.Debug.Fail("Trying to init enabled components on entity, but definition manager is null");
            }
        }