Exemple #1
0
        /// <summary>
        /// Inserts an item with the given bounds into the Bin.
        /// </summary>
        /// <param name="item">The item to insert.</param>
        /// <param name="bounds">The bounds of the item.</param>
        public void Insert(T item, Bounds bounds)
        {
            if (IsOutOfBounds(bounds))
            {
                return;
            }

            var internalBounds = GetInternalBounds(bounds);

            for (var z = internalBounds.MinZ; z <= internalBounds.MaxZ; z++)
            {
                for (var y = internalBounds.MinY; y <= internalBounds.MaxY; y++)
                {
                    for (var x = internalBounds.MinX; x <= internalBounds.MaxX; x++)
                    {
                        var cell = _grid[x, y, z];

                        if (cell == null)
                        {
                            _grid[x, y, z] = cell = PoolHelper <LinkedList <T> > .Spawn();
                        }

                        cell.AddLast(item);
                    }
                }
            }
        }
        /// <summary>
        /// Changes the resource amount by <paramref name="delta"/>.
        /// </summary>
        /// <param name="delta">The delta to apply.</param>
        /// <param name="source">The source of the change.</param>
        /// <param name="args">Optional args that will be passed to listeners.</param>
        /// <param name="forced">Controls whether to force the change, despite modifications by listeners.</param>
        /// <returns>The resulting change in the pool.</returns>
        protected virtual float Change(float delta, object source = null, object args = null, bool forced = false)
        {
            if (_isEmpty && _emptyTillRenewed)
            {
                return(0);
            }

            var resourceEvent = PoolHelper <ResourceEvent> .Spawn();

            resourceEvent.OriginalDelta = delta;
            resourceEvent.ModifiedDelta = delta;
            resourceEvent.Source        = source;
            resourceEvent.Args          = args;

            OnPreChange.Invoke(resourceEvent);

            if (forced)
            {
                resourceEvent.ModifiedDelta = resourceEvent.OriginalDelta;
            }

            if (Mathf.Approximately(resourceEvent.ModifiedDelta, 0))
            {
                // change was nullified completely
                PoolHelper <ResourceEvent> .Despawn(resourceEvent);

                return(0);
            }

            var valueBefore = _current;

            Current += resourceEvent.ModifiedDelta;
            resourceEvent.AppliedDelta = _current - valueBefore;

            var wasEmpty = _isEmpty;

            IsEmpty = _current < 0.01f;

            var wasFull = _isFull;

            IsFull = _current > _max - 0.01f;

            OnChange.Invoke(resourceEvent);

            if (_isEmpty && _isEmpty != wasEmpty)
            {
                OnEmpty.Invoke(resourceEvent);
            }

            if (_isFull && _isFull != wasFull)
            {
                OnFull.Invoke(resourceEvent);
            }

            var appliedDelta = resourceEvent.AppliedDelta;

            PoolHelper <ResourceEvent> .Despawn(resourceEvent);

            return(appliedDelta);
        }
Exemple #3
0
        private static BetterCoroutine SpawnCoroutine(IEnumerator enumerator, int updateLoopId)
        {
            var coroutine = PoolHelper <BetterCoroutine> .Spawn();

            coroutine.Id           = GetNextId();
            coroutine.Enumerator   = enumerator;
            coroutine.UpdateLoopId = updateLoopId;
            return(coroutine);
        }
        /// <summary>
        /// Restores the pool to the specified amount, regardless of <see cref="EmptyTillRenewed"/>.
        /// </summary>
        /// <param name="toValue">The amount of resource to restore to.</param>
        /// <param name="source">The source of the change.</param>
        /// <param name="args">Optional args that will be passed to listeners.</param>
        /// <param name="forced">Controls whether to force the change, despite modifications by listeners.</param>
        /// <returns>The resulting change in the pool.</returns>
        public float Renew(float toValue, object source = null, object args = null, bool forced = false)
        {
            var before = _emptyTillRenewed;

            _emptyTillRenewed = false;
            var appliedDelta = Fill(toValue, source, args, forced);

            _emptyTillRenewed = before;

            var e = PoolHelper <ResourceEvent> .Spawn();

            e.Source = source;
            e.Args   = args;
            OnRenew.Invoke(e);
            PoolHelper <ResourceEvent> .Despawn(e);

            return(appliedDelta);
        }