Skip to content

Commit

Permalink
+ remove empty archetypes function
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Sep 11, 2024
1 parent 2e2d972 commit fd66d99
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
56 changes: 53 additions & 3 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@

using var ecs = new World();

var entities = new EntityView[1];
for (var i = 0; i < entities.Length; ++i)
entities[i] = ecs.Entity();

foreach (var e in entities)
{
e.Set(new Component1());
e.Set(new Component2());
e.Set(new Component3());
e.Set(new Component4());
e.Set(new Component5());
}

// while (true)
// {
// foreach (var e in entities)
// e.Get<Component1>() = new Component1();

// // foreach (var e in entities)
// // {
// // e.Unset<Component1>();
// // e.Unset<Component2>();
// // e.Unset<Component3>();
// // e.Unset<Component4>();
// // e.Unset<Component5>();
// // }
// }


var asd = ecs.Entity().Set(new Position());
var name = ecs.Entity<Position>().Name();
Expand All @@ -32,7 +60,7 @@
child.Set(new EquippedItem() { Layer = 3 }, root);

// root.AddChild(child);
root.Delete();
// root.Delete();

ref var equip2 = ref ecs.Get<EquippedItem>(child);
ref var equip = ref ecs.Get<EquippedItem>(child, root);
Expand All @@ -51,6 +79,9 @@
var xx = ecs.Entity().Set(new Position() {X = 3});
ref var qq = ref xx.Get<Position>();


ecs.RemoveEmptyArchetypes();

var query = ecs.Query<(Position, Velocity)>();

// foreach (var (pos, vel) in query.Iter<Position, Velocity>())
Expand Down Expand Up @@ -250,6 +281,13 @@
var scheduler = new Scheduler(ecs);
scheduler.AddResource(0);

void initWnd(Query<int> query)
{

}

scheduler.AddSystem<Query<int>>(initWnd, stage: Stages.Startup);

scheduler.AddSystem((Res<int> res) => Console.WriteLine("system 0 - thread id {0}", Thread.CurrentThread.ManagedThreadId), threadingType: ThreadingMode.Auto);
scheduler.AddSystem(() => Console.WriteLine("system 1 - thread id {0}", Thread.CurrentThread.ManagedThreadId), threadingType: ThreadingMode.Auto);

Expand All @@ -259,8 +297,8 @@
// sysAfter.RunAfter(sys);
// sys.RunAfter(sysAfter);

while (true)
scheduler.Run();
// while (true)
// scheduler.Run();

scheduler.AddSystem((Local<int> i32, Res<string> str, Local<string> strLocal) => {
Console.WriteLine(i32.Value++);
Expand Down Expand Up @@ -550,3 +588,15 @@ struct Networked { }
delegate void Query2Del<T0, T1>(ref T0 t0, ref T1 t1);

struct EquippedItem { public byte Layer; }



public record struct Component1(int Value);

public record struct Component2(int Value);

public record struct Component3(int Value);

public record struct Component4(int Value);

public record struct Component5(int Value);
18 changes: 18 additions & 0 deletions src/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ private void TrimChunksIfNeeded()
Array.Resize(ref _chunks, half);
}

internal void RemoveEmptyArchetypes(ref int removed, Dictionary<EcsID, Archetype> cache)
{
for (var i = _add.Count - 1; i >= 0; --i)
{
var edge = _add[i];
edge.Archetype.RemoveEmptyArchetypes(ref removed, cache);

if (edge.Archetype.Count == 0 && edge.Archetype._add.Count == 0)
{
cache.Remove(edge.Archetype.Id);
_remove.Clear();
_add.RemoveAt(i);

removed += 1;
}
}
}

private static void MakeEdges(Archetype left, Archetype right, EcsID id)
{
left._add.Add(new EcsEdge() { Archetype = right, Id = id });
Expand Down
13 changes: 13 additions & 0 deletions src/World.Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public void Dispose()
_cachedQueries.Clear();
}

/// <summary>
/// Remove all empty archetypes.
/// </summary>
/// <returns></returns>
public int RemoveEmptyArchetypes()
{
var removed = 0;
_archRoot?.RemoveEmptyArchetypes(ref removed, _typeIndex);
if (removed > 0)
LastArchetypeId = ulong.MaxValue;
return removed;
}

/// <summary>
/// Get or create an archetype with the specified components.
/// </summary>
Expand Down

0 comments on commit fd66d99

Please sign in to comment.