Beispiel #1
0
        protected void HandleTupleInjection(ComponentSystem system)
        {
            Type systemType = system.GetType();

            Type injectTupleAttributeType = typeof(InjectTupleAttribute);
            Type iComponentArrayType      = typeof(ComponentArray);

            FieldInfo[] allFields = systemType.GetFieldsRecursive(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public).ToArray();
            IGrouping <int, FieldInfo>[] injectionTypeGroups = systemType.GetFieldsRecursive(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
                                                               .Where(field => field.GetCustomAttributes(injectTupleAttributeType, false).Any())
                                                               .Where(field => iComponentArrayType.IsAssignableFrom(field.FieldType))
                                                               .GroupBy(field => (field.GetCustomAttributes(injectTupleAttributeType, false).First() as InjectTupleAttribute).GroupId)
                                                               .ToArray();


            foreach (IGrouping <int, FieldInfo> injectionTypeGroup in injectionTypeGroups)
            {
                FieldInfo[] injectionTypeFields     = injectionTypeGroup.ToArray();
                Type[]      injectionComponentTypes = injectionTypeGroup
                                                      .Select(field => field.FieldType.GetGenericArguments()[0])
                                                      .ToArray();

                ComponentGroup group = _entityManager.GetComponentGroup(injectionComponentTypes);

                for (int i = 0; i < injectionComponentTypes.Length; i++)
                {
                    ComponentArray componentArray = group.GetComponent(injectionComponentTypes[i]);
                    injectionTypeFields[i].SetValue(system, componentArray);
                }

                IComponentSystemSetup systemSetup = system;
                systemSetup.AddGroup(group);
            }
        }
Beispiel #2
0
        private ComponentArray <TComponent> GetComponentMap <TComponent>(bool createIfNotFound) where TComponent : IComponent
        {
            ComponentArray entityComponentMap;
            Type           componentType = typeof(TComponent);

            if (!_components.TryGetValue(componentType, out entityComponentMap) && createIfNotFound)
            {
                entityComponentMap = new ComponentArray <TComponent>();
                _components.Add(componentType, entityComponentMap);
            }
            return((ComponentArray <TComponent>)entityComponentMap);
        }
Beispiel #3
0
        public void SetComponent <TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
        {
            Type componentType = typeof(TComponent);

            if (componentType == _iComponentType)
            {
                throw new InvalidTComponentException();
            }
            ComponentArray <TComponent> entityComponentMap = GetComponentMap <TComponent>(false);

            entityComponentMap.Update(entity, component);
            //_compoentChangedList.Add(new KeyValuePair<Entity, Type>(entity, componentType));
            UpdateComponentGroups(ref entity, ref component);
        }
        public void RemoveComponentImmediate <TComponent>(Entity entity) where TComponent : IComponent
        {
            if (typeof(TComponent) == _iComponentType)
            {
                throw new InvalidTComponentException();
            }
            Type componentType = typeof(TComponent);
            ComponentArray <TComponent> entityComponentMap = GetComponentMap <TComponent>(false);

            if (entityComponentMap != null && entityComponentMap.Contains(entity))
            {
                _entityComponents[entity].Remove(componentType);
                entityComponentMap.Remove(entity);
                InspectComponentGroups(entity);
            }
        }
Beispiel #5
0
        public void AddComponent <TComponent>(Entity entity, TComponent component) where TComponent : IComponent
        {
            if (typeof(TComponent) == _iComponentType)
            {
                throw new InvalidTComponentException();
            }

            Type componentType = typeof(TComponent);
            ComponentArray <TComponent> entityComponentMap = GetComponentMap <TComponent>(true);

            if (entityComponentMap.Add(entity, component))
            {
                var type = component.GetType();
                _entityComponents[entity].Add(componentType);
                InspectComponentGroups(entity);
                _componentAddedToEntityEvent.CallEvent(this, ref entity, componentType);
            }
        }
Beispiel #6
0
        public void RemoveComponent <TComponent>(Entity entity, TComponent component) where TComponent : IComponent
        {
            if (typeof(TComponent) == _iComponentType)
            {
                throw new InvalidTComponentException();
            }
            Type componentType = typeof(TComponent);
            ComponentArray <TComponent> entityComponentMap = GetComponentMap <TComponent>(false);

            if (entityComponentMap != null)
            {
                entityComponentMap.Remove(entity);
                _entityComponents[entity].Remove(componentType);
                InspectComponentGroups(entity);
                _componentRemovedFromEntityEvent.CallEvent(this, ref entity, componentType);
            }
            //_compoentRemovedList.Add(new KeyValuePair<Entity, Type>(entity, componentType));
        }
        public void AddComponent <TComponent>(Entity entity, TComponent component) where TComponent : IComponent
        {
            if (typeof(TComponent) == _iComponentType)
            {
                throw new InvalidTComponentException();
            }

            Type componentType = typeof(TComponent);
            ComponentArray <TComponent> entityComponentMap = GetComponentMap <TComponent>(true);

            HashSet <Type> componentTypes = _entityComponents[entity];

            if (!componentTypes.Contains(componentType))
            {
                _entityComponents[entity].Add(componentType);
                entityComponentMap.Add(entity, component);
                InspectComponentGroups(entity);
            }
        }
        private ComponentArray <TComponent> GetComponentMap <TComponent>(bool createIfNotFound) where TComponent : IComponent
        {
            ComponentArray entityComponentMap;
            Type           componentType = typeof(TComponent);

            if (!_components.TryGetValue(componentType, out entityComponentMap) && createIfNotFound)
            {
                ComponentArray <TComponent> typedEntityComponentMap = new ComponentArray <TComponent>();
                typedEntityComponentMap.SubscribeOnComponentAddedToEntity(this);
                typedEntityComponentMap.SubscribeOnComponentRemovingFromEntity(this);
                typedEntityComponentMap.SubscribeOnComponentRemovedFromEntity(this);
                typedEntityComponentMap.SubscribeOnComponentChangedOfEntity(this);

                entityComponentMap = typedEntityComponentMap;
                _entityRemovedEvent.Subscribe(typedEntityComponentMap);

                _components.Add(componentType, typedEntityComponentMap);
            }
            return((ComponentArray <TComponent>)entityComponentMap);
        }
        internal void HandleDeletion()
        {
            foreach (DeletionPair item in _deletableComponents)
            {
                Entity         entity             = item.entity;
                ComponentArray entityComponentMap = item.componentArray;
                if (entityComponentMap != null && entityComponentMap.Contains(entity))
                {
                    entityComponentMap.Remove(entity);
                    _entityComponents[entity].Remove(entityComponentMap.ComponentType);
                    InspectComponentGroups(entity);
                }
            }
            _deletableComponents.Clear();

            foreach (Entity entity in _destroyableEntities)
            {
                DestroyEntityImmediate(entity);
            }
            _destroyableEntities.Clear();
        }
 public DeletionPair(Entity entity, ComponentArray componentArray)
 {
     this.entity         = entity;
     this.componentArray = componentArray;
     _hash = GenerateHash(entity.GetHashCode(), componentArray.GetHashCode());
 }