Пример #1
0
 public void CopyFrom(EcsComponentMask mask)
 {
     BitsCount = mask.BitsCount;
     if (Bits.Length < BitsCount)
     {
         Bits = new int[mask.Bits.Length];
     }
     Array.Copy(mask.Bits, 0, Bits, 0, BitsCount);
 }
Пример #2
0
 public bool IsIntersects(EcsComponentMask mask)
 {
     if (BitsCount > 0 && mask.BitsCount > 0)
     {
         for (var i = 0; i < BitsCount; i++)
         {
             var bit = Bits[i];
             for (var j = 0; j < mask.BitsCount; j++)
             {
                 if (mask.Bits[j] == bit)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Пример #3
0
 public bool IsEquals(EcsComponentMask mask)
 {
     if (BitsCount != mask.BitsCount)
     {
         return(false);
     }
     for (var i = 0; i < BitsCount; i++)
     {
         var j   = mask.BitsCount - 1;
         var bit = Bits[i];
         for (; j >= 0; j--)
         {
             if (mask.Bits[j] == bit)
             {
                 break;
             }
         }
         if (j == -1)
         {
             return(false);
         }
     }
     return(true);
 }
Пример #4
0
        public static void Inject(EcsWorld world, IEcsSystem system)
        {
            var worldType            = world.GetType();
            var systemType           = system.GetType();
            var ecsFilter            = typeof(EcsFilter);
            var ecsIndex             = typeof(int);
            var attrEcsWorld         = typeof(EcsWorldAttribute);
            var attrEcsFilterInclude = typeof(EcsFilterIncludeAttribute);
            var attrEcsFilterExclude = typeof(EcsFilterExcludeAttribute);
            var attrEcsIndex         = typeof(EcsIndexAttribute);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // [EcsWorld]
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic && Attribute.IsDefined(f, attrEcsWorld))
                {
                    f.SetValue(system, world);
                }

                // [EcsFilterInclude]
                if (f.FieldType == ecsFilter && !f.IsStatic)
                {
                    EcsComponentMask includeMask = null;
                    var standardFilterIncDefined = Attribute.IsDefined(f, attrEcsFilterInclude);
                    if (standardFilterIncDefined)
                    {
                        includeMask = new EcsComponentMask();
                        var components = ((EcsFilterIncludeAttribute)Attribute.GetCustomAttribute(f, attrEcsFilterInclude)).Components;
                        for (var i = 0; i < components.Length; i++)
                        {
                            includeMask.SetBit(world.GetComponentIndex(components[i]), true);
                        }
                    }
                    EcsComponentMask excludeMask = null;
                    var standardFilterExcDefined = Attribute.IsDefined(f, attrEcsFilterExclude);
                    if (standardFilterExcDefined)
                    {
                        excludeMask = new EcsComponentMask();
                        var components = ((EcsFilterExcludeAttribute)Attribute.GetCustomAttribute(f, attrEcsFilterExclude)).Components;
                        for (var i = 0; i < components.Length; i++)
                        {
                            excludeMask.SetBit(world.GetComponentIndex(components[i]), true);
                        }
                    }
#if DEBUG && !ECS_PERF_TEST
                    if (standardFilterIncDefined && includeMask.IsEmpty())
                    {
                        throw new Exception("Include filter cant be empty at system: " + systemType.Name);
                    }
                    if (standardFilterExcDefined && excludeMask.IsEmpty())
                    {
                        throw new Exception("Exclude filter cant be empty at system: " + systemType.Name);
                    }
                    if (!standardFilterIncDefined && standardFilterExcDefined)
                    {
                        throw new Exception("EcsFilterExclude can be applied only as pair to EcsFilterInclude at system: " + systemType.Name);
                    }
                    if (includeMask != null && excludeMask != null && includeMask.IsIntersects(excludeMask))
                    {
                        throw new Exception("Exclude and include filters are intersected at system: " + systemType.Name);
                    }
#endif
                    if (standardFilterIncDefined)
                    {
                        f.SetValue(system, world.GetFilter(includeMask, excludeMask ?? new EcsComponentMask()));
                    }
                }

                // [EcsIndex]
                if (f.FieldType == ecsIndex && !f.IsStatic && Attribute.IsDefined(f, attrEcsIndex))
                {
                    var component = ((EcsIndexAttribute)Attribute.GetCustomAttribute(f, attrEcsIndex)).Component;
                    f.SetValue(system, world.GetComponentIndex(component));
                }
            }
        }