Esempio n. 1
0
        /// <summary> Destroys the given <see cref="Entity" />. </summary>
        /// <param name="entity"> [in,out] The <see cref="Entity" />. </param>
        public void Destroy(ref Entity entity)
        {
            // ReSharper disable once InconsistentlySynchronizedField
            if (_entityMap.TryGetValue(entity, out int index))
            {
                lock (_thisLock)
                {
                    _entityMap.Remove(entity);

                    _entitiesCount--;
                    _entities[index]          = _entities[_entitiesCount];
                    _entities[_entitiesCount] = null !;
                }
                lock (_toRemove)
                {
                    _toRemove.Add(entity);
                }
            }

            foreach (object component in entity.Components)
            {
                EntityComponentPool.Release((dynamic)component);
            }

            _entityPool.Release(entity);

            entity = null !;
        }
Esempio n. 2
0
        /// <summary> Adds a <typeparamref name="TComponent" /> to the specified <paramref name="entity" /> instance. </summary>
        /// <typeparam name="TComponent"> Type of the component. </typeparam>
        /// <param name="entity">     The entity. </param>
        /// <param name="usePooling"> True to use pooling. </param>
        /// <param name="action">     (Optional) The action. </param>
        /// <returns> An <see cref="EntityManager" />. </returns>
        public EntityManager Add <TComponent>(Entity entity, bool usePooling, Action <TComponent>?action = null)
            where TComponent : class
        {
            TComponent component = usePooling
                ? EntityComponentPool <TComponent> .Take()
                : EntityComponentPool <TComponent> .Create();

            action?.Invoke(component);
            entity.Add(component);

            if (!entity.IsInitialized)
            {
                return(this);
            }

            lock (_toChanged)
            {
                _toChanged.Add(entity);
            }

            return(this);
        }
Esempio n. 3
0
        /// <summary> Removes a <typeparamref name="TComponent" /> from the specified <paramref name="entity" /> instance. </summary>
        /// <typeparam name="TComponent"> Type of the component. </typeparam>
        /// <param name="entity">     The entity. </param>
        /// <param name="usePooling"> True to use pooling. </param>
        /// <param name="action">     (Optional) The action. </param>
        /// <returns> An <see cref="EntityManager" />. </returns>
        public EntityManager Remove <TComponent>(Entity entity, bool usePooling, Action <TComponent>?action = null)
            where TComponent : class
        {
            if (entity.Get(out TComponent component))
            {
                entity.Remove <TComponent>();
                action?.Invoke(component);
                if (usePooling)
                {
                    EntityComponentPool <TComponent> .Release(component);
                }

                if (!entity.IsInitialized)
                {
                    return(this);
                }

                lock (_toChanged)
                {
                    _toChanged.Add(entity);
                }
            }
            return(this);
        }
Esempio n. 4
0
 public static void Release <TComponent>(TComponent component)
     where TComponent : class
 {
     EntityComponentPool <TComponent> .Release(component);
 }