public void TestQueue()
        {
            var queue = new IndexedQueue <int>(new int[] { 1, 2, 3 });

            queue.Count.Should().Be(3);

            queue.Enqueue(4);
            queue.Count.Should().Be(4);
            queue.Peek().Should().Be(1);
            queue.TryPeek(out var a).Should().BeTrue();
            a.Should().Be(1);

            queue[0].Should().Be(1);
            queue[1].Should().Be(2);
            queue[2].Should().Be(3);
            queue.Dequeue().Should().Be(1);
            queue.Dequeue().Should().Be(2);
            queue.Dequeue().Should().Be(3);
            queue[0] = 5;
            queue.TryDequeue(out a).Should().BeTrue();
            a.Should().Be(5);

            queue.Enqueue(4);
            queue.Clear();
            queue.Count.Should().Be(0);
        }
        public void TestDefault()
        {
            var queue = new IndexedQueue <int>(10);

            queue.Count.Should().Be(0);

            queue = new IndexedQueue <int>();
            queue.Count.Should().Be(0);
            queue.TrimExcess();
            queue.Count.Should().Be(0);

            queue = new IndexedQueue <int>(Array.Empty <int>());
            queue.Count.Should().Be(0);
            queue.TryPeek(out var a).Should().BeFalse();
            a.Should().Be(0);
            queue.TryDequeue(out a).Should().BeFalse();
            a.Should().Be(0);

            Assert.ThrowsException <InvalidOperationException>(() => queue.Peek());
            Assert.ThrowsException <InvalidOperationException>(() => queue.Dequeue());
            Assert.ThrowsException <IndexOutOfRangeException>(() => _         = queue[-1]);
            Assert.ThrowsException <IndexOutOfRangeException>(() => queue[-1] = 1);
            Assert.ThrowsException <IndexOutOfRangeException>(() => _         = queue[1]);
            Assert.ThrowsException <IndexOutOfRangeException>(() => queue[1]  = 1);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => new IndexedQueue <int>(-1));
        }
示例#3
0
    void Update()
    {
        _recordPosTimer -= Time.deltaTime;
        if (_recordPosTimer <= 0)
        {
            _recordPosTimer = RecordFreuency + _recordPosTimer;

            if (_positionQueue.Count >= GetQueueMaxSize())
            {
                _positionQueue.Dequeue();
                _positionQueue.Enqueue(transform.position.ToVec2XY());
            }
        }

        for (int i = 0; i < _tailPieces.Count; ++i)
        {
            Physics2D.IgnoreCollision(_collider, _tailPieces[i].GetComponent <Collider2D>(), true);// i == 0);
            _tailPieces[i].transform.position = _positionQueue[_positionQueue.Count - 1 - i].ToVec3XY();
        }
    }
示例#4
0
        /** Process graph updating work items.
         * Process all queued work items, e.g graph updates and the likes.
         *
         * \returns
         * - false if there are still items to be processed.
         * - true if the last work items was processed and pathfinding threads are ready to be resumed.
         *
         * \see AddWorkItem
         * \see threadSafeUpdateState
         * \see Update
         */
        public bool ProcessWorkItems(bool force)
        {
            if (workItemsInProgressRightNow)
            {
                throw new System.Exception("Processing work items recursively. Please do not wait for other work items to be completed inside work items. " +
                                           "If you think this is not caused by any of your scripts, this might be a bug.");
            }

            workItemsInProgressRightNow = true;
            while (workItems.Count > 0)
            {
                // Working on a new batch
                if (!workItemsInProgress)
                {
                    workItemsInProgress     = true;
                    queuedWorkItemFloodFill = false;
                }

                // Peek at first item in the queue
                AstarWorkItem itm = workItems[0];

                // Call init the first time the item is seen
                if (itm.init != null)
                {
                    itm.init();
                    itm.init = null;
                }

                if (itm.initWithContext != null)
                {
                    itm.initWithContext(this);
                    itm.initWithContext = null;
                }

                // Make sure the item in the queue is up to date
                workItems[0] = itm;

                bool status;
                try {
                    if (itm.update != null)
                    {
                        status = itm.update(force);
                    }
                    else if (itm.updateWithContext != null)
                    {
                        status = itm.updateWithContext(this, force);
                    }
                    else
                    {
                        status = true;
                    }
                } catch {
                    workItems.Dequeue();
                    workItemsInProgressRightNow = false;
                    throw;
                }

                if (!status)
                {
                    if (force)
                    {
                        Debug.LogError("Misbehaving WorkItem. 'force'=true but the work item did not complete.\nIf force=true is passed to a WorkItem it should always return true.");
                    }

                    // Still work items to process
                    workItemsInProgressRightNow = false;
                    return(false);
                }
                else
                {
                    workItems.Dequeue();
                }
            }

            EnsureValidFloodFill();

            workItemsInProgressRightNow = false;
            workItemsInProgress         = false;
            return(true);
        }
        bool ProcessWorkItems(bool force, bool sendEvents)
        {
            if (workItemsInProgressRightNow)
            {
                throw new System.Exception("Processing work items recursively. Please do not wait for other work items to be completed inside work items. " +
                                           "If you think this is not caused by any of your scripts, this might be a bug.");
            }

            // Make sure the physics engine data is up to date.
            // Graph updates may use physics methods and it is very confusing if they
            // do not always pick up the latest changes made to the scene.
            UnityEngine.Physics.SyncTransforms();
            UnityEngine.Physics2D.SyncTransforms();

            workItemsInProgressRightNow = true;
            astar.data.LockGraphStructure(true);
            while (workItems.Count > 0)
            {
                // Working on a new batch
                if (!workItemsInProgress)
                {
                    workItemsInProgress = true;
                }

                // Peek at first item in the queue
                AstarWorkItem itm = workItems[0];
                bool          status;

                try {
                    // Call init the first time the item is seen
                    if (itm.init != null)
                    {
                        itm.init();
                        itm.init = null;
                    }

                    if (itm.initWithContext != null)
                    {
                        itm.initWithContext(this);
                        itm.initWithContext = null;
                    }

                    // Make sure the item in the queue is up to date
                    workItems[0] = itm;

                    if (itm.update != null)
                    {
                        status = itm.update(force);
                    }
                    else if (itm.updateWithContext != null)
                    {
                        status = itm.updateWithContext(this, force);
                    }
                    else
                    {
                        status = true;
                    }
                } catch {
                    workItems.Dequeue();
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    throw;
                }

                if (!status)
                {
                    if (force)
                    {
                        Debug.LogError("Misbehaving WorkItem. 'force'=true but the work item did not complete.\nIf force=true is passed to a WorkItem it should always return true.");
                    }

                    // Still work items to process
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    return(false);
                }
                else
                {
                    workItems.Dequeue();
                }
            }

            if (sendEvents)
            {
                Profiler.BeginSample("PostUpdate");
                if (anyGraphsDirty)
                {
                    GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdateBeforeAreaRecalculation);
                }
                Profiler.EndSample();

                EnsureValidFloodFill();

                Profiler.BeginSample("PostUpdate");
                if (anyGraphsDirty)
                {
                    GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
                    if (OnGraphsUpdated != null)
                    {
                        OnGraphsUpdated();
                    }
                }
                Profiler.EndSample();
            }

            // Reset flags at the end
            anyGraphsDirty     = false;
            preUpdateEventSent = false;

            workItemsInProgressRightNow = false;
            workItemsInProgress         = false;
            astar.data.UnlockGraphStructure();
            return(true);
        }
示例#6
0
        /// <summary>
        /// Process graph updating work items.
        /// Process all queued work items, e.g graph updates and the likes.
        ///
        /// Returns:
        /// - false if there are still items to be processed.
        /// - true if the last work items was processed and pathfinding threads are ready to be resumed.
        ///
        /// See: AddWorkItem
        /// See: threadSafeUpdateState
        /// See: Update
        /// </summary>
        public bool ProcessWorkItems(bool force)
        {
            if (workItemsInProgressRightNow)
            {
                throw new System.Exception("Processing work items recursively. Please do not wait for other work items to be completed inside work items. " +
                                           "If you think this is not caused by any of your scripts, this might be a bug.");
            }

            UnityEngine.Physics2D.SyncTransforms();
            workItemsInProgressRightNow = true;
            astar.data.LockGraphStructure(true);
            while (workItems.Count > 0)
            {
                // Working on a new batch
                if (!workItemsInProgress)
                {
                    workItemsInProgress     = true;
                    queuedWorkItemFloodFill = false;
                }

                // Peek at first item in the queue
                AstarWorkItem itm = workItems[0];
                bool          status;

                try {
                    // Call init the first time the item is seen
                    if (itm.init != null)
                    {
                        itm.init();
                        itm.init = null;
                    }

                    if (itm.initWithContext != null)
                    {
                        itm.initWithContext(this);
                        itm.initWithContext = null;
                    }

                    // Make sure the item in the queue is up to date
                    workItems[0] = itm;

                    if (itm.update != null)
                    {
                        status = itm.update(force);
                    }
                    else if (itm.updateWithContext != null)
                    {
                        status = itm.updateWithContext(this, force);
                    }
                    else
                    {
                        status = true;
                    }
                } catch {
                    workItems.Dequeue();
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    throw;
                }

                if (!status)
                {
                    if (force)
                    {
                        Debug.LogError("Misbehaving WorkItem. 'force'=true but the work item did not complete.\nIf force=true is passed to a WorkItem it should always return true.");
                    }

                    // Still work items to process
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    return(false);
                }
                else
                {
                    workItems.Dequeue();
                }
            }

            EnsureValidFloodFill();

            Profiler.BeginSample("PostUpdate");
            if (anyGraphsDirty)
            {
                GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
            }
            Profiler.EndSample();

            anyGraphsDirty = false;
            workItemsInProgressRightNow = false;
            workItemsInProgress         = false;
            astar.data.UnlockGraphStructure();
            return(true);
        }