/// <summary> /// Change an entity's archetype. Existing components that do not fit the new archetype will be lost. /// New components introduced will not be initialized. /// </summary> /// <param name="entity">Entity to change the archetype of</param> /// <param name="archetype">The target archetype</param> public void Become(Entity entity, Archetype archetype) { var currentArchetype = entityArchetypeMap[entity]; var currentGroup = archetypeGroupMap[currentArchetype]; var newGroup = archetypeGroupMap[archetype]; currentGroup.Move(entity, newGroup, ref entityIndexMap); }
private void MoveEntity(Entity entity, Archetype currentArchetype, Type[] types) { var newArchetype = typeMap[types]; if (newArchetype == defaultArchetype) { newArchetype = CreateArchetype(types); } var oldGroup = archetypeGroupMap[currentArchetype]; var newGroup = archetypeGroupMap[newArchetype]; oldGroup.Move(entity, newGroup, ref entityIndexMap); }
internal ArchetypeGroup(Archetype archetype, Type[] types) { entities = new Entity[initialSize]; tagTypes = types.Where(t => t.GetInterfaces().Contains(typeof(ITag))).ToArray(); componentTypes = types.Where(t => t.GetInterfaces().Contains(typeof(IComponent))).ToArray(); lockedForWriting = new bool[componentTypes.Length]; componentsMap = new Dictionary <Type, Array>(); componentArrays = new Array[componentTypes.Length]; for (var i = 0; i < componentArrays.Length; i++) { var componentType = componentTypes[i]; componentArrays[i] = Array.CreateInstance(componentType, initialSize); componentsMap.Add(componentType, componentArrays[i]); } swapBackRemove = componentArrays.Length > 0 ? CreateSwapBackRemove(componentArrays) : null; }
/// <summary> /// Create a new archetype. /// </summary> /// <param name="types">Component types that make up the archetype</param> /// <returns>New archetype</returns> public Archetype CreateArchetype(params Type[] types) { var archetype = new Archetype(nextArchetypeId++); // Register the types for the archetype archetypeMap[archetype] = types; // Add types for reverse lookup typeMap.Add(types, archetype); // Create an archetype group for the new archetype archetypeGroupMap[archetype] = new ArchetypeGroup(archetype, types); // Update component groups foreach (var componentGroup in componentGroups) { componentGroup.archetypeGroups = GetArchetypeGroups(componentGroup.includes, componentGroup.excludes).ToArray(); } return(archetype); }
/// <inheritdoc/> public bool Equals(Archetype other) { return(handle == other.handle); }
static World() { defaultArchetype = new Archetype(0); }
/// <summary> /// Get the component types of an archetype. /// </summary> /// <param name="archetype">Archetype</param> /// <returns>Component types of archetype</returns> public Type[] GetArchetypeTypes(Archetype archetype) => archetypeMap[archetype];
/// <summary> /// Destroy all entities adhering to an archetype. /// </summary> /// <param name="archetype">The <see cref="Entity"/> of the entities to remove</param> public void DestroyAll(Archetype archetype) { var archetypeGroup = archetypeGroupMap[archetype]; archetypeGroup.Clear(); }
/// <summary> /// Derive a new archetype from an existing archetype. /// </summary> /// <param name="parent">Archetype to derive from</param> /// <param name="types">New component types to introduce</param> /// <returns>New archetype derived from parent</returns> public Archetype CreateArchetype(Archetype parent, params Type[] types) { var parentTypes = archetypeMap[parent]; return(CreateArchetype(types.Union(parentTypes).ToArray())); }