internal void Remove(Entity entity, ref FlatMap <Entity, int> entityIndexMap) { Count--; // Get the index of the entity that needs to be removed var indexToReplace = entityIndexMap[entity]; // And replace the entity with the entity at the back of the array var lastEntity = entities[Count]; entities[indexToReplace] = lastEntity; // Reflect the change in the component arrays swapBackRemove(indexToReplace, Count); entityIndexMap[lastEntity] = indexToReplace; }
internal void Move(Entity entity, ArchetypeGroup newGroup, ref FlatMap <Entity, int> entityIndexMap) { int currentIndex = entityIndexMap[entity]; int newIndex = newGroup.Add(entity, ref entityIndexMap); foreach (var type in newGroup.componentTypes) { if (componentsMap.ContainsKey(type)) { newGroup.componentsMap[type].SetValue(componentsMap[type].GetValue(currentIndex), newIndex); } } Remove(entity, ref entityIndexMap); }
internal int Add(Entity entity, ref FlatMap <Entity, int> entityIndexMap) { // Allocate more space if needed if (Count == entities.Length - 1) { ResizeAllArrays(entities.Length * 2); } // Always add after the last element (use count as index) entities[Count] = entity; // Register the index in the world's entity index map entityIndexMap[entity] = Count; return(Count++); }
private World(IEnumerable <ISystem> systems, Dictionary <Type, object> dependencyMap) { const int reserved = 64; typeMap = new Dictionary <Type[], Archetype>(new TypeArrayCompararer()); archetypeExistanceMap = new FlatMap <Archetype, bool>(reserved); archetypeMap = new FlatMap <Archetype, Type[]>(reserved); archetypeGroupMap = new FlatMap <Archetype, ArchetypeGroup>(reserved); entityArchetypeMap = new FlatMap <Entity, Archetype>(reserved); entityArchetypeGroupMap = new FlatMap <Entity, ArchetypeGroup>(reserved); entityIndexMap = new FlatMap <Entity, int>(reserved); componentGroups = new List <ComponentGroup>(); openEntityIds = new Queue <int>(); groupInjectorMap = new Dictionary <ISystem, List <GroupInjector> >(); //systemMap = systems // .ToLookup(system => system.GetType().BaseType.GetGenericArguments()[0]) as Lookup<Type, ISystem>; var systemTuples = (systems .ToLookup(system => system.GetType().BaseType.GetGenericArguments()[0]) as Lookup <Type, ISystem>) .ApplyResultSelector((type, s) => (type, systems: s.ToArray())); systemMap = new Dictionary <Type, ISystem[]>(); foreach (var tuple in systemTuples) { systemMap.Add(tuple.type, tuple.systems); } this.dependencyMap = dependencyMap; postActions = new ConcurrentQueue <Action>(); postMessages = new Dictionary <Type, object>(); nextArchetypeId = 0; CreateArchetype(new Type[] { }); nextEntityId = 0; }