示例#1
0
        public void ShouldCreateChunkArray()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specifcation = new EntitySpec(
                ComponentType <Position> .Type
                );

            using var chunkArray = new EntityChunkList(_logFactory, memory, specifcation, 0);
            using var entityPool = new EntityPool(_logFactory, memory);

            var id0 = entityPool.TakeRef();
            var id1 = entityPool.TakeRef();

            //act
            chunkArray.Create(id0);
            chunkArray.Create(id1);

            //assert
            chunkArray.EntityCount.ShouldBe(2);
            id0.Index.ShouldBe(0);
            id1.Index.ShouldBe(1);
            id0.ChunkIndex.ShouldBe(0);
            id1.ChunkIndex.ShouldBe(0);
        }
示例#2
0
        public void ShouldDeleteAndMove()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specifcation = new EntitySpec(
                ComponentType <Position> .Type
                );

            using var chunkArray = new EntityChunkList(_logFactory, memory, specifcation, 0);
            using var entityPool = new EntityPool(_logFactory, memory);

            var id0 = entityPool.TakeRef();
            var id1 = entityPool.TakeRef();

            //act
            chunkArray.Create(id0);
            chunkArray.Create(id1);
            var span = chunkArray.AllChunks[0].PackedArray.GetComponentData <Position>();

            span[id1.Index] = new Position(10, 20);
            chunkArray.Delete(id0);

            //assert
            span[id0.Index].X.ShouldBe(10);
            span[id0.Index].Y.ShouldBe(20);
        }
        public void ShouldDeleteOneEntity()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specification = new EntitySpec(ComponentType <Position> .Type);

            using var entityChunk = new EntityChunk(_logFactory, memory, specification, 0, 0);
            var span = entityChunk.PackedArray.GetComponentData <Position>(0);

            using var entityPool = new EntityPool(_logFactory, memory);

            //act
            span[0] = new Position(1);
            span[1] = new Position(2);

            var id0 = entityPool.TakeRef();
            var id1 = entityPool.TakeRef();

            var free = entityChunk.Free;

            entityChunk.Create(id0);
            entityChunk.Create(id1);
            entityChunk.Delete(id0);

            //assert
            entityChunk.Count.ShouldBe(1);
            entityChunk.Free.ShouldBe(free - 1);
            //entityChunk.Get(id0).ShouldBe(id1);
            span[0].ShouldBe(new Position(2));
        }
示例#4
0
        public unsafe void ShoudlCopyPtrData()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specifcation = new EntitySpec(
                ComponentType <Position> .Type
                );

            var specIndex = 0;

            using var chunkArray = new EntityChunkList(_logFactory, memory, specifcation, specIndex);
            using var entityPool = new EntityPool(_logFactory, memory);

            var entity = entityPool.TakeRef();;

            chunkArray.Create(entity);
            var componentType  = stackalloc[] { ComponentType <Position> .Type };
            var componentIndex = chunkArray.Specification.GetComponentIndex(*componentType);
            var span           = chunkArray.AllChunks[entity.ChunkIndex].PackedArray.GetComponentData <Position>();

            //act
            var ptr = stackalloc[] { new Position(100, 100), new Position(200, 200), new Position(400, 100), new Position(100, 400) };
            var src = (void *)ptr;

            Span <EntityRef> entityRefs = stackalloc[] { entity };

            chunkArray.Copy(specIndex, componentType, ref src, entityRefs, false);

            //assert
            span[0].X.ShouldBe(100);
            span[0].Y.ShouldBe(100);
        }
        public void ShouldCreateOneEntity()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specification = new EntitySpec(
                ComponentType <Position> .Type,
                ComponentType <Velocity> .Type
                );

            using var entityChunk = new EntityChunk(_logFactory, memory, specification, 0, 0);
            using var entityPool  = new EntityPool(_logFactory, memory);

            var id0 = entityPool.TakeRef();

            //act
            var free = entityChunk.Free;

            entityChunk.Create(id0);

            //assert
            id0.Index.ShouldBe(0);
            entityChunk.Entities[0].ShouldBe(id0);
            entityChunk.Count.ShouldBe(1);
            entityChunk.Free.ShouldBe(free - 1);
        }
示例#6
0
        public unsafe void ShouldCopyMoreThanOne()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specifcation = new EntitySpec(
                ComponentType <Position> .Type
                );

            var specIndex = 0;

            using var chunkArray = new EntityChunkList(_logFactory, memory, specifcation, specIndex);
            using var entityPool = new EntityPool(_logFactory, memory);
            var entity0 = entityPool.TakeRef();
            var entity1 = entityPool.TakeRef();
            var entity2 = entityPool.TakeRef();
            var entity3 = entityPool.TakeRef();

            chunkArray.Create(entity0);
            chunkArray.Create(entity1);
            chunkArray.Create(entity2);
            chunkArray.Create(entity3);

            var componentType  = stackalloc[] { ComponentType <Position> .Type };
            var componentIndex = chunkArray.Specification.GetComponentIndex(*componentType);
            var span           = chunkArray.AllChunks[entity0.ChunkIndex].PackedArray.GetComponentData <Position>();

            var ptr = stackalloc[] { new Position(100, 100), new Position(200, 200), new Position(400, 100), new Position(100, 400) };
            var src = (void *)ptr;

            Span <EntityRef> entityRefs = stackalloc[] {
                entity0,
                entity1,
                //swapping 2 and 3 to see if we can resume correctly
                entity3,
                entity2,
            };

            //act
            chunkArray.Copy(specIndex, componentType, ref src, entityRefs, true);

            //assert
            span[0].X.ShouldBe(100);
            span[0].Y.ShouldBe(100);
            span[1].X.ShouldBe(200);
            span[1].Y.ShouldBe(200);
            span[3].X.ShouldBe(400);
            span[3].Y.ShouldBe(100);
            span[2].X.ShouldBe(100);
            span[2].Y.ShouldBe(400);
        }

        // public void ShouldMoveToAnotherArray()
        // {
        //     //arrange
        //     var specifcation = new EntitySpec(
        //         ComponentType<Position>.Type
        //     );

        //     var srcArray = new EntityChunkArray(_logFactory, specifcation);
        //     var dstArray = new EntityChunkArray(_logFactory, specifcation);

        //     //act
        //     var srcIndex = srcArray.Create(1, out var chunkIndex);
        //     Console.WriteLine(chunkIndex);
        //     var srcSpan = srcArray.AllChunks[chunkIndex].PackedArray.GetComponentSpan<Position>();
        //     srcSpan[srcIndex] = new Position(10, 20);

        //     var tempIndex = dstArray.Create(2, out var tempChunkIndex); //pushing the datain dst to check that it inserted correctly
        //     EntityChunkArray.MoveTo(1, srcArray, chunkIndex, srcIndex, dstArray, out var dstChunkIndex, out var dstIndex);

        //     //assert
        //     chunkIndex.ShouldBe(0);
        //     srcIndex.ShouldBe(0);
        //     srcArray.EntityCount.ShouldBe(0);

        //     dstChunkIndex.ShouldBe(0);
        //     dstIndex.ShouldBe(1);
        //     dstArray.EntityCount.ShouldBe(2);
        //     var dstSpan = dstArray.AllChunks[dstChunkIndex].PackedArray.GetComponentSpan<Position>();
        //     dstSpan[dstIndex].X.ShouldBe(10);
        //     dstSpan[dstIndex].Y.ShouldBe(20);

        // }
    }