Ejemplo n.º 1
0
        private EcsArchetype InnerFindOrCreateArchetype(byte[] indices)
        {
            EcsArchetype curArchetype = _emptyArchetype;

            for (int i = 0; i < indices.Length; i++)
            {
                byte         index         = indices[i];
                EcsArchetype nextArchetype = curArchetype.Next[index];
                if (nextArchetype == null)
                {
                    byte[] archetypeIndices = new byte[i + 1];
                    for (int j = 0; j < archetypeIndices.Length; j++)
                    {
                        archetypeIndices[j] = indices[j];
                    }

                    nextArchetype = new EcsArchetype(_archetypeIdCounter++, archetypeIndices);
                    nextArchetype.Prior[index] = curArchetype;
                    foreach (ushort componentType in nextArchetype.Indices)
                    {
                        _archetypeIndices[componentType].Add(nextArchetype);
                    }

                    curArchetype.Next[index] = nextArchetype;

                    _archetypes.Add(nextArchetype);
                }

                curArchetype = nextArchetype;
            }

            return(curArchetype);
        }
Ejemplo n.º 2
0
        public EcsArchetype FindOrCreateNextArchetype(EcsArchetype archetype, byte addIndex)
        {
            EcsArchetype nextArchetype = archetype.Next[addIndex];

            if (nextArchetype != null)
            {
                return(nextArchetype);
            }

            bool added  = false;
            int  length = 0;

            byte[] indices = new byte[archetype.Indices.Length + 1];
            foreach (byte index in archetype.Indices)
            {
                if (addIndex < index && !added)
                {
                    indices[length++] = addIndex;
                    added             = true;
                }

                indices[length++] = index;
            }

            if (!added)
            {
                indices[length] = addIndex;
            }

            return(InnerFindOrCreateArchetype(indices));
        }
Ejemplo n.º 3
0
        public void Destroy()
        {
            _archetype.RemoveEntity(this);
            _archetype = null;

            OnDestroy();
        }
Ejemplo n.º 4
0
        public void Initialize <TC0>(uint id, TC0 component0) where TC0 : class, IEcsComponent, new()
        {
            Id = id;

            byte index0 = EcsComponentType <TC0> .Index;

            _archetype = _archetypeManager.FindOrCreateArchetype(index0);
            _archetype.AddComponent(index0, component0);
            _archetype.AddEntity(this);
        }
Ejemplo n.º 5
0
        public EcsArchetypeManager()
        {
            _emptyArchetype = new EcsArchetype(_archetypeIdCounter++, new byte[] { });
            _archetypes     = new List <EcsArchetype> {
                _emptyArchetype
            };
            _archetypeIndices = new List <EcsArchetype> [byte.MaxValue];

            for (int i = 0; i < _archetypeIndices.Length; i++)
            {
                _archetypeIndices[i] = new List <EcsArchetype>();
            }
        }
Ejemplo n.º 6
0
        public IEnumerable <EcsArchetype> GetArchetypes(byte index, int startId)
        {
            List <EcsArchetype> archetypes = _archetypeIndices[index];

            for (int i = archetypes.Count - 1; i >= 0; i--)
            {
                EcsArchetype archetype = archetypes[i];
                if (archetype.Id <= startId)
                {
                    break;
                }

                yield return(archetype);
            }
        }
Ejemplo n.º 7
0
        public TC GetOrCreateSingleton <TC>() where TC : class, IEcsComponent, new()
        {
            EcsArchetype archetype = _archetypeManager.FindOrCreateArchetype(EcsComponentType <TC> .Index);

            EcsEntity[] entities = archetype.GetEntities(out int length);

            for (int i = 0; i < length;)
            {
                return(entities[i].GetComponent <TC>());
            }

            TC component = new TC();

            CreateEntity(component);
            return(component);
        }
Ejemplo n.º 8
0
        internal void AddComponent(byte index, IEcsComponent component)
        {
            EcsArchetype newArchetype = _archetypeManager.FindOrCreateNextArchetype(_archetype, index);

            foreach (byte curIndex in _archetype.Indices)
            {
                IEcsComponentPool componentPool = _archetype.GetComponentPool(curIndex);
                newArchetype.AddComponent(curIndex, componentPool.Get(ArchetypeIndex));
            }

            newArchetype.AddComponent(index, component);

            _archetype.RemoveEntity(this);
            _archetype = newArchetype;
            _archetype.AddEntity(this);
        }
Ejemplo n.º 9
0
        public void Initialize <TC0, TC1, TC2>(uint id, TC0 component0, TC1 component1, TC2 component2)
            where TC0 : class, IEcsComponent, new()
            where TC1 : class, IEcsComponent, new()
            where TC2 : class, IEcsComponent, new()
        {
            Id = id;

            byte index0 = EcsComponentType <TC0> .Index;
            byte index1 = EcsComponentType <TC1> .Index;
            byte index2 = EcsComponentType <TC2> .Index;

            _archetype = _archetypeManager.FindOrCreateArchetype(index0, index1, index2);
            _archetype.AddComponent(index0, component0);
            _archetype.AddComponent(index1, component1);
            _archetype.AddComponent(index2, component2);
            _archetype.AddEntity(this);
        }
Ejemplo n.º 10
0
        internal void RemoveComponent(byte index)
        {
            EcsArchetype newArchetype = _archetypeManager.FindOrCreatePriorArchetype(_archetype, index);

            foreach (byte curIndex in _archetype.Indices)
            {
                if (curIndex == index)
                {
                    continue;
                }

                IEcsComponentPool componentPool = _archetype.GetComponentPool(curIndex);
                newArchetype.AddComponent(curIndex, componentPool.Get(ArchetypeIndex));
            }

            _archetype.RemoveEntity(this);
            _archetype = newArchetype;
            _archetype.AddEntity(this);
        }
Ejemplo n.º 11
0
        public EcsArchetype FindOrCreatePriorArchetype(EcsArchetype archetype, byte removeIndex)
        {
            EcsArchetype priorArchetype = archetype.Prior[removeIndex];

            if (priorArchetype != null)
            {
                return(priorArchetype);
            }

            int length = 0;

            byte[] indices = new byte[archetype.Indices.Length - 1];
            foreach (byte index in archetype.Indices)
            {
                if (index != removeIndex)
                {
                    indices[length++] = index;
                }
            }

            return(InnerFindOrCreateArchetype(indices));
        }
Ejemplo n.º 12
0
 public void Initialize(uint id)
 {
     Id         = id;
     _archetype = _archetypeManager.Empty;
     _archetype.AddEntity(this);
 }