Example #1
0
        public void RemoveComponentFromEntity(Entity e, int componentTypeId)
        {
            int oldComponentTypesBitField = e.ComponentTypesBitField;

            // (1) update componentlookupids, and return component to pool
#if ALTERNATE_LAYOUT
            int iIndex = e.LiveId + componentTypeId * MaxEntities;
#else
            int iIndex = e.LiveId * EntityManager.MaxComponentTypes + componentTypeId;
#endif
            int componentLookupId = componentLookupIdsPerEntity[iIndex];
            if (componentLookupId != -1)
            {
                componentManagers[componentTypeId].FreeAtComponentLookupId(componentLookupId);
                componentLookupIdsPerEntity[iIndex] = -1;
            }
            else
            {
                Debug.Assert(false, "removing non-existent component. Is this ok?");
            }
            // (2) update e.ComponentTypesBitField by removing the bit for the removed component type.
            e.ComponentTypesBitField &= ~ComponentTypeIdHelper.GetBit(componentTypeId);
            // (3) Ensure its removed from the appropriate systems
            UpdateEntityInSystems(e, oldComponentTypesBitField);
        }
Example #2
0
        public EntitySystem(int updateOrder, int[] requiredComponentTypeIds, int[] optionalButOneRequiredComponentTypeIds, uint[] supportedMessages = null, SystemFlags flags = SystemFlags.None)
        {
            Flags       = flags;
            UpdateOrder = updateOrder;
            enabled     = true;
            foreach (int componentTypeId in requiredComponentTypeIds)
            {
                RequiredComponentTypesBitField |= ComponentTypeIdHelper.GetBit(componentTypeId);
            }
            if (optionalButOneRequiredComponentTypeIds == null)
            {
                RequireAtLeastOneComponentTypesBitField = ComponentTypeIdHelper.AllMask;
            }
            else
            {
                foreach (int componentTypeId in optionalButOneRequiredComponentTypeIds)
                {
                    RequireAtLeastOneComponentTypesBitField |= ComponentTypeIdHelper.GetBit(componentTypeId);
                }
            }

            liveIdToSlot = new int[EntityManager.MaxEntities];
            slotToLiveId = new int[EntityManager.MaxEntities];
            for (int i = 0; i < liveIdToSlot.Length; i++)
            {
                liveIdToSlot[i] = -1;
                slotToLiveId[i] = -1;
            }
            this.SupportedMessages = supportedMessages;

            entityIdCollection = new EntityIdCollection(this);
        }
Example #3
0
        // This shouldn't be public, since it doesn't do proper add/remove from systems.
        private Component GetOrAllocateComponentAndUpdateBitField(Entity e, int componentTypeId, out bool allocatedNew)
        {
            //int liveIndex = liveIdRoster[e.LiveId];
            int liveIndex = e.LiveId;

            Debug.Assert(e == entityPool[liveIndex], "Entity has wrong id");
            ComponentManagerBase componentManager = componentManagers[componentTypeId];

#if ALTERNATE_LAYOUT
            int index = liveIndex + componentTypeId * MaxEntities;
#else
            int index = liveIndex * EntityManager.MaxComponentTypes + componentTypeId;
#endif
            int componentTypeLookupId = componentLookupIdsPerEntity[index];
            if (componentTypeLookupId == -1)
            {
                // Allocate a new component for this entity
                short     componentLookupId;
                Component component = componentManager.AllocateComponentForEntity(e, out componentLookupId);
                componentLookupIdsPerEntity[index] = componentLookupId;
                allocatedNew = true;

                // Make sure the entity is tagged as having this component
                e.ComponentTypesBitField |= ComponentTypeIdHelper.GetBit(componentTypeId);

                return(component);
            }
            else
            {
                // Retrieve
                allocatedNew = false;
                return(componentManager.GetComponentAt(componentTypeLookupId));
            }
        }