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); }
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)); }
public void Destroy() { _archetype.RemoveEntity(this); _archetype = null; OnDestroy(); }
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); }
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>(); } }
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); } }
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); }
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); }
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); }
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); }
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)); }
public void Initialize(uint id) { Id = id; _archetype = _archetypeManager.Empty; _archetype.AddEntity(this); }