From dc776ace1f0a253441ebfd0562a7f4d08e870ba3 Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 20 Jan 2023 18:56:12 +0100 Subject: [PATCH] Remove LookupByType and related methods --- src/gaemstone.ECS/Component.cs | 7 +------ src/gaemstone.ECS/EntityBase.cs | 25 ------------------------ src/gaemstone.ECS/EntityRef.cs | 2 -- src/gaemstone.ECS/IdRef.cs | 8 -------- src/gaemstone.ECS/Iterator.cs | 5 ----- src/gaemstone.ECS/World+Lookup.cs | 32 ------------------------------- 6 files changed, 1 insertion(+), 78 deletions(-) diff --git a/src/gaemstone.ECS/Component.cs b/src/gaemstone.ECS/Component.cs index b614a6e..27cdffa 100644 --- a/src/gaemstone.ECS/Component.cs +++ b/src/gaemstone.ECS/Component.cs @@ -7,11 +7,6 @@ namespace gaemstone.ECS; public static unsafe class ComponentExtensions { - public static EntityRef InitComponent(this EntityRef entity, Type type) - => (EntityRef)typeof(ComponentExtensions) - .GetMethod(nameof(InitComponent), new[] { typeof(EntityRef) })! - .MakeGenericMethod(type).Invoke(null, new[]{ entity })!; - public static EntityRef InitComponent(this EntityRef entity) { if (typeof(T).IsPrimitive) throw new ArgumentException( @@ -37,7 +32,7 @@ public static unsafe class ComponentExtensions ecs_set_hooks_id(entity.World, entity, &typeHooks); } - return entity.CreateLookup(typeof(T)); + return entity; } } diff --git a/src/gaemstone.ECS/EntityBase.cs b/src/gaemstone.ECS/EntityBase.cs index 8b0d85a..36cc2ca 100644 --- a/src/gaemstone.ECS/EntityBase.cs +++ b/src/gaemstone.ECS/EntityBase.cs @@ -5,29 +5,18 @@ public abstract class EntityBase { public abstract World World { get; } - public abstract TReturn Add(Id id); public abstract TReturn Remove(Id id); public abstract bool Has(Id id); public TReturn Add(string symbol) => Add(World.LookupBySymbolOrThrow(symbol)); - public TReturn Add() => Add(World.LookupByTypeOrThrow(typeof(T))); public TReturn Add(Entity relation, Entity target) => Add(Id.Pair(relation, target)); - public TReturn Add(Entity target) => Add(World.LookupByTypeOrThrow(), target); - public TReturn Add() => Add(World.LookupByTypeOrThrow(), World.LookupByTypeOrThrow()); public TReturn Remove(string symbol) => Remove(World.LookupBySymbolOrThrow(symbol)); - public TReturn Remove() => Remove(World.LookupByTypeOrThrow(typeof(T))); public TReturn Remove(Entity relation, Entity target) => Remove(Id.Pair(relation, target)); - public TReturn Remove(Entity target) => Remove(World.LookupByTypeOrThrow(), target); - public TReturn Remove() => Remove(World.LookupByTypeOrThrow(), World.LookupByTypeOrThrow()); public bool Has(string symbol) => Has(World.LookupBySymbolOrThrow(symbol)); - public bool Has() => Has(World.LookupByTypeOrThrow(typeof(T))); public bool Has(Entity relation, Entity target) => Has(Id.Pair(relation, target)); - public bool Has(Entity target) => Has(World.LookupByTypeOrThrow(), target); - public bool Has() => Has(World.LookupByTypeOrThrow(), World.LookupByTypeOrThrow()); - public abstract T? GetOrNull(Id id) where T : unmanaged; public abstract T? GetOrNull(Id id, T _ = null!) where T : class; @@ -37,24 +26,10 @@ public abstract class EntityBase public abstract ref T GetRefOrThrow(Id id) where T : unmanaged; public abstract void Modified(Id id); - public T? GetOrNull() where T : unmanaged => GetOrNull(World.LookupByTypeOrThrow()); - public T? GetOrNull(T _ = null!) where T : class => GetOrNull(World.LookupByTypeOrThrow()); - public T GetOrThrow() => GetOrThrow(World.LookupByTypeOrThrow()); - public ref T GetMut() where T : unmanaged => ref GetMut(World.LookupByTypeOrThrow()); - public ref T GetRefOrNull() where T : unmanaged => ref GetRefOrNull(World.LookupByTypeOrThrow()); - public ref T GetRefOrThrow() where T : unmanaged => ref GetRefOrThrow(World.LookupByTypeOrThrow()); - public void Modified() => Modified(World.LookupByTypeOrThrow()); - - public abstract TReturn Set(Id id, in T value) where T : unmanaged; public abstract TReturn Set(Id id, T obj) where T : class; - public TReturn Set(in T value) where T : unmanaged => Set(World.LookupByTypeOrThrow(), value); - public TReturn Set(T obj) where T : class => Set(World.LookupByTypeOrThrow(), obj); - - public TReturn ChildOf(Entity parent) => Add(World.ChildOf, parent); - public TReturn ChildOf() => Add(World.ChildOf, World.LookupByTypeOrThrow()); public TReturn Disable() => Add(World.Disabled); public TReturn Enable() => Remove(World.Disabled); diff --git a/src/gaemstone.ECS/EntityRef.cs b/src/gaemstone.ECS/EntityRef.cs index 6a52a75..d7fb53b 100644 --- a/src/gaemstone.ECS/EntityRef.cs +++ b/src/gaemstone.ECS/EntityRef.cs @@ -152,8 +152,6 @@ public unsafe class EntityRef { var index = 0; while (GetTarget(relation, index++) is EntityRef target) yield return target; } public IEnumerable GetTargets(string symbol) => GetTargets(World.LookupBySymbolOrThrow(symbol)); - public IEnumerable GetTargets() - => GetTargets(World.LookupByTypeOrThrow(typeof(T))); public bool Equals(EntityRef? other) => (other is not null) && (World == other.World) && (Entity == other.Entity); public override bool Equals(object? obj) => Equals(obj as EntityRef); diff --git a/src/gaemstone.ECS/IdRef.cs b/src/gaemstone.ECS/IdRef.cs index 3e2fa82..1587734 100644 --- a/src/gaemstone.ECS/IdRef.cs +++ b/src/gaemstone.ECS/IdRef.cs @@ -33,14 +33,6 @@ public unsafe class IdRef public static IdRef Pair(EntityRef relation, EntityRef target) => Pair(relation.World, relation, target); - public static IdRef Pair(World world, Entity target) - => Pair(world, world.LookupByTypeOrThrow(), target); - public static IdRef Pair(EntityRef target) - => Pair(target.World.LookupByTypeOrThrow(), target); - public static IdRef Pair(World world) - => Pair(world, world.LookupByTypeOrThrow(), - world.LookupByTypeOrThrow()); - public EntityRef? AsEntity() => (Flags == default) ? World.LookupAlive(new Entity(new() { Data = Id })) : null; public (EntityRef Relation, EntityRef Target)? AsPair() diff --git a/src/gaemstone.ECS/Iterator.cs b/src/gaemstone.ECS/Iterator.cs index 939132a..fdf69fb 100644 --- a/src/gaemstone.ECS/Iterator.cs +++ b/src/gaemstone.ECS/Iterator.cs @@ -83,11 +83,6 @@ public unsafe class Iterator return ecs_field_id(ptr, index) == id.Value; } - // TODO: Potentially misleading, doesn't check the field's backing data type. - // The id might be "(Identifier, Name)", but its data type "Identifier". - public bool FieldIs(int index) - => FieldIs(index, World.LookupByType()); - public IdRef FieldId(int index) { fixed (ecs_iter_t* ptr = &Value) diff --git a/src/gaemstone.ECS/World+Lookup.cs b/src/gaemstone.ECS/World+Lookup.cs index ec613d6..5b2952a 100644 --- a/src/gaemstone.ECS/World+Lookup.cs +++ b/src/gaemstone.ECS/World+Lookup.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using gaemstone.Utility; using static flecs_hub.flecs; @@ -7,29 +6,6 @@ namespace gaemstone.ECS; public unsafe partial class World { - private readonly Dictionary _lookupByType = new(); - - public void AddLookupByType(Type type, Entity entity) - { - // If an existing lookup already exists with the same entity, don't throw an exception. - if (_lookupByType.TryGetValue(type, out var existing) && (existing == entity)) return; - _lookupByType.Add(type, entity); - } - public void RemoveLookupByType(Type type) - { if (!_lookupByType.Remove(type)) throw new InvalidOperationException( - $"Lookup for {type} does not exist"); } - - - public EntityRef? LookupByType() - => LookupByType(typeof(T)); - public EntityRef? LookupByType(Type type) - => LookupAlive(_lookupByType.GetValueOrDefault(type)); - public EntityRef LookupByTypeOrThrow() - => LookupByTypeOrThrow(typeof(T)); - public EntityRef LookupByTypeOrThrow(Type type) - => LookupByType(type) ?? throw new EntityNotFoundException( - $"Entity of type {type} not found"); - public EntityRef? LookupAlive(Entity value) => EntityRef.CreateOrNull(this, new(ecs_get_alive(this, value))); public EntityRef LookupAliveOrThrow(Entity entity) @@ -61,11 +37,3 @@ public unsafe partial class World public class EntityNotFoundException : Exception { public EntityNotFoundException(string message) : base(message) { } } } - -public static class LookupExtensions -{ - public static EntityRef CreateLookup(this EntityRef entity) - => entity.CreateLookup(typeof(T)); - public static EntityRef CreateLookup(this EntityRef entity, Type type) - { entity.World.AddLookupByType(type, entity); return entity; } -}