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]; } }
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); }
public void Initialize(int capacity) { Pre.Assert(capacity >= 0); var bitfieldLength = (capacity / BitfieldSize) + 1; _bitfield = new ulong[bitfieldLength]; Clear(); }
public void CheckDestruction(int id) { Pre.Assert(id < _entityValidation.Count, id); if (!_entityValidation[id]) { throw new System.Exception("Entity id: " + id + " was already invalid!"); } }
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]); }
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); } }
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); }
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); }
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++; }
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); }
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(); }
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]; } }
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; } }
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]; } }
public ulong GetField(int n) { Pre.Assert(n >= 0, n); Pre.Assert(n < (Capacity / BitfieldSize), n); return(_bitfield[n]); }
public void Initialize(int capacity = 4) { Pre.Assert(Data == null); Data = new T[capacity]; }