Files
VoxelGame/src/voxelgame/Voxels/ESCThing/StructuralInstance.Builder.cs

137 lines
3.5 KiB
C#
Raw Normal View History

2026-04-04 13:04:05 -04:00
namespace ChickenGameTest;
using System;
using System.Collections.Generic;
using System.Numerics;
// using System.Management;
// public partial class StructuralInstance<TAttribute> where TAttribute : class
// {
public sealed class StructuralBuilder<TAttribute> where TAttribute : class
{
private readonly Dictionary<int, TAttribute> _components = [];
public StructuralBuilder() { }
public StructuralBuilder(IStructuralInstance<TAttribute> existing)
{
foreach (var (id, value) in existing.GetAttributes())
{
_components[id] = value;
}
// for (int i = 0; i < existing.ComponentCount; i++)
// {
// var typeId = existing._attributesTypeIds[i];
// var component = existing.GetComponentAt(i);
// _components[typeId] = component;
// }
}
public StructuralBuilder<TAttribute> Add<T>(T component) where T : TAttribute
{
int id = ComponentTypeRegistry.GetId<T>();
_components[id] = component;
return this;
}
public StructuralBuilder<TAttribute> Upsert<T>(Func<T, T> ifExists, Func<T> none) where T : TAttribute
{
int id = ComponentTypeRegistry.GetId<T>();
_components[id] = _components.TryGetValue(id, out var old) ? ifExists((T)old) : none();
return this;
}
public StructuralBuilder<TAttribute> CombineWith(IStructuralInstance<TAttribute> other, Action<CombineBinder>? configure = null)
{
var binder = new CombineBinder();
configure?.Invoke(binder);
foreach (var (id, value) in other.GetAttributes())
{
if (binder._ignores.Contains(id))
{
continue;
}
if (_components.TryGetValue(id, out var existing)
&& binder._rules.TryGetValue(id, out var rule))
{
_components[id] = rule.Invoke(existing, value);
continue;
}
_components[id] = value;
}
return this;
}
public StructuralBuilder<TAttribute> Remove<T>() where T : TAttribute
{
int id = ComponentTypeRegistry.GetId<T>();
_components.Remove(id);
return this;
}
public bool Has<T>() where T : TAttribute
{
int id = ComponentTypeRegistry.GetId<T>();
return _components.ContainsKey(id);
}
public StructuralInstance<TAttribute> Build()
{
int count = _components.Count;
var typeIds = new int[count];
var components = new TAttribute[count];
int i = 0;
foreach (var kv in _components)
{
typeIds[i] = kv.Key;
components[i] = kv.Value;
i++;
}
Array.Sort(typeIds, components);
return new StructuralInstance<TAttribute>(typeIds, components);
}
public MutableStructuralInstance<TAttribute> BuildMutable()
{
// int count = _components.Count;
var sortedList = new SortedList<int, TAttribute>(_components);
// var typeIds = new int[count];
// var components = new TAttribute[count];
// int i = 0;
// foreach (var kv in _components)
// {
// typeIds[i] = kv.Key;
// components[i] = kv.Value;
// i++;
// }
// Array.Sort(typeIds, components);
return new MutableStructuralInstance<TAttribute>(sortedList);
}
public class CombineBinder
{
internal readonly Dictionary<int, Func<TAttribute, TAttribute, TAttribute>> _rules = [];
internal readonly HashSet<int> _ignores = [];
public CombineBinder Bind<T>(Func<T, T, T> func) where T : TAttribute
{
_rules[ComponentType<T>.Id] = (a, b) => func((T)a, (T)b);
return this;
}
public CombineBinder Ignore<T>() where T : TAttribute
{
_ignores.Add(ComponentType<T>.Id);
return this;
}
}
}
// }