예제 #1
0
        /// <summary>
        /// Adds component to entity.
        /// </summary>
        /// <param name="entity">Entity.</param>
        public T AddComponent <T> (int entity) where T : class, IEcsComponent
        {
            var componentId = GetComponentIndex <T> ();
            var entityData  = _entities[entity];

            if (entityData.Mask.GetBit(componentId))
            {
                return(entityData.Components[componentId] as T);
            }
            _delayedUpdates.Add(new DelayedUpdate(DelayedUpdate.Op.AddComponent, entity, componentId));

            EcsComponentPool pool;

            if (!_componentPools.TryGetValue(componentId, out pool))
            {
                pool = new EcsComponentPool(typeof(T));
                _componentPools[componentId] = pool;
            }
            var component = pool.Get() as T;

            while (entityData.ComponentsCount <= componentId)
            {
                entityData.Components.Add(null);
                entityData.ComponentsCount++;
            }
            entityData.Components[componentId] = component;
            return(component);
        }
        /// <summary>
        /// Gets component index. Useful for GetComponent() requests as second parameter for performance reason.
        /// </summary>
        /// <param name="componentType">Component type.</param>
        public int GetComponentIndex(Type componentType)
        {
#if DEBUG && !ECS_PERF_TEST
            if (componentType == null || !typeof(IEcsComponent).IsAssignableFrom(componentType) || !componentType.IsClass)
            {
                throw new Exception("Invalid component type");
            }
#endif
            int retVal;
            var type = componentType.GetHashCode();
            if (!_componentIds.TryGetValue(type, out retVal))
            {
                retVal = _componentIds.Count;
                _componentIds[type]     = retVal;
                _componentPools[retVal] = new EcsComponentPool(componentType);
            }
            return(retVal);
        }
예제 #3
0
        /// <summary>
        /// Gets component index. Useful for GetComponent() requests as second parameter for performance reason.
        /// </summary>
        /// <param name="componentType">Component type.</param>
        public int GetComponentIndex(Type componentType)
        {
#if DEBUG && !ECS_PERF_TEST
            if (componentType == null || !componentType.IsClass)
            {
                throw new Exception("Invalid component type");
            }
#endif
            int retVal;
            var type = componentType.GetHashCode();
            if (!_componentIds.TryGetValue(type, out retVal))
            {
                retVal = _componentIds.Count;
#if DEBUG && !ECS_PERF_TEST
                if (retVal >= EcsComponentMask.BitsCount)
                {
                    throw new Exception("No free room for new components - try to increase EcsComponentMask.RawLength.");
                }
#endif
                _componentIds[type]     = retVal;
                _componentPools[retVal] = new EcsComponentPool(componentType);
            }
            return(retVal);
        }