Skip to content

Commit

Permalink
enable disable
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 6, 2024
1 parent 1e44928 commit f4d6cfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
34 changes: 34 additions & 0 deletions plugins/TinyEcs.Plugins/EnableDisable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Runtime.CompilerServices;

namespace TinyEcs;

public readonly struct Disabled {}

public static class EnableDisable
{
[ModuleInitializer]

Check warning on line 9 in plugins/TinyEcs.Plugins/EnableDisable.cs

View workflow job for this annotation

GitHub Actions / build

The 'ModuleInitializer' attribute is only intended to be used in application code or advanced source generator scenarios (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2255)
internal static void ModuleInit()
{
World.OnPluginInitialization += world => {
world.Component<Disabled>();
};
}

public static EntityView Enable(this EntityView entity)
=> entity.Unset<Disabled>();

public static EntityView Disable(this EntityView entity)
=> entity.Set<Disabled>();

public static bool IsEnabled(this EntityView entity)
=> !entity.Has<Disabled>();

public static void Enable(this World world, EcsID id)
=> world.Unset<Disabled>(id);

public static void Disable(this World world, EcsID id)
=> world.Set<Disabled>(id);

public static bool IsEnabled(this World world, EcsID id)
=> !world.Has<Disabled>(id);
}
38 changes: 13 additions & 25 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
.Set<Position>(new Position() {X = 2})
.Set<Velocity>(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());
Expand Down Expand Up @@ -93,17 +100,21 @@

for (int i = 0; i < ENTITIES_COUNT / 1; i++)
ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity())
.Set<Position>(new Position() {X = i})
.Set<Velocity>(new Velocity(){X = i})
// .Set<PlayerTag>()
// .Set<Dogs>()
// .Set<Likes>()
;

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;
Expand All @@ -116,29 +127,6 @@
pos.X *= vel.X;
pos.Y *= vel.Y;
});

// foreach (var archetype in ecs.Filter<(Position, Velocity)>())
// {
// var column0 = archetype.GetComponentIndex<Position>();
// var column1 = archetype.GetComponentIndex<Velocity>();

// foreach (ref readonly var chunk in archetype)
// {
// ref var pos = ref chunk.GetReference<Position>(column0);
// ref var vel = ref chunk.GetReference<Velocity>(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;
Expand Down
17 changes: 0 additions & 17 deletions src/EntityView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,6 @@ public readonly EntityView Unset<T>() where T : struct
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly EntityView Enable()
{
World.Unset<EcsDisabled>(ID);
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly EntityView Disable()
{
World.Set<EcsDisabled>(ID);
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<EcsComponent> Type() => World.GetType(ID);

Expand All @@ -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<EcsDisabled>();


public static implicit operator EcsID(EntityView d) => d.ID;

Expand Down

0 comments on commit f4d6cfd

Please sign in to comment.