Skip to content

Commit

Permalink
fixed relationship deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Feb 29, 2024
1 parent 688b3e2 commit f6b294f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
28 changes: 24 additions & 4 deletions plugins/TinyEcs.Plugins/Relationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public struct Relationship
public readonly RelationshipIterator Children(World world) => new (world, First);
}

public readonly struct Parent { }
public readonly struct Child { }

public ref struct RelationshipIterator
{
private readonly World _world;
Expand Down Expand Up @@ -40,9 +43,6 @@ public bool MoveNext()
public readonly RelationshipIterator GetEnumerator() => this;
}

public readonly struct Parent { }
public readonly struct Child { }

public static class RelationshipPlugin
{
[ModuleInitializer]

Check warning on line 48 in plugins/TinyEcs.Plugins/Relationship.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)
Expand All @@ -51,7 +51,27 @@ internal static void ModuleInit()
World.OnPluginInitialization += world => {
world.Component<Parent>();
world.Component<Child>();
world.OnEntityDeleted += e => e.ClearChildren();
world.Component<Relationship>();
world.OnEntityDeleted += e => {
if (e.Has<Parent>())
{
e.World.Merge();

foreach (var childId in e.Children())
{
e.World.DeferredEntity(childId).Delete();
}

e.World.Merge();
}

if (e.Has<Child>())
{
ref var rel = ref e.Get<Relationship>();
if (rel.Parent != 0 && e.World.Exists(rel.Parent))
e.World.Entity(rel.Parent).RemoveChild(e);
}
};
};
}

Expand Down
39 changes: 26 additions & 13 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
.Set<Position>(new Position() {X = 2})
.Set<Velocity>(new Velocity());


ecs.Filter<(Position, Velocity)>()
.Query((EntityView entity) => {
Console.WriteLine(entity.Name());
});



var e2 = ecs.Entity("Main");
ref var pp = ref e2.Get<Position>();
// var e2 = ecs.Entity("Main");
// ref var pp = ref e2.Get<Position>();

var child = ecs.Entity("child 0");
var child2 = ecs.Entity("child 1");
Expand Down Expand Up @@ -68,21 +67,34 @@

Console.WriteLine();

e.ClearChildren();
ecs.Filter<With<Parent>>().Query((EntityView entity, ref Relationship relation) => {
Console.WriteLine("parent {0} has {1} children", entity.Name(), relation.Count);
});
// e.ClearChildren();
// ecs.Filter<With<Parent>>().Query((EntityView entity, ref Relationship relation) => {
// Console.WriteLine("parent {0} has {1} children", entity.Name(), relation.Count);
// });

for (var i = 0; i < 5; ++i)
{
var c = ecs.Entity();
Console.WriteLine("Add {0}", c.ID);
e.AddChild(c);
}

foreach (var childId in e.Children())
{
Console.WriteLine("child {0}", childId);
}

e.Delete();
var exists = e.Exists();

ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity());
ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity());

for (int i = 0; i < ENTITIES_COUNT / 1; i++)
ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity())
.Set<Position>(new Position())
.Set<Velocity>(new Velocity())
// .Set<PlayerTag>()
// .Set<Dogs>()
// .Set<Likes>()
Expand All @@ -97,7 +109,8 @@
//var cur = (start - last) / 1000f;
for (int i = 0; i < 3600; ++i)
{
ecs.Filter<With<PlayerTag>>()
ecs
//.Filter<With<PlayerTag>>()
.Query((ref Position pos, ref Velocity vel) =>
{
pos.X *= vel.X;
Expand Down
2 changes: 1 addition & 1 deletion src/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public World()

public ReadOnlySpan<Archetype> Archetypes => _archetypes.AsSpan(0, _archetypeCount);

public CommandEntityView DeferredEntity() => _commands.Entity();
public CommandEntityView DeferredEntity(EcsID id = default) => _commands.Entity(id);

public void Merge() => _commands.Merge();

Expand Down

0 comments on commit f6b294f

Please sign in to comment.