Compare commits

...

4 Commits
main ... debug

Author SHA1 Message Date
cosmonaut 9212aa5bcb make component search case insensitive
continuous-integration/drone/push Build is passing Details
2021-03-26 14:45:37 -07:00
cosmonaut 25bcb2b0f8 add new DebugRenderer which can set component values
continuous-integration/drone/push Build is passing Details
2021-03-26 12:08:00 -07:00
cosmonaut f87cfc4c5a add another debug method
continuous-integration/drone/push Build is passing Details
2021-03-25 16:23:17 -07:00
cosmonaut 7ff3323391 add some useful things for debug inspector
continuous-integration/drone/push Build is passing Details
2021-03-24 17:46:11 -07:00
4 changed files with 92 additions and 4 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace Encompass
{
@ -118,6 +119,17 @@ namespace Encompass
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)
{
return left & right;

View File

@ -11,19 +11,21 @@ namespace Encompass
private readonly ComponentStore _upToDateComponentStore;
public Dictionary<Type, int> TypeToIndex { get; }
private Dictionary<int, Type> IndexToType { get; }
private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>();
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.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);
_immediateComponentStore = new ComponentStore(typeToIndex);
_replayStore = new ComponentDeltaStore(typeToIndex);
_upToDateComponentStore = new ComponentStore(typeToIndex);
TypeToIndex = typeToIndex;
IndexToType = indexToType;
}
public void RegisterComponentType<TComponent>() where TComponent : struct
@ -82,6 +84,13 @@ namespace Encompass
return result;
}
#if DEBUG
internal void Debug_UpdateComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_existingComponentStore.Set(entityID, component);
}
#endif
internal void AddComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_upToDateComponentStore.Set(entityID, component);
@ -186,6 +195,13 @@ namespace Encompass
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
{
return ref _immediateComponentStore.Get<TComponent>(entityID);
@ -284,5 +300,29 @@ namespace Encompass
{
return _upToDateComponentStore.EntityBitArray(entityID).AllFalse();
}
// should be used for debugging only!
#if DEBUG
internal IEnumerable<object> Debug_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 });
}
}
internal IEnumerable<Type> Debug_SearchComponentType(string typeString)
{
foreach (var type in TypeToIndex.Keys)
{
if (type.ToString().ToLower().Contains(typeString.ToLower()))
{
yield return type;
}
}
}
#endif
}
}

View File

@ -0,0 +1,33 @@
#if DEBUG
using System;
using System.Collections.Generic;
namespace Encompass
{
public abstract class DebugRenderer : Renderer
{
protected IEnumerable<object> Debug_GetAllComponents(Entity entity)
{
return _componentManager.Debug_Components(entity.ID);
}
protected IEnumerable<Entity> Debug_Entities(Type componentType)
{
var method = typeof(Renderer).GetMethod(nameof(ReadEntitiesAsEnumerable), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
return (IEnumerable<Entity>)generic.Invoke(this, null);
}
protected IEnumerable<Type> Debug_SearchComponentType(string typeString)
{
return _componentManager.Debug_SearchComponentType(typeString);
}
protected void Debug_SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
{
_componentManager.Debug_UpdateComponent(entity.ID, component);
}
}
}
#endif

View File

@ -34,13 +34,14 @@ namespace Encompass
private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>();
private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
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)
{
_entityCapacity = entityCapacity;
_timeManager = new TimeManager();
_trackingManager = new TrackingManager();
_componentManager = new ComponentManager(_typeToIndex);
_componentManager = new ComponentManager(_typeToIndex, _indexToType);
_messageManager = new MessageManager(_timeManager);
_entityManager = new EntityManager(_componentManager, entityCapacity);
_renderManager = new RenderManager(_entityManager);
@ -94,7 +95,9 @@ namespace Encompass
{
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));
_componentManager.RegisterComponentType<TComponent>();
_startingExistingComponentStore.RegisterComponentType<TComponent>();
@ -428,7 +431,7 @@ namespace Encompass
var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(_typeToIndex);
var dummyComponentManager = new ComponentManager(_typeToIndex, _indexToType);
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager);