diff --git a/plugins/TinyEcs.Plugins/EnableDisable.cs b/plugins/TinyEcs.Plugins/EnableDisable.cs new file mode 100644 index 0000000..8a39cf8 --- /dev/null +++ b/plugins/TinyEcs.Plugins/EnableDisable.cs @@ -0,0 +1,34 @@ +using System.Runtime.CompilerServices; + +namespace TinyEcs; + +public readonly struct Disabled {} + +public static class EnableDisable +{ + [ModuleInitializer] + internal static void ModuleInit() + { + World.OnPluginInitialization += world => { + world.Component(); + }; + } + + public static EntityView Enable(this EntityView entity) + => entity.Unset(); + + public static EntityView Disable(this EntityView entity) + => entity.Set(); + + public static bool IsEnabled(this EntityView entity) + => !entity.Has(); + + public static void Enable(this World world, EcsID id) + => world.Unset(id); + + public static void Disable(this World world, EcsID id) + => world.Set(id); + + public static bool IsEnabled(this World world, EcsID id) + => !world.Has(id); +} diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 0257f67..a30d849 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -12,6 +12,13 @@ .Set(new Position() {X = 2}) .Set(new Velocity()); +e.Disable(); +var enabled = e.IsEnabled(); +e.Disable(); +enabled = e.IsEnabled(); +e.Enable(); +enabled = e.IsEnabled(); + ecs.Filter<(Position, Velocity)>() .Query((EntityView entity) => { Console.WriteLine(entity.Name()); @@ -93,17 +100,21 @@ for (int i = 0; i < ENTITIES_COUNT / 1; i++) ecs.Entity() - .Set(new Position()) - .Set(new Velocity()) + .Set(new Position() {X = i}) + .Set(new Velocity(){X = i}) // .Set() // .Set() // .Set() ; +for (var i = 7000; i < 8000 * 2; ++i) + ecs.Entity((ulong)i).Delete(); + var sw = Stopwatch.StartNew(); var start = 0f; var last = 0f; + while (true) { //var cur = (start - last) / 1000f; @@ -116,29 +127,6 @@ pos.X *= vel.X; pos.Y *= vel.Y; }); - - // foreach (var archetype in ecs.Filter<(Position, Velocity)>()) - // { - // var column0 = archetype.GetComponentIndex(); - // var column1 = archetype.GetComponentIndex(); - - // foreach (ref readonly var chunk in archetype) - // { - // ref var pos = ref chunk.GetReference(column0); - // ref var vel = ref chunk.GetReference(column1); - - // ref var last2 = ref Unsafe.Add(ref pos, chunk.Count); - - // while (Unsafe.IsAddressLessThan(ref pos, ref last2)) - // { - // pos.X *= vel.X; - // pos.Y *= vel.Y; - - // pos = ref Unsafe.Add(ref pos, 1); - // vel = ref Unsafe.Add(ref vel, 1); - // } - // } - // } } last = start; diff --git a/src/EntityView.cs b/src/EntityView.cs index 29dd4f0..0cbf9a8 100644 --- a/src/EntityView.cs +++ b/src/EntityView.cs @@ -76,20 +76,6 @@ public readonly EntityView Unset() where T : struct return this; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly EntityView Enable() - { - World.Unset(ID); - return this; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly EntityView Disable() - { - World.Set(ID); - return this; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ReadOnlySpan Type() => World.GetType(ID); @@ -105,9 +91,6 @@ public readonly EntityView Disable() [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Exists() => World.Exists(ID); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly bool IsEnabled() => !Has(); - public static implicit operator EcsID(EntityView d) => d.ID;