Skip to content

Commit

Permalink
add LinkComponentIndex<> - made IndexExtensions.GetAllLinkedEntities(…
Browse files Browse the repository at this point in the history
…) obsolete
  • Loading branch information
friflo committed Dec 1, 2024
1 parent 279922e commit 724d36d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
22 changes: 1 addition & 21 deletions src/ECS/Index/ComponentIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,5 @@ internal ComponentIndex(AbstractComponentIndex<TValue> index) {
/// </remarks>
public IReadOnlyCollection<TValue> Values => index.IndexedComponentValues;

/// <summary>
/// Returns all entities linked by the specified <see cref="ILinkComponent"/> type.<br/>
/// Executes in O(1). Each entity in the returned list is unique. See remarks for additional infos.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>
/// The returned collection changes when component link values are updated, removed or added.
/// </item>
/// <item>
/// To get the entities linking a specific entity use <see cref="IndexExtensions.GetIncomingLinks{TComponent}"/>.<br/>
/// </item>
/// <item>
/// The method is a specialized version of <see cref="Values"/><br/>
/// using <c> TIndexedComponent = ILinkComponent</c> and <c>TValue = Entity</c>.
/// </item>
/// </list>
/// </remarks>
public IReadOnlyCollection<Entity> LinkedEntities => ((AbstractComponentIndex<Entity>)(object)index).IndexedComponentValues;

public override string ToString() => $"Count: {Values.Count}";
}
}
2 changes: 1 addition & 1 deletion src/ECS/Index/ILinkComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Friflo.Engine.ECS;
/// </item>
/// <item>
/// Return all entities linked by a specific <see cref="ILinkComponent"/> type.<br/>
/// See <see cref="IndexExtensions.GetAllLinkedEntities{TComponent}"/>
/// See <see cref="LinkComponentIndex{TLinkComponent}.Values"/>
/// </item>
/// <item>
/// Filter entities in a query having a <see cref="ILinkComponent"/> to a specific entity.<br/>
Expand Down
14 changes: 13 additions & 1 deletion src/ECS/Index/IndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public static ComponentIndex<TIndexedComponent,TValue> ComponentIndex<TIndexedCo
var index = (AbstractComponentIndex<TValue>)StoreIndex.GetIndex(store, StructInfo<TIndexedComponent>.Index);
return new ComponentIndex<TIndexedComponent, TValue>(index);
}

/// <summary>
/// Returns the index for link components to search entities with a specific entity in O(1).<br/>
/// Executes in O(1).
/// </summary>
public static LinkComponentIndex<TLinkComponent> LinkComponentIndex<TLinkComponent>(this EntityStore store)
where TLinkComponent: struct, ILinkComponent
{
var index = (AbstractComponentIndex<Entity>)StoreIndex.GetIndex(store, StructInfo<TLinkComponent>.Index);
return new LinkComponentIndex<TLinkComponent>(index);
}

/// <summary>
/// Obsolete: Use <see cref="ComponentIndex{TIndexedComponent,TValue}.this[TValue]"/><br/>
Expand Down Expand Up @@ -82,7 +93,7 @@ public static IReadOnlyCollection<TValue> GetAllIndexedComponentValues<TCompone
}

/// <summary>
/// Obsolete: Use <see cref="ComponentIndex{TIndexedComponent,TValue}.LinkedEntities"/><br/>
/// Obsolete: Use <see cref="LinkComponentIndex{TLinkComponent}.Values"/><br/>
/// Returns all entities linked by the specified <see cref="ILinkComponent"/> type.<br/>
/// Executes in O(1). Each entity in the returned list is unique. See remarks for additional infos.
/// </summary>
Expand All @@ -100,6 +111,7 @@ public static IReadOnlyCollection<TValue> GetAllIndexedComponentValues<TCompone
/// </item>
/// </list>
/// </remarks>
[Obsolete("replace with property: LinkComponentIndex<TLinkComponent>().Values")]
public static IReadOnlyCollection<Entity> GetAllLinkedEntities<TComponent>(this EntityStore store)
where TComponent: struct, ILinkComponent
{
Expand Down
39 changes: 39 additions & 0 deletions src/ECS/Index/LinkComponentIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using Friflo.Engine.ECS.Index;

// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS;

public readonly struct LinkComponentIndex <TLinkComponent>
where TLinkComponent: struct, ILinkComponent
{
private readonly AbstractComponentIndex<Entity> index;

internal LinkComponentIndex(AbstractComponentIndex<Entity> index) {
this.index = index;
}

/// <summary>
/// Return the entities having a link component with the passed component value.<br/>
/// Executes in O(1) with default index.
/// </summary>
public Entities this[Entity value] => index.GetHasValueEntities(value);

/// <summary>
/// Returns all indexed link component values of the passed<see cref="Entity"/> type.<br/>
/// Executes in O(1). Each value in the returned list is unique. See remarks for additional infos.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>
/// The returned collection changes when indexed component values are updated, removed or added.
/// </item>
/// <item>
/// To get the entities having a specific component value use <see cref="this[Entity]"/>.
/// </item>
/// </list>
/// </remarks>
public IReadOnlyCollection<Entity> Values => index.IndexedComponentValues;

public override string ToString() => $"Count: {Values.Count}";
}
6 changes: 5 additions & 1 deletion src/Tests/ECS/Index/Test_Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static void Test_Index_Component_Update()
public static void Test_Index_indexed_Entity()
{
var store = new EntityStore();
var index = store.LinkComponentIndex<LinkComponent>();
var entity1 = store.CreateEntity(1);
var entity2 = store.CreateEntity(2);
var entity3 = store.CreateEntity(3);
Expand All @@ -113,11 +114,14 @@ public static void Test_Index_indexed_Entity()
var target5 = store.CreateEntity(5);
var target6 = store.CreateEntity(6);

var values = store.GetAllLinkedEntities<LinkComponent>();
var values = index.Values;

entity1.AddComponent(new LinkComponent { entity = target4 }); AreEqual(1, values.Count);
entity2.AddComponent(new LinkComponent { entity = target5 }); AreEqual(2, values.Count);
entity3.AddComponent(new LinkComponent { entity = target5 }); AreEqual(2, values.Count);

AreEqual(1, index[target4].Count);
AreEqual(2, index[target5].Count);

int count = 0;
foreach (var entity in values) {
Expand Down

0 comments on commit 724d36d

Please sign in to comment.