Skip to content

Commit

Permalink
Engine - Index: simplify - changed StoreIndex to static class
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jun 21, 2024
1 parent b71e598 commit 1b7a8dc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Engine/src/ECS/Entity/Store/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal partial struct StoreExtension
#endregion

#region component indices
internal StoreIndex[] indexMap; // 8 - map and its component indexes created on demand
internal ComponentIndex[] indexMap; // 8 - map and its component indexes created on demand
#endregion

internal StoreExtension(PidType pidType)
Expand Down
7 changes: 4 additions & 3 deletions Engine/src/ECS/Lab/Component/IndexedComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ private IndexedComponentType(ComponentType componentType, Type indexType) {
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2077", Justification = "TODO")] // TODO
internal ComponentIndex CreateComponentIndex(EntityStore store)
{
var obj = Activator.CreateInstance(indexType);
var index = (ComponentIndex)obj!;
index.store = store;
var obj = Activator.CreateInstance(indexType);
var index = (ComponentIndex)obj!;
index.store = store;
index.componentType = componentType;
return index;
}

Expand Down
10 changes: 8 additions & 2 deletions Engine/src/ECS/Lab/Indexes/ComponentIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace Friflo.Engine.ECS.Index;

internal abstract class ComponentIndex
{
internal abstract int Count { get; }
internal abstract int Count { get; }
public override string ToString() => GetString();

internal EntityStore store; // could be made readonly
internal EntityStore store; // could be made readonly
internal ComponentType componentType; // only for debugging

internal abstract void Add <TComponent>(int id, in TComponent component) where TComponent : struct, IComponent;
internal abstract void Update<TComponent>(int id, in TComponent component, StructHeap heap) where TComponent : struct, IComponent;
Expand All @@ -20,6 +22,10 @@ internal abstract class ComponentIndex
internal NotSupportedException NotSupportedException(string name) {
return new NotSupportedException($"{name} not supported by {GetType().Name}");
}

private string GetString() {
return $"{componentType.Name} - {GetType().Name} count: {Count}";
}
}

internal abstract class ComponentIndex<TValue> : ComponentIndex
Expand Down
63 changes: 0 additions & 63 deletions Engine/src/ECS/Lab/StoreIndex.cs

This file was deleted.

34 changes: 34 additions & 0 deletions Engine/src/ECS/Lab/Utils/StoreIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

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

internal static class StoreIndex
{
internal static ComponentIndex GetIndex(EntityStore store, int structIndex)
{
var indexMap = store.extension.indexMap;
if (indexMap != null) {
var index = indexMap[structIndex];
if (index != null) {
return index;
}
return indexMap[structIndex] = CreateIndex(store, structIndex);
}
indexMap = store.extension.indexMap = CreateStoreIndexMap();
return indexMap[structIndex] = CreateIndex(store, structIndex);
}

private static ComponentIndex CreateIndex(EntityStore store, int structIndex)
{
var type = EntityStoreBase.Static.EntitySchema.indexedComponentMap[structIndex];
return type.CreateComponentIndex(store);
}

private static ComponentIndex[] CreateStoreIndexMap()
{
var schema = EntityStoreBase.Static.EntitySchema;
return new ComponentIndex[schema.maxStructIndex]; // could create smaller array containing no null elements
}
}
4 changes: 1 addition & 3 deletions Engine/src/Tests-internal/ECS/Test_Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ public static void Test_Index_StoreIndex_ToString()
entity1.AddComponent(new IndexedName { name = "test" });

var indexMap = store.extension.indexMap;
AreEqual(null, indexMap[StructInfo<Position>.Index].ToString());

AreEqual("IndexedEntity", indexMap[StructInfo<IndexedEntity>.Index].ToString());
AreEqual(null, indexMap[StructInfo<Position>.Index]);

AreEqual("IndexedName - ValueClassIndex`1 count: 1", indexMap[StructInfo<IndexedName>.Index].ToString());

Expand Down

0 comments on commit 1b7a8dc

Please sign in to comment.