コード例 #1
0
ファイル: EcsInjections.cs プロジェクト: Instalox/ecs
        /// <summary>
        /// Injects EcsWorld / EcsFilter fields to IEcsSystem.
        /// </summary>
        /// <param name="system">System to scan for injection.</param>
        /// <param name="world">EcsWorld instance to inject.</param>
        public static void Inject(IEcsSystem system, EcsWorld world)
        {
            var systemType = system.GetType();

            if (!Attribute.IsDefined(systemType, typeof(EcsInjectAttribute)))
            {
                return;
            }
            var worldType  = world.GetType();
            var filterType = typeof(EcsFilter);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // EcsWorld
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic)
                {
                    f.SetValue(system, world);
                    continue;
                }
                // EcsFilter
                Internals.EcsHelpers.Assert(f.FieldType != filterType,
                                            string.Format("Cant use EcsFilter type at \"{0}\" system for dependency injection, use generic version instead", system));
                if (f.FieldType.IsSubclassOf(filterType) && !f.IsStatic)
                {
                    f.SetValue(system, world.GetFilter(f.FieldType));
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Creates entity with single component at specified EcsWorld.
        /// </summary>
        /// <param name="world">World instance.</param>
        public static Inc1 Create(EcsWorld world)
        {
            world.GetFilter <EcsFilterSingle <Inc1> > ();
            var data = world.CreateEntityWith <Inc1> ();

            world.ProcessDelayedUpdates();
            return(data);
        }
コード例 #3
0
        /// <summary>
        /// Injects EcsWorld / EcsFilter fields to IEcsSystem.
        /// </summary>
        /// <param name="system">System to scan for injection.</param>
        /// <param name="world">EcsWorld instance to inject.</param>
        public static void Inject(IEcsSystem system, EcsWorld world, System.Collections.Generic.Dictionary <Type, object> injections)
        {
            var systemType = system.GetType();

            if (!Attribute.IsDefined(systemType, typeof(EcsInjectAttribute)))
            {
                return;
            }
            var worldType  = world.GetType();
            var filterType = typeof(EcsFilter);
            var ignoreType = typeof(EcsIgnoreInjectAttribute);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // skip fields with [EcsIgnoreInject] attribute.
                if (Attribute.IsDefined(f, ignoreType))
                {
                    continue;
                }
                // EcsWorld
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic)
                {
                    f.SetValue(system, world);
                    continue;
                }
                // EcsFilter
#if DEBUG
                if (f.FieldType == filterType)
                {
                    throw new Exception(string.Format("Cant use EcsFilter type at \"{0}\" system for dependency injection, use generic version instead", system));
                }
#endif
                if (f.FieldType.IsSubclassOf(filterType) && !f.IsStatic)
                {
                    f.SetValue(system, world.GetFilter(f.FieldType));
                    continue;
                }
                // Other injections.
                foreach (var pair in injections)
                {
                    if (f.FieldType.IsAssignableFrom(pair.Key) && !f.IsStatic)
                    {
                        f.SetValue(system, pair.Value);
                        break;
                    }
                }
            }
        }