示例#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 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);
        }
示例#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);
        }
示例#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);
        }
        public unsafe void ShouldCreateManyEntities()
        {
            //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);

            Span <EntityRef> ids = stackalloc EntityRef[entityChunk.Free + 1];

            entityPool.Take(ids);

            //act
            var created = entityChunk.Create(ids);

            //assert
            entityChunk.Entities[0].ShouldBe(ids[0]);
            entityChunk.Entities[created - 1].ShouldBe(ids[created - 1]);
            entityChunk.Count.ShouldBe(created);
            entityChunk.Free.ShouldBe(0);
            created.ShouldBe(Entity.ENTITY_MAX);
        }
        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);
        }
        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));
        }
示例#8
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);

        // }
    }
        public unsafe void ShouldDeleteManyEntities()
        {
            //arrange
            using var memory = new DynamicAllocator(_logFactory);
            var specification = new EntitySpec(ComponentType <Position> .Type);

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

            var ids = new EntityRef[entityChunk.Free + 1];

            entityPool.Take(ids);

            var created = entityChunk.Create(ids);
            var span    = entityChunk.PackedArray.GetComponentData <Position>();

            for (var i = 0; i < span.Length; i++)
            {
                span[i] = new Position(i + 1);
            }

            //act
            Span <EntityRef> deleteIndicies = stackalloc EntityRef[128];

            for (var i = 0; i < deleteIndicies.Length; i++)
            {
                deleteIndicies[i] = ids[i + 128];
            }

            entityChunk.Delete(deleteIndicies);

            //assert
            var first  = Enumerable.Range(0, 128).Select(x => ids[x]).ToArray();
            var second = Enumerable.Range(0, 128).Select(x => ids[Entity.ENTITY_MAX - x - 1]).ToArray();
            var third  = Enumerable.Range(256, Entity.ENTITY_MAX - 256 - 128).Select(x => ids[x]).ToArray();
            var fourth = Enumerable.Range(0, 128).Select(x => 0).Select(x => 0u).ToArray();

            var firstSet  = entityChunk.Entities.Slice(0, 128).ToArray();
            var secondSet = entityChunk.Entities.Slice(128, 128).ToArray();
            var thirdSet  = entityChunk.Entities.Slice(256, Entity.ENTITY_MAX - 256 - 128).ToArray();

            //var fourthSet = entityChunk.Entities.Slice(Entity.ENTITY_MAX - 128, 128).ToArray();

            firstSet.ShouldBe(first);
            secondSet.ShouldBe(second);
            thirdSet.ShouldBe(third);
            //fourthSet.ShouldBe(fourth);
            entityChunk.Entities.Length.ShouldBe(Entity.ENTITY_MAX - 128);

            var firstPosition  = Enumerable.Range(0, 128).Select(x => new Position(x + 1)).ToArray();
            var secondPosition = Enumerable.Range(0, 128).Select(x => new Position(Entity.ENTITY_MAX - x)).ToArray();
            var thirdPosition  = Enumerable.Range(256, Entity.ENTITY_MAX - 256 - 128).Select(x => new Position(x + 1)).ToArray();

            var firstSetPosition  = span.Slice(0, 128).ToArray();
            var secondSetPosition = span.Slice(128, 128).ToArray();
            var thirdSetPosition  = span.Slice(256, Entity.ENTITY_MAX - 256 - 128).ToArray();

            firstSetPosition.ShouldBe(firstPosition);
            secondSetPosition.ShouldBe(secondPosition);
            thirdSetPosition.ShouldBe(thirdPosition);

#if DEBUG
            //we only reset the data in debug mode for performance reasons
            var fourthPosition    = Enumerable.Range(0, 128).Select(x => new Position(0)).ToArray();
            var fourthSetPosition = span.Slice(Entity.ENTITY_MAX - 128, 128).ToArray();
            fourthSetPosition.ShouldBe(fourthPosition);
#endif

            entityChunk.Count.ShouldBe(Entity.ENTITY_MAX - 128);
            entityChunk.Free.ShouldBe(128);
        }