internal bool Includes(ArcheType *other) { if (other->Count > Count) { return(false); } for (int i = 0; i < other->Count; ++i) { var hashOther = other->GetHash(i); bool found = false; for (int j = 0; j < Count; ++j) { var hash = GetHash(j); if (hash == hashOther) { found = true; break; } } if (!found) { return(false); } } return(true); }
internal void Copy(ArcheType *toArcheType, Chunk *toChunk, int toIndexInChunk, ArcheType *fromArcheType, Chunk *fromChunk, int fromIndexInChunk) { var fromBlock = FindBlock(fromArcheType); Assert.IsNotNull(fromBlock); var toBlock = GetOrCreateBlock(toArcheType); (toBlock.GetEntityIndexArray(toChunk))[toIndexInChunk] = (fromBlock.GetEntityIndexArray(fromChunk))[fromIndexInChunk]; for (int i = 0; i < toArcheType->Count; ++i) { int toHash = toArcheType->GetHash(i); for (int j = 0; j < fromArcheType->Count; ++j) { int fromHash = fromArcheType->GetHash(j); if (toHash != fromHash) { continue; } int toSize = toArcheType->GetSize(i); int fromSize = fromArcheType->GetSize(j); Assert.AreEqual(toSize, fromSize); var toBuf = toBlock.GetComponentDataArray(toChunk, i) + toSize * toIndexInChunk; var fromBuf = fromBlock.GetComponentDataArray(fromChunk, j) + fromSize * fromIndexInChunk; UnsafeUtility.MemCpy(toBuf, fromBuf, toSize); break; } } }
public unsafe void Create(ref Entity entity, ArcheType *archeType = null) { int idx = -1; again: if (0 <= _lastUnused && _lastUnused < _capacity && !_occupation.Check(_lastUnused)) { idx = _lastUnused++; } if (idx < 0) { idx = _occupation.FindCleared(); } if (idx < 0) { Capacity = Capacity * 2; goto again; } var buf = (Entity *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(_entities); buf[idx].Index = idx; entity.Index = idx; _occupation.Set(idx); ++_count; _dataManager.Clear(entity.Index); // if (archeType != null) { SetArcheType(entity, archeType); } }
public unsafe Entity Create(ArcheType *archeType = null) { Entity entity = new Entity(); Create(ref entity, archeType); return(entity); }
internal void Free(ArcheType *archeType, Chunk *chunk, int indexInChunk) { var block = FindBlock(archeType); Assert.IsNotNull(block); Chunk *newChunk; int newIndexInChunk; block.Free(chunk, indexInChunk, &newChunk, &newIndexInChunk); }
internal ComponentBlock GetOrCreateBlock(ArcheType *archeType) { var block = FindBlock(archeType); if (block != null) { return(block); } return(CreateBlock(archeType)); }
ComponentBlock CreateBlock(ArcheType *archeType) { var ptr = (ulong)archeType; Assert.IsFalse(_blocks.ContainsKey(ptr)); var block = new ComponentBlock(archeType, _entityDataManager); _blocks[ptr] = block; return(block); }
ComponentBlock FindBlock(ArcheType *archeType) { var ptr = (ulong)archeType; if (_blocks.ContainsKey(ptr)) { return(_blocks[ptr]); } return(null); }
internal ComponentBlock(ArcheType *ptr, EntityDataManager entityDataManager) { Assert.IsTrue(ptr != null); Assert.IsNotNull(entityDataManager); ArcheType = ptr; _entityDataManager = entityDataManager; _data = new ChunkAllocator(); _dataSize = Util.AlignmentPow2(ArcheType->TotalSize, AlignmentSize); _countInChunk = _data.CalcAlignedCapacity(AlignmentSize) / _dataSize; _countInTotal = 0; _usedCountInChunk = -1; _usedCountInTotal = 0; }
internal ArcheType *CreateArcheType(ArcheType *baseArcheType, params Type[] types) { int newlen = (baseArcheType != null)? baseArcheType->Count + types.Length : types.Length; int newsize = sizeof(ArcheType) + (sizeof(ArcheType.Pair) * newlen); ArcheType.Pair *candidateArray = stackalloc ArcheType.Pair[newlen]; if (baseArcheType != null) { for (int i = 0; i < baseArcheType->Count; ++i) { var p = &candidateArray[i]; p->Hash = baseArcheType->GetHash(i); p->Size = baseArcheType->GetSize(i); } } int ofs = (baseArcheType != null)? baseArcheType->Count : 0; for (int i = ofs; i < newlen; ++i) { var t = types[i - ofs]; Assert.IsTrue(t.IsValueType); Assert.IsNotNull(t.GetInterface("SimpleECS.IComponentData", false)); var p = &candidateArray[i]; p->Hash = t.GetHashCode(); p->Size = Marshal.SizeOf(t); } var archeType = FindArcheType(candidateArray, newlen); if (archeType != null) { return(archeType); } // var ptr = _buffer.Allocate(newsize, sizeof(int)); archeType = (ArcheType *)ptr; archeType->Count = newlen; var pairArray = (ArcheType.Pair *)(ptr + sizeof(ArcheType)); for (int i = 0; i < newlen; ++i) { pairArray[i] = candidateArray[i]; } _archeTypes.Add((IntPtr)archeType); return(archeType); }
public unsafe void SetArcheType(Entity entity, ArcheType *newArcheType) { var data = _dataManager.Get(entity.Index); if (data->ArcheType == null) { data->ArcheType = newArcheType; data->IndexInChunk = _componentBlockManager.Allocate(data->ArcheType, &data->Chunk, entity.Index); } else { Chunk *newChunk = null; var newIndexInChunk = _componentBlockManager.Allocate(newArcheType, &newChunk, entity.Index); _componentBlockManager.Copy(newArcheType, newChunk, newIndexInChunk, data->ArcheType, data->Chunk, data->IndexInChunk); _componentBlockManager.Free(data->ArcheType, data->Chunk, data->IndexInChunk); data->ArcheType = newArcheType; data->Chunk = newChunk; data->IndexInChunk = newIndexInChunk; } }
internal int Allocate(ArcheType *archeType, Chunk **chunk, int entityIndex) { var block = GetOrCreateBlock(archeType); return(block.Allocate(chunk, entityIndex)); }
protected ComponentSystem(ArcheType *archeType) { ArcheType = archeType; }
// public MakeMatrixSystem(ArcheType *archeType) : base(archeType) { }
// public MoveSystem(ArcheType *archeType) : base(archeType) { }