diff --git a/src/gaemstone.ECS/EntityRef.cs b/src/gaemstone.ECS/EntityRef.cs index 0ae590c..67a4e60 100644 --- a/src/gaemstone.ECS/EntityRef.cs +++ b/src/gaemstone.ECS/EntityRef.cs @@ -50,14 +50,17 @@ public unsafe class EntityRef => ecs_delete(World, this); public EntityBuilder NewChild(EntityPath? path = null) - => World.New(EnsureRelativePath(path)).ChildOf(this); + => World.New(this, EnsureRelativePath(path)); public EntityRef? LookupChild(EntityPath path) => World.LookupByPath(this, EnsureRelativePath(path)!); public EntityRef LookupChildOrThrow(EntityPath path) => World.LookupByPathOrThrow(this, EnsureRelativePath(path)!); private static EntityPath? EnsureRelativePath(EntityPath? path) - { if (path?.IsAbsolute == true) throw new ArgumentException("path must not be absolute", nameof(path)); return path; } + { + if (path?.IsAbsolute == true) throw new ArgumentException( + $"Path '{path}' must not be absolute", nameof(path)); return path; + } public EntityRef? Parent diff --git a/src/gaemstone.ECS/World.cs b/src/gaemstone.ECS/World.cs index cd2b445..c52d731 100644 --- a/src/gaemstone.ECS/World.cs +++ b/src/gaemstone.ECS/World.cs @@ -33,14 +33,28 @@ public unsafe partial class World public void Dispose() => ecs_fini(this); + public EntityBuilder New(EntityPath? path = null) => new(this, path); + public EntityBuilder New(EntityRef? parent, EntityPath? path = null) + { + var entity = New(path); + // If given path is absolute, the new entity won't be created as a + // child of the specified parent. Alternatively, EntityRef.NewChild + // can be used, which will throw when an absolute path is given. + if ((path?.IsRelative != false) && (parent != null)) + entity.ChildOf(parent); + return entity; + } + + public bool Progress(TimeSpan delta) => ecs_progress(this, (float)delta.TotalSeconds); public void Quit() => ecs_quit(this); + public static implicit operator ecs_world_t*(World w) => w.Handle; }