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);
            }
        }
        public void AddSystem(ComponentSystem instance)
        {
            bool allowInstanceTypeSearch = true;
            Type instanceType            = instance.GetType();

            while (allowInstanceTypeSearch)
            {
                if (instanceType == typeof(ComponentSystem))
                {
                    break;
                }

                foreach (object attribute in instanceType.GetCustomAttributes(false))
                {
                    if (attribute is AllowMultipleAttribute)
                    {
                        allowInstanceTypeSearch = false;
                        break;
                    }
                }

                if (typeof(EntitySystem)
                    .GetMethod("GetSystem")
                    .MakeGenericMethod(instanceType)
                    .Invoke(this, null) != null)
                {
                    UnityEngine.Debug.LogError(string.Format(
                                                   "OverallEntitySystem--AddSystem--Cannot have two instances of a ComponentSystem--{1}",
                                                   instance.GetType().ToString()));
                    return;
                }

                instanceType = instanceType.BaseType;
            }

            this.systems.Add(instance);
            this.ResetSystemCaches();
        }
Beispiel #3
0
        public virtual void AddSystem(ComponentSystem componentSystem)
        {
            Type componentSystemType = componentSystem.GetType();

            if (componentSystemType.GetMethod("OnStart").DeclaringType == componentSystemType)
            {
                _startSystemList.Add(componentSystem);
            }
            if (componentSystemType.GetMethod("OnUpdate").DeclaringType == componentSystemType)
            {
                _updateSystemList.Add(componentSystem);
            }
            if (componentSystemType.GetMethod("OnFixedUpdate").DeclaringType == componentSystemType)
            {
                _fixedUpdateSystemList.Add(componentSystem);
            }
            InjectionManager.ResolveDependency(componentSystem);
            HandleTupleInjection(componentSystem);
        }