Ecs changes

- BitArray has always at least sizeof(uint) bits
- Renamed Entity to EcsEntity
- Added value parameter to EcsWorld.AssignComponent
- Matched EcsWorld generic parameters
- Added Scene
- Added Entity
This commit is contained in:
Simon Lübeß
2022-02-28 23:34:52 +01:00
parent ba5ccd7107
commit 9197a209d6
16 changed files with 270 additions and 100 deletions
+42 -16
View File
@@ -4,27 +4,53 @@ using internal GlitchyEngine.World;
namespace GlitchyEngine.World
{
public struct Entity : uint64
{
// Binary Format:
// Bits: [0 - 31] [32 - 64]
// Data: Version Index
public struct Entity
{
private EcsEntity _entity = .InvalidEntity;
[Inline]
internal uint32 Version => (uint32)this;
private Scene _scene = null;
[Inline]
internal uint32 Index => (uint32)(this >> 32);
[Inline]
static internal Entity CreateEntityID(uint32 index, uint32 version)
public EcsEntity Handle => _entity;
public Scene Scene => _scene;
public this()
{
return ((uint64)index << 32) | version;
}
public this(EcsEntity entity, Scene scene)
{
_entity = entity;
_scene = scene;
}
public bool IsValid => _entity.IsValid;
public T* AddComponent<T>(T value = T()) where T: struct, new
{
Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component.");
return _scene._ecsWorld.AssignComponent<T>(_entity, value);
}
public T* GetComponent<T>() where T: struct, new
{
Log.EngineLogger.AssertDebug(HasComponent<T>(), "Entity doesn't have component!");
return _scene._ecsWorld.GetComponent<T>(_entity);
}
[Inline]
internal bool IsValid => Index != InvalidEntity.Index;
public bool HasComponent<T>() where T: struct, new
{
return _scene._ecsWorld.HasComponent<T>(_entity);
}
public void RemoveComponent<T>() where T: struct, new
{
Log.EngineLogger.AssertDebug(HasComponent<T>(), "Entity doesn't have component!");
_scene._ecsWorld.RemoveComponent<T>(_entity);
}
public const Entity InvalidEntity = ((uint64)uint32.MaxValue << 32) | 0;//TODO: Report bug: CreateEntityID(uint32.MaxValue, 0);
}
}