From 330261b9713a6506317428e08c642d90b381add3 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 3 Mar 2024 05:52:19 +0800 Subject: [PATCH] refacotr GetComponents(debug) & add GetComponentTypeIDs(debug) --- src/EcsWorld.cs | 54 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index e4d9786..bdaf99a 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -1,5 +1,6 @@ using DCFApixels.DragonECS.Internal; using System; +using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -624,43 +625,35 @@ namespace DCFApixels.DragonECS #endregion #region Debug Components + private static int[] _componentIDsBuffer = new int[64]; + public ReadOnlySpan GetComponentTypeIDs(int entityID) + { + int count = GetComponentTypeIDs(entityID, ref _componentIDsBuffer); + return new ReadOnlySpan(_componentIDsBuffer, 0, count); + } public void GetComponents(int entityID, List list) { list.Clear(); - var itemsCount = GetComponentsCount(entityID); - if (itemsCount <= 0) { return; } - int poolIndex = 0; - uint bit; - for (int chunkIndex = entityID * _entityComponentMaskLength, chunkIndexMax = chunkIndex + _entityComponentMaskLength; chunkIndex < chunkIndexMax; chunkIndex++) + int count = GetComponentTypeIDs(entityID, ref _componentIDsBuffer); + for (int i = 0; i < count; i++) { - bit = 0x0000_0001; - int chunk = _entityComponentMasks[chunkIndex]; - if (chunk == 0) - { - poolIndex += COMPONENT_MASK_CHUNK_SIZE; - } - else - { - while (bit != 0) - { - if ((chunk & bit) != 0) - { - itemsCount--; - list.Add(_pools[poolIndex].GetRaw(entityID)); - if (itemsCount <= 0) { goto exit; } - } - bit <<= 1; - poolIndex++; - } - } + list.Add(_pools[_componentIDsBuffer[i]].GetRaw(entityID)); } - exit:; } public void GetComponentTypes(int entityID, HashSet typeSet) { typeSet.Clear(); + int count = GetComponentTypeIDs(entityID, ref _componentIDsBuffer); + for (int i = 0; i < count; i++) + { + typeSet.Add(_pools[_componentIDsBuffer[i]].ComponentType); + } + } + private int GetComponentTypeIDs(int entityID, ref int[] componentIDs) + { + int count = 0; var itemsCount = GetComponentsCount(entityID); - if(itemsCount <= 0) { return; } + if (itemsCount <= 0) { return count; } int poolIndex = 0; uint bit; for (int chunkIndex = entityID * _entityComponentMaskLength, chunkIndexMax = chunkIndex + _entityComponentMaskLength; chunkIndex < chunkIndexMax; chunkIndex++) @@ -678,7 +671,11 @@ namespace DCFApixels.DragonECS if ((chunk & bit) != 0) { itemsCount--; - typeSet.Add(_pools[poolIndex].ComponentType); + if(count > componentIDs.Length) + { + Array.Resize(ref componentIDs, count << 1); + } + componentIDs[count++] = _pools[poolIndex].ComponentTypeID; if (itemsCount <= 0) { goto exit; } } bit <<= 1; @@ -687,6 +684,7 @@ namespace DCFApixels.DragonECS } } exit:; + return count; } #endregion }