Exemple #1
0
        public unsafe void ShouldCreateManyEntities()
        {
            //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 entities = new EntityRef[Entity.ENTITY_MAX * 2];

            entityPool.Take(entities);

            //act
            chunkArray.Create(entities);

            // //assert
            // var created = createdEntities.ToArray();
            // var first = created.Take(Entity.ENTITY_MAX).ToArray();
            // var firstSet = Enumerable.Range(0, Entity.ENTITY_MAX).Select(x => new CreatedEntity(0, x)).ToArray();
            // first.ShouldBe(firstSet);

            // var second = created.Skip(Entity.ENTITY_MAX).Take(Entity.ENTITY_MAX).ToArray();
            // var secondSet = Enumerable.Range(0, Entity.ENTITY_MAX).Select(x => new CreatedEntity(1, x)).ToArray();
            // second.ShouldBe(secondSet);

            chunkArray.ChunkCount.ShouldBe(2);
            chunkArray.EntityCount.ShouldBe(entities.Length);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);

        // }
    }