Exemple #1
0
        /// <summary>
        /// Get the component instance of the given component type for the given entity.
        /// </summary>
        /// <param name="entity">The entity for which you want to get the component</param>
        /// <param name="componentType">The desired component type</param>
        /// <returns>Component instance</returns>
        internal IEntityComponent GetComponent(Entity entity, ComponentType componentType)
        {
            Debug.Assert(entity != null, "Entity must not be null.");
            Debug.Assert(componentType != null, "Component type must not be null.");

            if (componentType.Id >= this.componentsByType.Capacity)
            {
                return(null);
            }

            int entityId = entity.Index;
            Bag <IEntityComponent> bag = this.componentsByType.Get(componentType.Id);

            if (bag != null &&
                entityId < bag.Capacity)
            {
                return(bag.Get(entityId));
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Get all components assigned to an entity.
        /// </summary>
        /// <param name="entity">Entity for which you want the components.</param>
        /// <returns>Bag of components</returns>
        public Bag <IEntityComponent> GetComponents(Entity entity)
        {
            Debug.Assert(entity != null, "Entity must not be null.");
            //Debug.Assert(entity.entityManager == this, "");  // TODO

            Bag <IEntityComponent> entityComponents = new Bag <IEntityComponent>();
            int entityId = entity.Index;

            for (int index = 0, b = this.componentsByType.Count; b > index; ++index)
            {
                Bag <IEntityComponent> components = this.componentsByType.Get(index);
                if (components != null &&
                    entityId < components.Count)
                {
                    IEntityComponent component = components.Get(entityId);
                    if (component != null)
                    {
                        entityComponents.Add(component);
                    }
                }
            }

            return(entityComponents);
        }