public virtual SetItemResult <T> Set(IntVector position, T item, SetItemResult <T> result)
        {
            var halfSize = HalfSize;
            var x        = position.X;
            var y        = position.Y;
            var z        = position.Z;

            // 0|0|0
            if (x < halfSize && y < halfSize && z < halfSize)
            {
                return(GetOrCreateChild(0).Set(position, item, result));
            }

            // 1|0|0
            if (x >= halfSize && y < halfSize && z < halfSize)
            {
                return(GetOrCreateChild(1).Set(new IntVector(x - halfSize, y, z), item, result));
            }

            // 0|1|0
            if (x < halfSize && y >= halfSize && z < halfSize)
            {
                return(GetOrCreateChild(2).Set(new IntVector(x, y - halfSize, z), item, result));
            }

            // 1|1|0
            if (x >= halfSize && y >= halfSize && z < halfSize)
            {
                return(GetOrCreateChild(3).Set(new IntVector(x - halfSize, y - halfSize, z), item, result));
            }

            // 0|0|1
            if (x < halfSize && y < halfSize && z >= halfSize)
            {
                return(GetOrCreateChild(4).Set(new IntVector(x, y, z - halfSize), item, result));
            }

            // 1|0|1
            if (x >= halfSize && y < halfSize && z >= halfSize)
            {
                return(GetOrCreateChild(5).Set(new IntVector(x - halfSize, y, z - halfSize), item, result));
            }

            // 0|1|1
            if (x < halfSize && y >= halfSize && z >= halfSize)
            {
                return(GetOrCreateChild(6).Set(new IntVector(x, y - halfSize, z - halfSize), item, result));
            }

            // 1|1|1
            if (x >= halfSize && y >= halfSize && z >= halfSize)
            {
                return(GetOrCreateChild(7).Set(new IntVector(x - halfSize, y - halfSize, z - halfSize), item, result));
            }

            throw new InvalidOperationException("Logic Error.");
        }
Exemplo n.º 2
0
        public override SetItemResult <T> Set(IntVector position, T item, SetItemResult <T> result)
        {
            var absolutePosition = position;

            position -= Offset;

            if (Extend(position.X, position.Y, position.Z, result))
            {
                return(Set(absolutePosition, item, result));
            }

            return(base.Set(position, item, result));
        }
Exemplo n.º 3
0
        private bool Extend(int x, int y, int z, SetItemResult <T> result)
        {
            var xShift = x < 0 ? -1 : (x >= Size ? 1 : 0);
            var yShift = y < 0 ? -1 : (y >= Size ? 1 : 0);
            var zShift = z < 0 ? -1 : (z >= Size ? 1 : 0);

            if (xShift == 0 && yShift == 0 && zShift == 0)
            {
                return(false);
            }

            DoExtend(Math.Min(0, xShift), Math.Min(0, yShift), Math.Min(0, zShift), result);

            return(true);
        }
Exemplo n.º 4
0
        private void DoExtend(int shiftX, int shiftY, int shiftZ, SetItemResult <T> result)
        {
            var prevSize     = Size;
            var prevChildren = Children;

            var offsetShift = new IntVector(shiftX, shiftY, shiftZ) * prevSize;

            result.OffsetShift = result.OffsetShift + offsetShift;
            Offset            += offsetShift;

            SetSize(prevSize * 2);

            var child      = new BinaryGridBranch <T>(prevSize, prevChildren);
            var childIndex = -shiftX + 2 * -shiftY + 4 * -shiftZ;

            Children             = new IBinarySubGrid <T> [8];
            Children[childIndex] = child;
        }
Exemplo n.º 5
0
 public SetItemResult <T> Set(IntVector position, T item, SetItemResult <T> result)
 {
     result.RemovedItem = _items[position.X, position.Y, position.Z];
     _items[position.X, position.Y, position.Z] = item;
     return(result);
 }