Esempio n. 1
0
        public ComponentMemoryBlock(EntityArchetype archetype, BlockAllocator allocator)
        {
            id             = nextId++;
            this.archetype = archetype;
            //_typeLocations = new Dictionary<Type, ComponentSliceValues>();
            //_typeLocations = new Dictionary<int, ComponentSliceValues>();
            OptimizeDictionary(archetype, out dictionarySize, out dictionaryMask);
            _typeLocations     = new ComponentSliceValues[dictionarySize];
            _componentVersions = new long[dictionarySize];

            _data = allocator.Rent();
            _data.Memory.Span.Fill(0);

            int amountOfBytes  = _data.Size();
            int entitySize     = Unsafe.SizeOf <Entity>();
            int bytesPerEntity = entitySize;

            foreach (var component in archetype.components)
            {
                bytesPerEntity += component.Value;
            }
            int maxPaddingBytes = (28 * archetype.components.Count);

            //calculate maximum size of entities held by this memory block
            _maxSize = (int)MathF.Floor((float)(amountOfBytes - maxPaddingBytes) / (float)bytesPerEntity);
            _size    = 0;

            //place entities as the first "component"
            entityComponentSlice = new ComponentSliceValues {
                start         = 0,
                length        = entitySize * _maxSize,
                componentSize = entitySize
            };

            int nextIdx = entitySize * _maxSize;             //start from after entities

            nextIdx = ScanToNext32(nextIdx);
            foreach (var component in archetype.components)
            {
                int componentLength = component.Value * _maxSize;
                //_typeLocations.Add(component.Key.GetHashCode(), new ComponentSliceValues { start = nextIdx, length = componentLength, componentSize = component.Value});
                //_componentVersions.Add(component.Key.GetHashCode(), 0);
                _typeLocations[GetDictionaryKey(component.Key)] =
                    new ComponentSliceValues {
                    start         = nextIdx,
                    length        = componentLength,
                    componentSize = component.Value
                };
                _componentVersions[GetDictionaryKey(component.Key)] = 0;
                nextIdx += componentLength;
                nextIdx  = ScanToNext32(nextIdx);
            }
#if DEBUG
            unsafe
            {
                fixed(byte *bytes = _data.memory.Span)
                {
                    long address = (long)bytes;

                    DebugHelper.AssertThrow <ApplicationException>(_typeLocations.All(x => (address + x.start) % 32 == 0));
                }
            }
#endif
        }
Esempio n. 2
0
 public ComponentMemoryBlock(EntityArchetype archetype) : this(archetype, defaultAllocator)
 {
 }
Esempio n. 3
0
 public void CreateEntity(EntityArchetype archetype)
 {
     GetLocal().CreateEntity(archetype);
 }
Esempio n. 4
0
 public CreateEntityArchetypeMod(EntityArchetype archetype)
 {
     this.archetype = archetype;
 }
Esempio n. 5
0
 public EntityArchetypeBlock(EntityArchetype archetype)
 {
     this.archetype = archetype;
     blocks         = new List <ComponentMemoryBlock>();
     lastUsedIndex  = -1;
 }