You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.4 KiB
117 lines
4.4 KiB
1 year ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using gaemstone.Utility;
|
||
|
using static flecs_hub.flecs;
|
||
|
|
||
|
namespace gaemstone.ECS;
|
||
|
|
||
|
public class EntityBuilder
|
||
|
: EntityBase<EntityBuilder>
|
||
|
{
|
||
|
public override World World { get; }
|
||
|
|
||
|
/// <summary> Set to modify existing entity (optional). </summary>
|
||
|
public Entity Id { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Path of the entity. If no entity is provided, an entity with this path
|
||
|
/// will be looked up first. When an entity is provided, the path will be
|
||
|
/// verified with the existing entity.
|
||
|
/// </summary>
|
||
|
public EntityPath? Path { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Optional entity symbol. A symbol is an unscoped identifier that can
|
||
|
/// be used to lookup an entity. The primary use case for this is to
|
||
|
/// associate the entity with a language identifier, such as a type or
|
||
|
/// function name, where these identifiers differ from the name they are
|
||
|
/// registered with in flecs.
|
||
|
/// </summary>
|
||
|
public EntityBuilder Symbol(string symbol) { _symbol = symbol; return this; }
|
||
|
private string? _symbol = null;
|
||
|
|
||
|
/// <summary>
|
||
|
/// When set to true, a low id (typically reserved for components)
|
||
|
/// will be used to create the entity, if no id is specified.
|
||
|
/// </summary>
|
||
|
public bool UseLowId { get; set; }
|
||
|
|
||
|
/// <summary> Ids to add to the new or existing entity. </summary>
|
||
|
private readonly HashSet<Identifier> _toAdd = new();
|
||
|
private Entity _parent = Entity.None;
|
||
|
|
||
|
/// <summary> String expression with components to add. </summary>
|
||
|
public string? Expression { get; }
|
||
|
|
||
|
/// <summary> Actions to run once the entity has been created. </summary>
|
||
|
private readonly List<Action<EntityRef>> _toSet = new();
|
||
|
|
||
|
public EntityBuilder(World world, EntityPath? path = null)
|
||
|
{ World = world; Path = path; }
|
||
|
|
||
|
public override EntityBuilder Add(Identifier id)
|
||
|
{
|
||
|
// If adding a ChildOf relation, store the parent separately.
|
||
|
if (id.AsPair(World) is (EntityRef relation, EntityRef target) &&
|
||
|
(relation == World.ChildOf)) { _parent = target; return this; }
|
||
|
|
||
|
if (_toAdd.Count == 31) throw new NotSupportedException(
|
||
|
"Must not add more than 31 Ids at once with EntityBuilder");
|
||
|
_toAdd.Add(id);
|
||
|
|
||
|
return this;
|
||
|
}
|
||
|
public override EntityBuilder Remove(Identifier id)
|
||
|
=> throw new NotSupportedException();
|
||
|
public override bool Has(Identifier id)
|
||
|
=> !id.IsWildcard ? _toAdd.Contains(id)
|
||
|
: throw new NotSupportedException(); // TODO: Support wildcard.
|
||
|
|
||
|
public override T Get<T>(Identifier id) => throw new NotSupportedException();
|
||
|
public override T? GetOrNull<T>(Identifier id) => throw new NotSupportedException();
|
||
|
public override T? GetOrNull<T>(Identifier id, T _ = null!) where T : class => throw new NotSupportedException();
|
||
|
public override ref T GetMut<T>(Identifier id) => throw new NotSupportedException();
|
||
|
public override ref T GetRefOrNull<T>(Identifier id) => throw new NotSupportedException();
|
||
|
public override ref T GetRefOrThrow<T>(Identifier id) => throw new NotSupportedException();
|
||
|
public override void Modified<T>(Identifier id) => throw new NotImplementedException();
|
||
|
|
||
|
public override EntityBuilder Set<T>(Identifier id, in T value)
|
||
|
// "in" can't be used with lambdas, so we make a local copy.
|
||
|
{ var copy = value; _toSet.Add(e => e.Set(id, copy)); return this; }
|
||
|
|
||
|
public override EntityBuilder Set<T>(Identifier id, T obj)
|
||
|
{ _toSet.Add(e => e.Set(id, obj)); return this; }
|
||
|
|
||
|
public unsafe EntityRef Build()
|
||
|
{
|
||
|
var parent = _parent;
|
||
|
|
||
|
if (Path != null) {
|
||
|
if (parent.IsSome && Path.IsAbsolute) throw new InvalidOperationException(
|
||
|
"Entity already has parent set (via ChildOf), so path must not be absolute");
|
||
|
// If path specifies more than just a name, ensure the parent entity exists.
|
||
|
if (Path.Count > 1) parent = EntityPath.EnsureEntityExists(World, parent, Path.Parent!);
|
||
|
}
|
||
|
|
||
|
using var alloc = TempAllocator.Use();
|
||
|
var desc = new ecs_entity_desc_t {
|
||
|
id = Id,
|
||
|
name = (Path != null) ? alloc.AllocateCString(Path.Name.AsSpan()) : default,
|
||
|
symbol = alloc.AllocateCString(_symbol),
|
||
|
add_expr = alloc.AllocateCString(Expression),
|
||
|
use_low_id = UseLowId,
|
||
|
sep = CStringExtensions.ETX,
|
||
|
};
|
||
|
|
||
|
var add = desc.add; var index = 0;
|
||
|
if (parent.IsSome) add[index++] = Identifier.Pair(World.ChildOf, parent);
|
||
|
foreach (var id in _toAdd) add[index++] = id;
|
||
|
|
||
|
var entityId = ecs_entity_init(World, &desc);
|
||
|
var entity = new EntityRef(World, new(entityId));
|
||
|
foreach (var action in _toSet) action(entity);
|
||
|
|
||
|
return entity;
|
||
|
}
|
||
|
}
|