Exemple #1
0
        /// <summary>
        /// Removes an item which was inserted with the given bounds from the Bin.
        /// </summary>
        /// <param name="item">The item to remove.</param>
        /// <param name="bounds">The bounds that the item was inserted with.</param>
        public void Remove(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)
                        {
                            continue;
                        }

                        cell.Remove(item);

                        if (cell.Count == 0)
                        {
                            PoolHelper <LinkedList <T> > .Despawn(cell);

                            _grid[x, y, z] = null;
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static void Update(PooledLinkedList <BetterCoroutine> coroutines)
        {
            var scaledTime   = BetterTime.Time;
            var unscaledTime = BetterTime.UnscaledTime;

            _current = coroutines.First;
            while (_current != null)
            {
                var routine = _current.Value;

                var shouldContinue = UpdateCoroutine(scaledTime, unscaledTime, routine);

                if (!shouldContinue)
                {
                    var next = _current.Next;

                    Stop(routine);
                    coroutines.Remove(_current);
                    IdToCoroutine.Remove(routine.Id);
                    PoolHelper <BetterCoroutine> .Despawn(routine);

                    _current = next;
                    continue;
                }

                _current = _current.Next;
            }
        }
        /// <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 #4
0
        /// <summary>
        /// Stops all coroutines that are running in the specified update loop.
        /// </summary>
        public static void StopAll(int updateLoopId)
        {
            var coroutines = GetList(updateLoopId);

            if (coroutines == null)
            {
                return;
            }

            var isInUpdate = _current != null && _current.Value.UpdateLoopId == updateLoopId;

            var current = coroutines.First;

            while (current != null)
            {
                var next    = current.Next;
                var routine = current.Value;

                Stop(routine);

                if (!isInUpdate)
                {
                    IdToCoroutine.Remove(routine.Id);
                    PoolHelper <BetterCoroutine> .Despawn(routine);

                    coroutines.Remove(current);
                }

                current = next;
            }
        }
Exemple #5
0
        private static void Start(BetterCoroutine coroutine)
        {
            var scaledTime   = GetTime(coroutine.UpdateLoop, false);
            var unscaledTime = GetTime(coroutine.UpdateLoop, true);

            var continueRunning = UpdateCoroutine(scaledTime, unscaledTime, coroutine);

            if (!continueRunning)
            {
                PoolHelper <BetterCoroutine> .Despawn(coroutine);

                return;
            }

            IdToCoroutine[coroutine.Id] = coroutine;

            var list = GetList(coroutine.UpdateLoop);

            if (_current != null && _current.List == list.BackingList)
            {
                list.AddBefore(_current, coroutine);
            }
            else
            {
                list.AddFirst(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);
        }
Exemple #7
0
        /// <summary>
        /// Removes all items from the Bin.
        /// </summary>
        public void Clear()
        {
            for (var y = 0; y < _grid.Height; y++)
            {
                for (var x = 0; x < _grid.Width; x++)
                {
                    var cell = _grid[x, y];

                    if (cell == null)
                    {
                        continue;
                    }

                    cell.Clear();
                    PoolHelper <LinkedList <T> > .Despawn(cell);

                    _grid[x, y] = null;
                }
            }
        }
Exemple #8
0
        private static void Start(BetterCoroutine coroutine)
        {
            var scaledTime   = GetTime(coroutine.UpdateLoopId, false);
            var unscaledTime = GetTime(coroutine.UpdateLoopId, true);

            IdToCoroutine[coroutine.Id] = coroutine;

            var list = GetList(coroutine.UpdateLoopId);

            if (list == null)
            {
                UpdateLoopToCoroutines[coroutine.UpdateLoopId] = list = new PooledLinkedList <BetterCoroutine>(SharedNodePool);
                ManagedUpdate.AddListener(coroutine.UpdateLoopId, Instance);
            }

            var node = list.AddFirst(coroutine);

            var prevCurrent = _current;

            _current = node;

            var continueRunning = UpdateCoroutine(scaledTime, unscaledTime, coroutine);

            if (!continueRunning)
            {
                IdToCoroutine.Remove(coroutine.Id);
                list.Remove(coroutine);
                if (coroutine.Parent != null)
                {
                    coroutine.Parent.Child = null;
                }
                PoolHelper <BetterCoroutine> .Despawn(coroutine);
            }

            _current = prevCurrent;
        }