Beispiel #1
0
        public void _RemoveComponentWithoutUpdatingShortcut(Entity entity)
        {
            Pre.Assert(_Components.Count > 0);

            _Components.Count--;
            var position = GetIndex(entity);

            RemoveEntity(entity);

            if (position == _Components.Count)
            {
                return;
            }

            if (_isReferenceType)
            {
                var component = _Components.Data[position];
                _Components.Data[position]          = _Components.Data[_Components.Count];
                _Components.Data[_Components.Count] = component;
            }
            else
            {
                _Components.Data[position] = _Components.Data[_Components.Count];
            }
        }
Beispiel #2
0
        public void AddEntity(Entity entity)
        {
            var id = (int)entity.Id;

            if (id >= _IndexByEntity.Array.Data.Length)
            {
                Pre.Assert(_IndexByEntity.Array.Data.Length > 0);
                var newIndexCapacity = (id / _IndexByEntity.Array.Data.Length + 1) * _IndexByEntity.Array.Data.Length;
                _IndexByEntity._GrowIndexesCapacity(newIndexCapacity);
                _bitSet.GrowCapacity(newIndexCapacity);
            }

            var position = _Entities.Count;

            if (position >= _Entities.Data.Length)
            {
                Pre.Assert(_Entities.Data.Length > 0);
                GrowEntitiesCapacity((position / _Entities.Data.Length + 1) * _Entities.Data.Length);
            }

            Pre.Assert(!HasEntity(entity), "Can't be valid: " + entity.Id);
            _IndexByEntity.Array.Data[id] = _Entities.Count;
            _Entities.Data[position]      = entity;
            _Entities.Count++;
            _bitSet.Add(id);
        }
Beispiel #3
0
        public void Initialize(int capacity)
        {
            Pre.Assert(capacity >= 0);
            var bitfieldLength = (capacity / BitfieldSize) + 1;

            _bitfield = new ulong[bitfieldLength];
            Clear();
        }
Beispiel #4
0
 public void CheckDestruction(int id)
 {
     Pre.Assert(id < _entityValidation.Count, id);
     if (!_entityValidation[id])
     {
         throw new System.Exception("Entity id: " + id + " was already invalid!");
     }
 }
Beispiel #5
0
        public TComponent Get(Entity entity)
        {
            var index = _IndexByEntity.Array.Data[(int)entity.Id];

            Pre.Assert(index >= 0, index, entity.Id, entity._GetDebugName());
            Pre.Assert(index < _Components.Count, entity._GetDebugName());
            Pre.Assert(index < _Components.Data.Length, entity._GetDebugName());
            return(_Components.Data[index]);
        }
Beispiel #6
0
        private void GrowComponentCapacity(int desiredCapacity)
        {
            Pre.Assert(desiredCapacity == _Entities.Data.Length, "This must be in sync with _Entities.Data.Length");
            var oldComponentCapacity = _Components.Data.Length;

            _Components.IncreaseCapacity(desiredCapacity);
            if (_isReferenceType)
            {
                FillDefaultComponents(oldComponentCapacity, _Components.Data.Length);
            }
        }
Beispiel #7
0
        public void Remove(int element)
        {
            Pre.Assert(Contains(element), element);
            var   bitfieldPos    = element / BitfieldSize;
            int   bitfieldOffset = element % BitfieldSize;
            ulong patch          = One << bitfieldOffset;

            Pre.Assert(bitfieldPos >= 0 && bitfieldPos < _bitfield.Length, element, _bitfield.Length);
            _bitfield[bitfieldPos] -= patch;
            Pre.Assert(!Contains(element), element);
        }
Beispiel #8
0
        public bool Contains(int element)
        {
            Pre.Assert(element >= 0, element);
            Pre.Assert(element < (BitfieldSize * _bitfield.Length), element, _bitfield.Length);
            var   bitfieldPos    = element / BitfieldSize;
            int   bitfieldOffset = element % BitfieldSize;
            ulong patch          = One << bitfieldOffset;

            Pre.Assert(bitfieldPos >= 0 && bitfieldPos < _bitfield.Length, element, _bitfield.Length);
            return((_bitfield[bitfieldPos] & patch) == patch);
        }
Beispiel #9
0
        public void _NewComponentWithoutUpdatingShortcut(Entity entity)
        {
            AddEntity(entity);
            var entitiesLength = _Entities.Data.Length;

            if (entitiesLength != _Components.Data.Length)
            {
                Pre.Assert(entitiesLength > _Components.Data.Length);
                GrowComponentCapacity(entitiesLength);
            }
            _Components.Count++;
        }
Beispiel #10
0
        public void _GrowIndexesCapacity(int desiredCapacity)
        {
            Pre.Assert(desiredCapacity >= Array.Data.Length);

            if (desiredCapacity == Array.Data.Length)
            {
                return;
            }

            var oldCapacityIndexes = Array.Data.Length;

            Array.IncreaseCapacity(desiredCapacity);

            FillDefaultEntityIndexes(oldCapacityIndexes, Array.Data.Length);
        }
Beispiel #11
0
        public void Destroy(Entity entity)
        {
            Pre.Assert(IsValid(entity), entity._GetDebugName());

            var id = (int)entity.Id;

            CheckDestruction(id);

            foreach (var pool in _removeEntitiesShortcut[id])
            {
                pool._RemoveComponentWithoutUpdatingShortcut(entity);
            }

            _entityValidation[id] = false;
            _availableEntities.Enqueue(entity);
            _removeEntitiesShortcut[id].Clear();
        }
Beispiel #12
0
        public void IncreaseCapacity(int desiredCapacity)
        {
            Pre.Assert(Data != null);
            var oldCapacity = Data.Length;

            Pre.Assert(desiredCapacity > oldCapacity, "Use ReduceCapacity, to reduce capacity.");

            DebugLogStupidAllocation(oldCapacity, desiredCapacity);

            var oldArray = Data;

            Data = new T[desiredCapacity];
            for (var i = 0; i < oldCapacity; i++)
            {
                Data[i] = oldArray[i];
            }
        }
Beispiel #13
0
        public void RemoveEntity(Entity entity)
        {
            Pre.Assert(_Entities.Count > 0);

            _Entities.Count--;
            var id       = (int)entity.Id;
            var position = _IndexByEntity.Array.Data[id];

            _IndexByEntity.Array.Data[id] = -1;
            _bitSet.Remove(id);

            if (position != _Entities.Count)
            {
                var lastEntity = _Entities.Data[_Entities.Count];
                _Entities.Data[position] = _Entities.Data[_Entities.Count];
                _IndexByEntity.Array.Data[(int)lastEntity.Id] = position;
            }
        }
Beispiel #14
0
        public void ReduceCapacity(int targetCapacity)
        {
            Pre.Assert(Data != null);
            Pre.Assert(targetCapacity >= Count, "Try using Reset first if you want to free all memory.");
            var oldCapacity = Data.Length;

            Pre.Assert(targetCapacity <= oldCapacity, "Use IncreaseCapacity to increase capacity.");
            if (targetCapacity == oldCapacity)
            {
                return;
            }

            DebugLogStupidAllocation(oldCapacity, targetCapacity);

            var oldArray = Data;

            Data = new T[targetCapacity];
            for (var i = 0; i < targetCapacity; i++)
            {
                Data[i] = oldArray[i];
            }
        }
Beispiel #15
0
 public ulong GetField(int n)
 {
     Pre.Assert(n >= 0, n);
     Pre.Assert(n < (Capacity / BitfieldSize), n);
     return(_bitfield[n]);
 }
Beispiel #16
0
 public void Initialize(int capacity = 4)
 {
     Pre.Assert(Data == null);
     Data = new T[capacity];
 }