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);
        }
Ejemplo n.º 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);
        }
        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);
        }