add some useful things for debug inspector
continuous-integration/drone/push Build is passing Details

debug
cosmonaut 2021-03-24 17:46:11 -07:00
parent 00614661ec
commit 7ff3323391
4 changed files with 48 additions and 4 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
@ -118,6 +119,17 @@ namespace Encompass
return this == Zero; return this == Zero;
} }
public IEnumerable<int> TrueIndices()
{
for (int i = 0; i < 512; i++)
{
if (Get(i))
{
yield return i;
}
}
}
public static BitSet512 BitwiseAnd(BitSet512 left, BitSet512 right) public static BitSet512 BitwiseAnd(BitSet512 left, BitSet512 right)
{ {
return left & right; return left & right;

View File

@ -11,19 +11,21 @@ namespace Encompass
private readonly ComponentStore _upToDateComponentStore; private readonly ComponentStore _upToDateComponentStore;
public Dictionary<Type, int> TypeToIndex { get; } public Dictionary<Type, int> TypeToIndex { get; }
private Dictionary<int, Type> IndexToType { get; }
private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>(); private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>();
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } } internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } } internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
public ComponentManager(Dictionary<Type, int> typeToIndex) public ComponentManager(Dictionary<Type, int> typeToIndex, Dictionary<int, Type> indexToType)
{ {
_existingComponentStore = new ComponentStore(typeToIndex); _existingComponentStore = new ComponentStore(typeToIndex);
_immediateComponentStore = new ComponentStore(typeToIndex); _immediateComponentStore = new ComponentStore(typeToIndex);
_replayStore = new ComponentDeltaStore(typeToIndex); _replayStore = new ComponentDeltaStore(typeToIndex);
_upToDateComponentStore = new ComponentStore(typeToIndex); _upToDateComponentStore = new ComponentStore(typeToIndex);
TypeToIndex = typeToIndex; TypeToIndex = typeToIndex;
IndexToType = indexToType;
} }
public void RegisterComponentType<TComponent>() where TComponent : struct public void RegisterComponentType<TComponent>() where TComponent : struct
@ -186,6 +188,13 @@ namespace Encompass
return ref _existingComponentStore.Get<TComponent>(entityID); return ref _existingComponentStore.Get<TComponent>(entityID);
} }
#if DEBUG
internal TComponent Debug_ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{
return _existingComponentStore.Get<TComponent>(entityID);
}
#endif
internal ref TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct internal ref TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _immediateComponentStore.Get<TComponent>(entityID); return ref _immediateComponentStore.Get<TComponent>(entityID);
@ -284,5 +293,18 @@ namespace Encompass
{ {
return _upToDateComponentStore.EntityBitArray(entityID).AllFalse(); return _upToDateComponentStore.EntityBitArray(entityID).AllFalse();
} }
// should be used for debugging only!
#if DEBUG
internal IEnumerable<object> Components(int entityID)
{
foreach (var typeIndex in ExistingBits.EntityBitArray(entityID).TrueIndices())
{
var method = typeof(ComponentManager).GetMethod(nameof(ComponentManager.Debug_ReadExistingComponentByEntityAndType), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var generic = method.MakeGenericMethod(IndexToType[typeIndex]);
yield return generic.Invoke(this, new object[] { entityID });
}
}
#endif
} }
} }

View File

@ -64,5 +64,12 @@ namespace Encompass
{ {
return _componentManager.SomeExistingComponent<TComponent>(); return _componentManager.SomeExistingComponent<TComponent>();
} }
#if DEBUG
protected IEnumerable<object> Debug_GetAllComponents(Entity entity)
{
return _componentManager.Components(entity.ID);
}
#endif
} }
} }

View File

@ -34,13 +34,14 @@ namespace Encompass
private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>(); private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>();
private readonly HashSet<Type> _messageTypes = new HashSet<Type>(); private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>(); private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>();
private readonly Dictionary<int, Type> _indexToType = new Dictionary<int, Type>();
public WorldBuilder(int entityCapacity = 32768) public WorldBuilder(int entityCapacity = 32768)
{ {
_entityCapacity = entityCapacity; _entityCapacity = entityCapacity;
_timeManager = new TimeManager(); _timeManager = new TimeManager();
_trackingManager = new TrackingManager(); _trackingManager = new TrackingManager();
_componentManager = new ComponentManager(_typeToIndex); _componentManager = new ComponentManager(_typeToIndex, _indexToType);
_messageManager = new MessageManager(_timeManager); _messageManager = new MessageManager(_timeManager);
_entityManager = new EntityManager(_componentManager, entityCapacity); _entityManager = new EntityManager(_componentManager, entityCapacity);
_renderManager = new RenderManager(_entityManager); _renderManager = new RenderManager(_entityManager);
@ -94,7 +95,9 @@ namespace Encompass
{ {
if (!_typeToIndex.ContainsKey(typeof(TComponent))) if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{ {
_typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); var count = _typeToIndex.Count;
_typeToIndex.Add(typeof(TComponent), count);
_indexToType.Add(count, typeof(TComponent));
_componentTypesToPreload.Add(typeof(TComponent)); _componentTypesToPreload.Add(typeof(TComponent));
_componentManager.RegisterComponentType<TComponent>(); _componentManager.RegisterComponentType<TComponent>();
_startingExistingComponentStore.RegisterComponentType<TComponent>(); _startingExistingComponentStore.RegisterComponentType<TComponent>();
@ -428,7 +431,7 @@ namespace Encompass
var dummyTimeManager = new TimeManager(); var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager); var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyTrackingManager = new TrackingManager(); var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(_typeToIndex); var dummyComponentManager = new ComponentManager(_typeToIndex, _indexToType);
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity); var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager); var dummyRenderManager = new RenderManager(dummyEntityManager);