Exemple #1
0
        public bool Update()
        {
            Root.UpdateDelegate(this);

            // bottom-up traversal avoids cascading HasGrandChildren() tests
            bool hasGrandChildren = false;

            foreach (var child in Children)
            {
                if (child.Update())
                {
                    hasGrandChildren = true;
                }
            }

            // nothing to do if grandchildren exist
            if (hasGrandChildren)
            {
                return(true);
            }

            // nothing to do if loading task is active
            if (IsLoading)
            {
                return(HasChildren());
            }

            // process distances
            if (!hasGrandChildren && IsDistanceTested)
            {
                Root.SwitchDelegate(this);
            }

            if (InRange)
            {
                // start load if not loaded and children state is unknown
                if (!IsLoaded && !ChildrenLoaded())
                {
                    IsLoading = true;
                    Root.LoadDelegate(this);
                    return(HasChildren());
                }

                // activate if not active and children are not active
                if (!IsActive && !ChildrenActive())
                {
                    Root.LoadedDelegate(this);
                    IsActive = true;
                    return(HasChildren());
                }

                // if active and children are ready, deactivate and activate the children
                if (IsActive && ChildrenLoaded())
                {
                    Children.ForEach(child => child.IsActive = true);
                    IsActive = false;
                    Root.UnloadDelegate(this);
                    return(HasChildren());
                }
            }
            else
            {
                Root.UnloadDelegate(this);
            }

            // distance testing
            if (IsDistanceTesting)
            {
                return(HasChildren());
            }

            if (Time.time - lastDistanceTest < 4f)
            {
                return(HasChildren());
            }
            lastDistanceTest  = Time.time;
            IsDistanceTesting = true;
            IsDistanceTested  = false;
            Task.Run(() => TaskDistanceTest());

            return(HasChildren());
        }
Exemple #2
0
        public bool Update()
        {
            var timer = new System.Diagnostics.Stopwatch();

            Root.UpdateDelegate(this);

            // bottom-up traversal avoids cascading HasGrandChildren() tests
            bool hasGrandChildren = false;

            foreach (var child in Children)
            {
                if (child.Update())
                {
                    hasGrandChildren = true;
                }
            }

            // nothing to do if grandchildren exist
            if (hasGrandChildren)
            {
                return(true);
            }

            // nothing to do if loading task is active
            if (IsLoading)
            {
                return(HasChildren());
            }

            // process distances
            if (!hasGrandChildren && IsDistanceTested)
            {
                Root.SwitchDelegate(this);
            }
            timer.Start();
            if (InRange)
            {
                // start load if not loaded and children state is unknown
                if (!IsLoaded && !ChildrenLoaded())
                {
                    IsLoading = true;
                    Root.LoadDelegate(this);
                    timer.Stop();
                    if (timer.ElapsedMilliseconds > 0)
                    {
                        Debug.Log("Quad Tree !IsLoaded && !ChildrenLoaded took " + timer.ElapsedMilliseconds + "ms to run.");
                    }
                    timer.Reset();
                    return(HasChildren());
                }

                // activate if not active and children are not active
                if (!IsActive && !ChildrenActive())
                {
                    Root.LoadedDelegate(this);
                    IsActive = true;
                    timer.Stop();
                    if (timer.ElapsedMilliseconds > 0)
                    {
                        Debug.Log("Quad Tree !IsActive && !ChildrenLoaded took " + timer.ElapsedMilliseconds + "ms to run.");
                    }
                    timer.Reset();
                    return(HasChildren());
                }

                // if active and children are ready, deactivate and activate the children
                if (IsActive && ChildrenLoaded())
                {
                    Children.ForEach(child => child.IsActive = true);
                    IsActive = false;
                    Root.UnloadDelegate(this);
                    timer.Stop();
                    if (timer.ElapsedMilliseconds > 0)
                    {
                        Debug.Log("Quad Tree IsActive && ChildrenLoaded took " + timer.ElapsedMilliseconds + "ms to run.");
                    }
                    timer.Reset();
                    return(HasChildren());
                }
            }
            else
            {
                timer.Stop();
                if (timer.ElapsedMilliseconds > 0)
                {
                    Debug.Log("Quad Tree Not In Range took " + timer.ElapsedMilliseconds + "ms to run.");
                }
                timer.Reset();
                Root.UnloadDelegate(this);
            }

            // distance testing
            if (IsDistanceTesting)
            {
                return(HasChildren());
            }

            if (Time.time - lastDistanceTest < 4f)
            {
                return(HasChildren());
            }
            lastDistanceTest  = Time.time;
            IsDistanceTesting = true;
            IsDistanceTested  = false;
            Task.Run(() => TaskDistanceTest());

            return(HasChildren());
        }