Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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;
        }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
 /// <inheritdoc/>
 public bool Equals(Archetype other)
 {
     return(handle == other.handle);
 }
Beispiel #6
0
 static World()
 {
     defaultArchetype = new Archetype(0);
 }
Beispiel #7
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];
Beispiel #8
0
        /// <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();
        }
Beispiel #9
0
        /// <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()));
        }