From 210be7f15f24d68168cdf0a7a963895cbf668358 Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Fri, 22 Nov 2024 10:10:05 +0100 Subject: [PATCH] Fix wrong merge --- src/ComponentManager.bf | 109 -------------------------------- src/ECS.bf | 5 -- src/Example/MoverSystem.bf | 13 ---- src/Example/Position.bf | 9 --- src/Example/Program.bf | 62 ------------------- src/Example/Velocity.bf | 9 --- src/SystemMethodAttribute.bf | 117 ----------------------------------- 7 files changed, 324 deletions(-) delete mode 100644 src/ComponentManager.bf delete mode 100644 src/Example/MoverSystem.bf delete mode 100644 src/Example/Position.bf delete mode 100644 src/Example/Program.bf delete mode 100644 src/Example/Velocity.bf delete mode 100644 src/SystemMethodAttribute.bf diff --git a/src/ComponentManager.bf b/src/ComponentManager.bf deleted file mode 100644 index 8fe5778..0000000 --- a/src/ComponentManager.bf +++ /dev/null @@ -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 _Data = null ~ delete _; - private List _Available = new .() ~ delete _; - private HashSet _Deleted = new .() ~ delete _; - - ///Make the component manager useable for a specific type of component - public void Initialize(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 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() where T : struct - { - return .((.)(void*)_Data.Ptr, Count); - } - - [Unchecked] - public T* Get(int_cosize idx) where T : struct - { - return (T*)(void*)(_Data.Ptr + idx * (_ComponentSize + _IndexSize) + _IndexSize); - } - - [Unchecked] - public void GetEntities(List 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); - } - -} \ No newline at end of file diff --git a/src/ECS.bf b/src/ECS.bf index 370b889..91dbc5a 100644 --- a/src/ECS.bf +++ b/src/ECS.bf @@ -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 _; diff --git a/src/Example/MoverSystem.bf b/src/Example/MoverSystem.bf deleted file mode 100644 index e02cec7..0000000 --- a/src/Example/MoverSystem.bf +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/src/Example/Position.bf b/src/Example/Position.bf deleted file mode 100644 index 1157b34..0000000 --- a/src/Example/Position.bf +++ /dev/null @@ -1,9 +0,0 @@ -namespace Theater_ECS.Example; - -using System; - -struct Position -{ - public float x = (.)gRand.NextI32() % 25; - public float y = (.)gRand.NextI32() % 25; -} \ No newline at end of file diff --git a/src/Example/Program.bf b/src/Example/Program.bf deleted file mode 100644 index 1c78700..0000000 --- a/src/Example/Program.bf +++ /dev/null @@ -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(100); - //var waste = ecs.RegisterComponent(100); - var vel = ecs.RegisterComponent(100); - - - List 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; -} \ No newline at end of file diff --git a/src/Example/Velocity.bf b/src/Example/Velocity.bf deleted file mode 100644 index 8067be8..0000000 --- a/src/Example/Velocity.bf +++ /dev/null @@ -1,9 +0,0 @@ -namespace Theater_ECS.Example; - -using System; - -struct Velocity -{ - public float x = (.)gRand.NextI32() % 25; - public float y = (.)gRand.NextI32() % 25; -} \ No newline at end of file diff --git a/src/SystemMethodAttribute.bf b/src/SystemMethodAttribute.bf deleted file mode 100644 index 1541cea..0000000 --- a/src/SystemMethodAttribute.bf +++ /dev/null @@ -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 _Types; - - public this(StringView name, params Span 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"); - } - } -} \ No newline at end of file