public void AddComponent <T>(Entity e, Component component) where T : Component
        {
            ComponentType type = ComponentTypeManager.GetTypeFor <T>();

            if (type.GetId() >= componentsByType.GetCapacity())
            {
                componentsByType.Set(type.GetId(), null);
            }

            Bag <Component> components = componentsByType.Get(type.GetId());

            if (components == null)
            {
                components = new Bag <Component>();
                componentsByType.Set(type.GetId(), components);
            }

            components.Set(e.GetId(), component);

            e.AddTypeBit(type.GetBit());
            if (AddedComponentEvent != null)
            {
                AddedComponentEvent(e, component);
            }
        }
        /**
         * @param e entity
         * @return the name of the group that this entity belongs to, null if none.
         */
        public String GetGroupOf(Entity e)
        {
            int entityId = e.GetId();

            if (entityId < groupByEntity.GetCapacity())
            {
                return(groupByEntity.Get(entityId));
            }
            return(null);
        }
        public Component GetComponent(Entity e, ComponentType type)
        {
            int             entityId = e.GetId();
            Bag <Component> bag      = componentsByType.Get(type.GetId());

            if (bag != null && entityId < bag.GetCapacity())
            {
                return(bag.Get(entityId));
            }
            return(null);
        }
        public void RemoveComponent(Entity e, ComponentType type)
        {
            int             entityId   = e.GetId();
            Bag <Component> components = componentsByType.Get(type.GetId());

            if (RemovedComponentEvent != null)
            {
                RemovedComponentEvent(e, components.Get(entityId));
            }
            components.Set(entityId, null);
            e.RemoveTypeBit(type.GetBit());
        }
        /**
         * Set the group of the entity.
         *
         * @param group group to set the entity into.
         * @param e entity to set into the group.
         */
        public void Set(String group, Entity e)
        {
            Remove(e);             // Entity can only belong to one group.

            Bag <Entity> entities;

            if (!entitiesByGroup.TryGetValue(group, out entities))
            {
                entities = new Bag <Entity>();
                entitiesByGroup.Add(group, entities);
            }
            entities.Add(e);

            groupByEntity.Set(e.GetId(), group);
        }
        public void Change(Entity e)
        {
            bool contains = (systemBit & e.GetSystemBits()) == systemBit;
            bool interest = (typeFlags & e.GetTypeBits()) == typeFlags;

            if (interest && !contains && typeFlags > 0)
            {
                actives.Add(e.GetId(), e);
                e.AddSystemBit(systemBit);
                Added(e);
            }
            else if (!interest && contains && typeFlags > 0)
            {
                Remove(e);
            }
        }
        private void RemoveComponentsOfEntity(Entity e)
        {
            int entityId = e.GetId();

            for (int a = 0, b = componentsByType.Size(); b > a; a++)
            {
                Bag <Component> components = componentsByType.Get(a);
                if (components != null && entityId < components.Size())
                {
                    if (RemovedComponentEvent != null)
                    {
                        RemovedComponentEvent(e, components.Get(entityId));
                    }
                    components.Set(entityId, null);
                }
            }
        }
        /**
         * Removes the provided entity from the group it is assigned to, if any.
         * @param e the entity.
         */
        public void Remove(Entity e)
        {
            int entityId = e.GetId();

            if (entityId < groupByEntity.GetCapacity())
            {
                String group = groupByEntity.Get(entityId);
                if (group != null)
                {
                    groupByEntity.Set(entityId, null);

                    Bag <Entity> entities;
                    if (entitiesByGroup.TryGetValue(group, out entities))
                    {
                        entities.Remove(e);
                    }
                }
            }
        }
        public void Remove(Entity e)
        {
            activeEntities.Set(e.GetId(), null);

            e.SetTypeBits(0);

            Refresh(e);

            RemoveComponentsOfEntity(e);

            count--;
            totalRemoved++;

            removedAndAvailable.Add(e);
            if (RemovedEntityEvent != null)
            {
                RemovedEntityEvent(e);
            }
        }
        public Bag <Component> GetComponents(Entity e)
        {
            entityComponents.Clear();
            int entityId = e.GetId();

            for (int a = 0, b = componentsByType.Size(); b > a; a++)
            {
                Bag <Component> components = componentsByType.Get(a);
                if (components != null && entityId < components.Size())
                {
                    Component component = components.Get(entityId);
                    if (component != null)
                    {
                        entityComponents.Add(component);
                    }
                }
            }
            return(entityComponents);
        }
 private void Remove(Entity e)
 {
     actives.Remove(e.GetId());
     e.RemoveSystemBit(systemBit);
     Removed(e);
 }