initial setup for entt
This commit is contained in:
parent
171b36d571
commit
239ddd1b18
6 changed files with 114 additions and 17 deletions
|
@ -33,7 +33,7 @@ class ComponentManager
|
|||
var idx = _Available.PopFront();
|
||||
_Deleted.Remove(idx);
|
||||
|
||||
*(int_cosize*)(void*)&_Data[idx * (_ComponentSize + _IndexSize)] = owner;
|
||||
*(int_cosize*)(void*)&_Data[idx * (_ComponentSize + _IndexSize)] = (.)owner;
|
||||
void* toReturn = (void*)&_Data[idx * (_ComponentSize + _IndexSize) + _IndexSize];
|
||||
return (
|
||||
idx,
|
||||
|
@ -43,7 +43,7 @@ class ComponentManager
|
|||
else
|
||||
{
|
||||
Grow();
|
||||
*(int_cosize*)(void*)(_Data.Ptr + (_Next * (_ComponentSize + _IndexSize))) = owner;
|
||||
*(int_cosize*)(void*)(_Data.Ptr + (_Next * (_ComponentSize + _IndexSize))) = (.)owner;
|
||||
//This calculates to the next biggest position + the offset of the index
|
||||
void* toReturn = (void*)(_Data.Ptr + (_Next * (_ComponentSize + _IndexSize) + _IndexSize));
|
||||
return (
|
||||
|
|
22
src/ECS.bf
22
src/ECS.bf
|
@ -3,7 +3,7 @@ namespace Theater_ECS;
|
|||
using System;
|
||||
using System.Collections;
|
||||
|
||||
public typealias Entity = int_cosize;
|
||||
//public typealias Entity = int_cosize;
|
||||
|
||||
public typealias Component = int_cosize;
|
||||
|
||||
|
@ -15,7 +15,7 @@ class ECS
|
|||
private List<ComponentManager> _ComponentsPacked = new .() ~ DeleteContainerAndItems!(_);
|
||||
private Dictionary<Type, Component> _ComponentMap = new .() ~ delete _;
|
||||
|
||||
private Entity _EntityNext = 0;
|
||||
private int_cosize _EntityNext = 0;
|
||||
private List<List<int_cosize>> _EntityList = new .() ~ DeleteContainerAndItems!(_);
|
||||
private Queue<Entity> _EntityAvailable = new .() ~ delete _;
|
||||
private HashSet<Entity> _DeletedEntities = new .() ~ delete _;
|
||||
|
@ -47,13 +47,13 @@ class ECS
|
|||
{
|
||||
for(var i in _EntityList)
|
||||
i.Add(-1);
|
||||
return _EntityNext++;
|
||||
return (.)_EntityNext++;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveEntity(Entity e)
|
||||
{
|
||||
if(e >= _EntityNext || _DeletedEntities.Contains(e))
|
||||
if(e >= (.)_EntityNext || _DeletedEntities.Contains(e))
|
||||
return;
|
||||
|
||||
_DeletedEntities.Add(e);
|
||||
|
@ -61,30 +61,30 @@ class ECS
|
|||
|
||||
for(int i < _EntityList.Count)
|
||||
{
|
||||
if(_EntityList[i][e] != -1)
|
||||
if(_EntityList[i][(.)e] != -1)
|
||||
{
|
||||
_ComponentsPacked[i].Remove(_EntityList[i][e]);
|
||||
_EntityList[i][e] = -1;
|
||||
_ComponentsPacked[i].Remove(_EntityList[i][(.)e]);
|
||||
_EntityList[i][(.)e] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Result<void*> AddComponentToEntity(Entity e, Component c)
|
||||
{
|
||||
if(e >= _EntityNext || _DeletedEntities.Contains(e))
|
||||
if(e >= (.)_EntityNext || _DeletedEntities.Contains(e))
|
||||
return .Err;
|
||||
|
||||
var res = _ComponentsPacked[c].Create(e);
|
||||
_EntityList[c][e] = res.0;
|
||||
_EntityList[c][(.)e] = res.0;
|
||||
return res.1;
|
||||
}
|
||||
|
||||
public Result<void*> RemoveComponentFromEntity(Entity e, Component c)
|
||||
{
|
||||
if(e >= _EntityNext || _DeletedEntities.Contains(e))
|
||||
if(e >= (.)_EntityNext || _DeletedEntities.Contains(e))
|
||||
return .Err;
|
||||
|
||||
return _ComponentsPacked[c].Remove(e);
|
||||
return _ComponentsPacked[c].Remove((.)e);
|
||||
}
|
||||
|
||||
public void GetEntityFromComponent(Component c, List<Entity> e)
|
||||
|
|
71
src/EntityRegister.bf
Normal file
71
src/EntityRegister.bf
Normal file
|
@ -0,0 +1,71 @@
|
|||
namespace Theater_ECS;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
static
|
||||
{
|
||||
|
||||
public struct Entity : uint32
|
||||
{
|
||||
public static readonly Entity Null => 0xFFFFF;
|
||||
public static readonly uint32 VersionMask => 0xFFF00000;
|
||||
public static readonly uint32 IndexMask => 0x000FFFFF;
|
||||
public static readonly uint8 VersionOffset => 20;
|
||||
|
||||
public uint32 Index => (.)this & IndexMask;
|
||||
public uint32 Version => (.)(this & VersionMask) >> VersionOffset
|
||||
public Entity Next => .(Index, Version + 1);
|
||||
|
||||
public this() {};
|
||||
|
||||
public this(uint32 index, uint32 version)
|
||||
{
|
||||
this = (index & IndexMask) | ((version << VersionOffset) & VersionMask);
|
||||
|
||||
}
|
||||
|
||||
public override void ToString(String strBuffer)
|
||||
{
|
||||
strBuffer.Append(scope $"{Index}:{Version}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EntityRegister
|
||||
{
|
||||
private List<Entity> _entities = new .() ~ delete _;
|
||||
private int32 _available = 0;
|
||||
private Entity _next = .Null;
|
||||
|
||||
|
||||
public Entity CreateEntity()
|
||||
{
|
||||
if(_available == 0)
|
||||
{
|
||||
if(_entities.Count + 1 > Entity.IndexMask)
|
||||
return .Null; //It would be beneficial if we could avoid this check
|
||||
return .((.)_entities..Add(.((.)_entities.Count, 0)).Count, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
_available--;
|
||||
|
||||
let temp = _next.Index;
|
||||
_next = (.)_entities[_next.Index].Index;
|
||||
_entities[temp] = (.)(temp | (_entities[temp].Version << Entity.VersionOffset));
|
||||
|
||||
return _entities[temp];
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveEntity(Entity e)
|
||||
{
|
||||
let temp = _next;
|
||||
_next = .(e.Index, 255);
|
||||
_entities[e.Index] = .(temp.Index, _entities[e.Index].Version+1);
|
||||
_available++;
|
||||
}
|
||||
|
||||
public bool IsAlive(Entity e) => e == _entities[e.Index];
|
||||
}
|
|
@ -11,6 +11,29 @@ class Program
|
|||
|
||||
public static void Main()
|
||||
{
|
||||
/*
|
||||
Entity e = Entity(45454, 797979790);
|
||||
Console.WriteLine(e);
|
||||
Console.Read();
|
||||
|
||||
EntityRegister r = scope .();
|
||||
while(true)
|
||||
{
|
||||
Console.WriteLine(scope $"Next: {r.[Friend]_next}");
|
||||
for(var i in r.[Friend]_entities)
|
||||
Console.WriteLine(i);
|
||||
var l = Console.ReadLine(.. scope .());
|
||||
|
||||
Console.Clear();
|
||||
|
||||
if(l == "c")
|
||||
Console.WriteLine(r.CreateEntity());
|
||||
else if(l == "d")
|
||||
r.RemoveEntity(.((.)gRand.Next(0 , r.[Friend]_entities.Count), 0));
|
||||
Console.WriteLine();
|
||||
}
|
||||
*/
|
||||
|
||||
ECS ecs = scope .();
|
||||
var pos = ecs.RegisterComponent<Position>(100);
|
||||
//var waste = ecs.RegisterComponent<waste>(100);
|
||||
|
@ -50,10 +73,8 @@ class Program
|
|||
watch.Stop();
|
||||
watch.Reset();
|
||||
}
|
||||
//Console.Read();
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public struct waste
|
||||
|
|
5
src/PagedSparseSet.bf
Normal file
5
src/PagedSparseSet.bf
Normal file
|
@ -0,0 +1,5 @@
|
|||
namespace Theater_ECS;
|
||||
|
||||
class PagedSparseSet<T>
|
||||
{
|
||||
}
|
|
@ -100,7 +100,7 @@ struct SystemMethodAttribute : Attribute, IOnTypeInit
|
|||
for(var i in _Types)
|
||||
{
|
||||
buffer.Append(scope $"""
|
||||
ref *ecs.[System.Friend]_ComponentsPacked[_{i.GetName(.. scope .())}].Get<{i.GetName(.. scope .())}>(ecs.[System.Friend]_EntityList[_{i.GetName(.. scope .())}][e])
|
||||
ref *ecs.[System.Friend]_ComponentsPacked[_{i.GetName(.. scope .())}].Get<{i.GetName(.. scope .())}>(ecs.[System.Friend]_EntityList[_{i.GetName(.. scope .())}][(.)e])
|
||||
""");
|
||||
if(@i.Index+1 < _Types.Length)
|
||||
buffer.Append(",\n");
|
||||
|
|
Loading…
Add table
Reference in a new issue