Beispiel #1
0
        internal ReadOnlySpan <T> GetReadOnlyComponentData <T>() where T : unmanaged, IComponent
        {
            DebugHelper.AssertThrow <ComponentNotFoundException>(archetype.Has <T>());
            int key = GetDictionaryKey <T>();
            ComponentSliceValues componentSlice = _typeLocations[key];
            ReadOnlySpan <byte>  bytes          = _data.Memory.Span.Slice(componentSlice.start, componentSlice.length);
            ReadOnlySpan <T>     span           = MemoryMarshal.Cast <byte, T>(bytes);

            return(span);
        }
Beispiel #2
0
        internal Span <byte> GetRawReadonlyComponentDataAtIndex(System.Type type, int elementIndex)
        {
            DebugHelper.AssertThrow <ComponentNotFoundException>(archetype.components.ContainsKey(type));
            DebugHelper.AssertThrow <IndexOutOfRangeException>(elementIndex < MaxSize);
            int key = GetDictionaryKey(type);

            ComponentSliceValues componentSlice = _typeLocations[key];
            Span <byte>          bytes          = _data.Memory.Span.Slice(componentSlice.start, componentSlice.length);

            bytes = bytes.Slice(componentSlice.componentSize * elementIndex, componentSlice.componentSize);
            return(bytes);
        }
Beispiel #3
0
        internal Span <byte> GetRawComponentData(System.Type type)
        {
            DebugHelper.AssertThrow <ComponentNotFoundException>(archetype.components.ContainsKey(type));
            int key = GetDictionaryKey(type);

            _componentVersions[key] = _componentVersions[key] + 1;

            ComponentSliceValues componentSlice = _typeLocations[key];
            Span <byte>          bytes          = _data.Memory.Span.Slice(componentSlice.start, componentSlice.length);

            return(bytes);
        }
Beispiel #4
0
        internal Span <byte> GetRawComponentData <T>() where T : unmanaged, IComponent
        {
            DebugHelper.AssertThrow <ComponentNotFoundException>(archetype.Has <T>());

            int key = GetDictionaryKey <T>();

            _componentVersions[key] = _componentVersions[key] + 1;

            ComponentSliceValues componentSlice = _typeLocations[key];
            Span <byte>          bytes          = _data.Memory.Span.Slice(componentSlice.start, componentSlice.length);

            return(bytes);
        }
Beispiel #5
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
        }