Beispiel #1
0
        public bool RemoveEntity(IWorldEntity entity)
        {
            Contract.Requires(entity != null);

            if (IsLeaf)
            {
                _entities.Remove(entity.Guid);
                entity.PostAsync(() => entity.Node = null);

                return(true);
            }

            var pos = entity.Position;

            for (var i = 0; i < 2; i++)
            {
                for (var j = 0; j < 2; j++)
                {
                    var node = _children[i, j];
                    if (node.Bounds.Contains(pos) != ContainmentType.Contains)
                    {
                        continue;
                    }

                    return(node.RemoveEntity(entity));
                }
            }

            return(false);
        }
Beispiel #2
0
        public bool RemoveEntity(IWorldEntity entity)
        {
            Contract.Requires(entity != null);

            if (IsLeaf)
            {
// ReSharper disable PossibleNullReferenceException
                _entities.Remove(entity.Guid);
// ReSharper restore PossibleNullReferenceException
                entity.PostAsync(() => entity.Node = null);

                return(true);
            }

// ReSharper disable PossibleNullReferenceException
            var pos = entity.Position;

// ReSharper restore PossibleNullReferenceException
            for (var i = 0; i < 2; i++)
            {
                for (var j = 0; j < 2; j++)
                {
                    var node = _children[i, j];
                    if (node.Bounds.Contains(pos) != ContainmentType.Contains)
                    {
                        continue;
                    }

                    return(node.RemoveEntity(entity));
                }
            }

            return(false);
        }
Beispiel #3
0
        public bool AddEntity(IWorldEntity entity)
        {
            if (!_isLeaf)
            {
                var node = GetChildContaining(entity);
                _numEntities++;
                return(node.AddEntity(entity));
            }

            if (_bucket.Count < _partitionThreshold)
            {
                _bucket.Add(entity);
                entity.PostAsync(() => entity.Node = this);
                return(true);
            }

            Partition();
            return(AddEntity(entity));
        }
Beispiel #4
0
        public bool RemoveEntity(IWorldEntity entity)
        {
            if (isLeaf)
            {
                bucket.Remove(entity);
                entity.PostAsync(() => entity.Node = null);

                if (parent != null)
                {
                    parent.BalanceIfNeeded();
                }
                return(true);
            }

            // Yeah, now we need to check if our children have it, and pass it on
            var node = GetChildContaining(entity);

            numEntities--;
            return(node.RemoveEntity(entity));
        }
Beispiel #5
0
        public bool RemoveEntity(IWorldEntity entity)
        {
            if (_isLeaf)
            {
                _bucket.Remove(entity);
                entity.PostAsync(() => entity.Node = null);

                if (Parent != null)
                    Parent.BalanceIfNeeded();
                return true;
            }

            // Yeah, now we need to check if our children have it, and pass it on
            var node = GetChildContaining(entity);
            _numEntities--;
            return node.RemoveEntity(entity);
        }
Beispiel #6
0
        public bool AddEntity(IWorldEntity entity)
        {
            if (!_isLeaf)
            {
                var node = GetChildContaining(entity);
                _numEntities++;
                return node.AddEntity(entity);
            }

            if (_bucket.Count < _partitionThreshold)
            {
                _bucket.Add(entity);
                entity.PostAsync(() => entity.Node = this);
                return true;
            }

            Partition();
            return AddEntity(entity);
        }