Merge branch 'dev' into 4-Book-Documentation
This commit is contained in:
commit
2f797ece52
7 changed files with 0 additions and 324 deletions
|
@ -1,109 +0,0 @@
|
|||
namespace Theater_ECS;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Collections;
|
||||
|
||||
class ComponentManager
|
||||
{
|
||||
private Monitor _Lock = new .() ~ delete _;
|
||||
|
||||
private int_cosize _ComponentSize = -1;
|
||||
private readonly int _IndexSize = sizeof(int_cosize);
|
||||
|
||||
private int_cosize _Next = 0;
|
||||
private List<uint8> _Data = null ~ delete _;
|
||||
private List<int_cosize> _Available = new .() ~ delete _;
|
||||
private HashSet<int_cosize> _Deleted = new .() ~ delete _;
|
||||
|
||||
///Make the component manager useable for a specific type of component
|
||||
public void Initialize<T>(int_cosize count) where T : struct
|
||||
{
|
||||
_ComponentSize = sizeof(T);
|
||||
_Data = new .( //So that we dont have to realloc as frequently
|
||||
(_IndexSize + _ComponentSize) * count
|
||||
);
|
||||
}
|
||||
|
||||
///Creates space for a new component instance and returns a pointer to it
|
||||
public (Component, void*) Create(Entity owner)
|
||||
{
|
||||
if (_Available.Count > 0)
|
||||
{
|
||||
var idx = _Available.PopFront();
|
||||
_Deleted.Remove(idx);
|
||||
|
||||
*(int_cosize*)(void*)&_Data[idx * (_ComponentSize + _IndexSize)] = owner;
|
||||
void* toReturn = (void*)&_Data[idx * (_ComponentSize + _IndexSize) + _IndexSize];
|
||||
return (
|
||||
idx,
|
||||
toReturn
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grow();
|
||||
*(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 (
|
||||
_Next++,
|
||||
toReturn
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///Remove an entry, and returns a pointer to the removed object, if you want to
|
||||
public Result<void*> Remove(int_cosize idx)
|
||||
{
|
||||
if (idx >= _Next || _Deleted.Contains(idx))
|
||||
return .Err;
|
||||
|
||||
*(int_cosize*)(void*)&_Data[idx * (_ComponentSize + _IndexSize)] = -1;
|
||||
|
||||
_Deleted.Add(idx);
|
||||
|
||||
return .Ok(
|
||||
(void*)(_Data.Ptr + idx * (_ComponentSize + _IndexSize) + _IndexSize)
|
||||
);
|
||||
}
|
||||
|
||||
public int_cosize Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return (.)(_Data.Count / (_ComponentSize + _IndexSize));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Span<(Entity, T)> GetAll<T>() where T : struct
|
||||
{
|
||||
return .((.)(void*)_Data.Ptr, Count);
|
||||
}
|
||||
|
||||
[Unchecked]
|
||||
public T* Get<T>(int_cosize idx) where T : struct
|
||||
{
|
||||
return (T*)(void*)(_Data.Ptr + idx * (_ComponentSize + _IndexSize) + _IndexSize);
|
||||
}
|
||||
|
||||
[Unchecked]
|
||||
public void GetEntities(List<Entity> list)
|
||||
{
|
||||
var ec = (_Data.Count / (_ComponentSize + _IndexSize));
|
||||
list.GrowUninitialized(ec);
|
||||
for (int i < ec)
|
||||
*(list.Ptr + (i)) = *(Entity*)(void*)(_Data.Ptr +i * (_ComponentSize + _IndexSize));
|
||||
}
|
||||
|
||||
public void Use() => _Lock.Enter();
|
||||
public void StopUsing() => _Lock.Exit();
|
||||
|
||||
|
||||
public void Grow(int_cosize amount = 1)
|
||||
{ //Make more size useable
|
||||
_Data.GrowUninitialized((_ComponentSize + _IndexSize) * amount);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,6 @@
|
|||
namespace Theater_ECS;
|
||||
using Theater_ECS.Internal.Managers;
|
||||
|
||||
|
||||
public typealias Entity = int_cosize;
|
||||
|
||||
public typealias Component = int_cosize;
|
||||
|
||||
class ECS
|
||||
{
|
||||
public SystemsManager Systems = new .(this) ~ delete _;
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
namespace Theater_ECS.Example;
|
||||
|
||||
|
||||
class MoverSystem : System
|
||||
{
|
||||
|
||||
[SystemMethod("MoveItems", typeof(Velocity), typeof(Position))]
|
||||
public void MoveItems(ref Velocity v,ref Position p)
|
||||
{
|
||||
//p.x = v.x + p.x;
|
||||
//p.y = v.y = p.y;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace Theater_ECS.Example;
|
||||
|
||||
using System;
|
||||
|
||||
struct Position
|
||||
{
|
||||
public float x = (.)gRand.NextI32() % 25;
|
||||
public float y = (.)gRand.NextI32() % 25;
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
namespace Theater_ECS;
|
||||
using Theater_ECS.Example;
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static uint64 counter = 0;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
ECS ecs = scope .();
|
||||
var pos = ecs.RegisterComponent<Position>(100);
|
||||
//var waste = ecs.RegisterComponent<waste>(100);
|
||||
var vel = ecs.RegisterComponent<Velocity>(100);
|
||||
|
||||
|
||||
List<Entity> entities = scope .();
|
||||
for(int i < 500000)
|
||||
entities.Add(ecs.CreateEntity());
|
||||
|
||||
for(var i in entities)
|
||||
*(Position*)ecs.AddComponentToEntity(i, pos).Value = .();
|
||||
|
||||
for(var i in entities)
|
||||
*(Velocity*)ecs.AddComponentToEntity(i, vel).Value = .();
|
||||
|
||||
/*
|
||||
for(var i in entities)
|
||||
if(gRand.NextI32() % 2 == 1)
|
||||
*(waste*)ecs.AddComponentToEntity(i, waste).Value = .();
|
||||
*/
|
||||
|
||||
MoverSystem s = scope .();
|
||||
s.IntializeSystem(ecs);
|
||||
|
||||
System.Diagnostics.Stopwatch watch = scope .();
|
||||
for(int a < 10)
|
||||
{
|
||||
watch.Start();
|
||||
for(int i < 10)
|
||||
{
|
||||
|
||||
s.RunSystem(ecs);
|
||||
|
||||
}
|
||||
Console.WriteLine(scope $"{watch.ElapsedMilliseconds}ms");
|
||||
watch.Stop();
|
||||
watch.Reset();
|
||||
}
|
||||
//Console.Read();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public struct waste
|
||||
{
|
||||
private float[10] a;
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace Theater_ECS.Example;
|
||||
|
||||
using System;
|
||||
|
||||
struct Velocity
|
||||
{
|
||||
public float x = (.)gRand.NextI32() % 25;
|
||||
public float y = (.)gRand.NextI32() % 25;
|
||||
}
|
|
@ -1,117 +0,0 @@
|
|||
namespace Theater_ECS;
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections;
|
||||
|
||||
[AttributeUsage(.Method)]
|
||||
struct SystemMethodAttribute : Attribute, IOnTypeInit
|
||||
{
|
||||
private StringView _Name;
|
||||
private Span<Type> _Types;
|
||||
|
||||
public this(StringView name, params Span<Type> types)
|
||||
{
|
||||
_Name = name;
|
||||
_Types = types;
|
||||
}
|
||||
|
||||
[Comptime]
|
||||
public void OnTypeInit(Type type, Self* prev)
|
||||
{
|
||||
//InitializeFunction
|
||||
Compiler.EmitTypeBody(type, scope $"""
|
||||
public override bool IntializeSystem(ECS ecs)
|
||||
\{
|
||||
{InitializeSystemTypes( .. scope .())}
|
||||
return true;
|
||||
\}
|
||||
|
||||
""");
|
||||
|
||||
|
||||
Compiler.EmitTypeBody(type, scope $"""
|
||||
[System.DisableChecks]
|
||||
public override void RunSystem(ECS ecs)
|
||||
\{
|
||||
Lock(ecs);
|
||||
|
||||
Component lowest = System.int_cosize.MaxValue;
|
||||
System.int_cosize lowestAmount = System.int_cosize.MaxValue;
|
||||
for(var c in _ComponentMap)
|
||||
\{
|
||||
if(ecs.[System.Friend]_ComponentsPacked[c.value].Count < lowestAmount)
|
||||
\{
|
||||
lowest = c.value;
|
||||
lowestAmount = ecs.[System.Friend]_ComponentsPacked[c.value].Count;
|
||||
\}
|
||||
\}
|
||||
|
||||
|
||||
ecs.GetEntityFromComponent(lowest, _Target);
|
||||
|
||||
{CacheComponentData(.. scope .())}
|
||||
|
||||
|
||||
for(var e in _Target)
|
||||
\{
|
||||
|
||||
{_Name}(
|
||||
{GetComponentData(.. scope .())}
|
||||
);
|
||||
\}
|
||||
|
||||
_Target.Clear();
|
||||
|
||||
Unlock(ecs);
|
||||
|
||||
\}
|
||||
|
||||
private void Lock(ECS ecs)
|
||||
\{
|
||||
for(var i in _ComponentMap)
|
||||
ecs.[System.Friend]_ComponentsPacked[i.value].Use();
|
||||
\}
|
||||
|
||||
private void Unlock(ECS ecs)
|
||||
\{
|
||||
for(var i in _ComponentMap)
|
||||
ecs.[System.Friend]_ComponentsPacked[i.value].StopUsing();
|
||||
\}
|
||||
""");
|
||||
}
|
||||
|
||||
private void InitializeSystemTypes(String buffer)
|
||||
{
|
||||
for(var i in _Types)
|
||||
{
|
||||
buffer.Append(scope $"""
|
||||
if(ecs.GetComponent<{i.GetName( .. scope .())}>() == -1)
|
||||
return false;
|
||||
_ComponentMap.Add(typeof({i.GetName(.. scope .())}), ecs.GetComponent<{i.GetName( .. scope .())}>());
|
||||
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
private void GetComponentData(String buffer)
|
||||
{
|
||||
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])
|
||||
""");
|
||||
if(@i.Index+1 < _Types.Length)
|
||||
buffer.Append(",\n");
|
||||
}
|
||||
}
|
||||
|
||||
private void CacheComponentData(String buffer)
|
||||
{
|
||||
for(var i in _Types)
|
||||
{
|
||||
buffer.Append(scope $"Component _{i.GetName(.. scope .())} = _ComponentMap[typeof({i.GetName(.. scope .())})];\n");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue