Skip to content

Commit

Permalink
ECS - Support QuerySystem without generic component arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Aug 21, 2024
1 parent 0883053 commit 46ca92a
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Systems/BaseSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ internal virtual void AppendPerfStats(StringBuilder sb, int depth)
sb.Append($" {Perf.LastMemory,12}");
sb.Append($" {Perf.SumMemory,12}");

if (this is QuerySystem querySystem) {
if (this is QuerySystemBase querySystem) {
sb.Append($" {querySystem.EntityCount,12}");
}
sb.Append('\n');
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Systems/Extensions/SystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void GetGroupSystems(Archetype type, SystemGroup group, int store
GetGroupSystems(type, systemGroup, storeIndex, subParent, depth + 1);
continue;
}
if (system is QuerySystem querySystem) {
if (system is QuerySystemBase querySystem) {
var query = querySystem.Queries[storeIndex];
if (query.IsMatch(type.ComponentTypes, type.Tags)) {
IncrementCount(parent);
Expand All @@ -98,7 +98,7 @@ private static void GetSystems(Archetype type, SystemGroup group, int storeIndex
GetSystems(type, systemGroup, storeIndex);
continue;
}
if (system is QuerySystem querySystem) {
if (system is QuerySystemBase querySystem) {
var query = querySystem.Queries[storeIndex];
if (query.IsMatch(type.ComponentTypes, type.Tags)) {
AddMatch(new SystemMatch { system = querySystem, count = 1 });
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Internal/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override string ToString()
sb.Append(system.id);
sb.Append(" - ");
switch (system) {
case QuerySystem querySystem:
case QuerySystemBase querySystem:
sb.Append(system.Name);
sb.Append(" - entities: ");
sb.Append(querySystem.EntityCount);
Expand Down
34 changes: 34 additions & 0 deletions src/ECS/Systems/Query/Arg.0.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.

using static System.Diagnostics.DebuggerBrowsableState;
using Browse = System.Diagnostics.DebuggerBrowsableAttribute;
// Hard Rule! file must not have any dependency a to a specific game engine. E.g. Unity, Godot, Monogame, ...

// ReSharper disable ConvertToPrimaryConstructor
// ReSharper disable ConvertToAutoPropertyWithPrivateSetter
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Systems;

/// <summary>
/// A query system returning entities with the specified component type via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem : QuerySystemBase
{
/// <summary> Return all entities matching the <see cref="Query"/>. </summary>
protected ArchetypeQuery Query => query;

public override string ToString() => $"{Name} - []";

#region fields
[Browse(Never)] private ArchetypeQuery query;
#endregion

protected QuerySystem() : base (default) { }

internal override void SetQuery(ArchetypeQuery query) { this.query = query; }

internal override ArchetypeQuery CreateQuery(EntityStore store) {
return store.Query(Filter);
}
}
2 changes: 1 addition & 1 deletion src/ECS/Systems/Query/Arg.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning entities with the specified component type via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem<T1> : QuerySystem
public abstract class QuerySystem<T1> : QuerySystemBase
where T1 : struct, IComponent
{
/// <summary> Return all entities matching the <see cref="Query"/>. </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Query/Arg.2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning entities with the specified component types via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem<T1, T2> : QuerySystem
public abstract class QuerySystem<T1, T2> : QuerySystemBase
where T1 : struct, IComponent
where T2 : struct, IComponent
{
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Query/Arg.3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning entities with the specified component types via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem<T1, T2, T3> : QuerySystem
public abstract class QuerySystem<T1, T2, T3> : QuerySystemBase
where T1 : struct, IComponent
where T2 : struct, IComponent
where T3 : struct, IComponent
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Query/Arg.4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning entities with the specified component types via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem<T1, T2, T3, T4> : QuerySystem
public abstract class QuerySystem<T1, T2, T3, T4> : QuerySystemBase
where T1 : struct, IComponent
where T2 : struct, IComponent
where T3 : struct, IComponent
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Query/Arg.5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning entities with the specified component types via its <see cref="Query"/> property.
/// </summary>
public abstract class QuerySystem<T1, T2, T3, T4, T5> : QuerySystem
public abstract class QuerySystem<T1, T2, T3, T4, T5> : QuerySystemBase
where T1 : struct, IComponent
where T2 : struct, IComponent
where T3 : struct, IComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@ namespace Friflo.Engine.ECS.Systems;
/// <summary>
/// A query system returning the components specified in a subclass extending <c>QuerySystem&lt;T1, ... , Tn></c>.
/// </summary>
public abstract class QuerySystem : BaseSystem
public abstract class QuerySystemBase : BaseSystem
{
#region properties
/// <summary> A query filter used to restrict the entities returned by its <c>Query</c> property. </summary>
/// <summary>
/// A query filter used to restrict the entities returned by its <c>Query</c> property.<br/>
/// See remarks to add a tag filter to a custom <c>QuerySystem</c>.
/// </summary>
/// <remarks>
/// Additional tag filters can be added in the constructor of a class extending a <c>QuerySystem</c>.
/// <code>
/// class MySystem : QuerySystem&lt;Scale3>
/// {
/// public MySystem() => Filter.AnyTags(Tags.Get&lt;MyTag>());
/// protected override void OnUpdate() { ... }
/// }
/// </code>
/// </remarks>
[Browse(Never)] public QueryFilter Filter => filter;

/// <summary> The number of entities matching the <c>Query</c>. </summary>
Expand All @@ -43,7 +56,7 @@ public abstract class QuerySystem : BaseSystem
#endregion

#region constructor
internal QuerySystem(in ComponentTypes componentTypes) {
internal QuerySystemBase(in ComponentTypes componentTypes) {
this.componentTypes = componentTypes;
queries = new ReadOnlyList<ArchetypeQuery>(Array.Empty<ArchetypeQuery>());
}
Expand Down
6 changes: 6 additions & 0 deletions src/Tests/ECS/Systems/Test_QuerySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public static class Test_QuerySystem
[Test]
public static void Test_QuerySystem_ToString()
{
var query0 = new TestQuerySystem0();
var query1 = new TestQuerySystem1();
var query2 = new TestQuerySystem2();
var query3 = new TestQuerySystem3();
var query4 = new TestQuerySystem4();
var query5 = new TestQuerySystem5();

AreEqual("TestQuerySystem0 - []", query0.ToString());
AreEqual("TestQuerySystem1 - [Position]", query1.ToString());
AreEqual("TestQuerySystem2 - [Position, Scale3]", query2.ToString());
AreEqual("TestQuerySystem3 - [Position, Scale3, Rotation]", query3.ToString());
Expand Down Expand Up @@ -56,6 +58,10 @@ public static void Test_System_Enabled()
}
}

internal class TestQuerySystem0 : QuerySystem {
protected override void OnUpdate() { }
}

internal class TestQuerySystem1 : QuerySystem<Position> {
protected override void OnUpdate() { }
}
Expand Down
32 changes: 31 additions & 1 deletion src/Tests/ECS/Systems/Test_ScriptSystems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class SystemSet : Script
private readonly SystemRoot Systems = new ("Systems");

public override void Start() {
QuerySystem system = argCount switch {
QuerySystemBase system = argCount switch {
0 => new MySystem_Arg0(),
1 => new MySystem_Arg1(),
2 => new MySystem_Arg2(),
3 => new MySystem_Arg3(),
Expand All @@ -32,6 +33,17 @@ public override void Update() {
}
}

public class MySystem_Arg0 : QuerySystem
{
/// <summary> Cover <see cref="ChunkEnumerator{T1}.MoveNext"/> </summary>
protected override void OnUpdate()
{
Mem.AreEqual(1000, Query.Count);
Mem.AreEqual(1000, Query.Store.Count);
Mem.AreEqual(1000, Query.Entities.Count);
}
}

public class MySystem_Arg1 : QuerySystem<Position>
{

Expand Down Expand Up @@ -171,6 +183,24 @@ protected override void OnUpdate()

public static class Test_ScriptSystems
{
[Test]
public static void Test_ScriptSystems_query_arg_count_0()
{
var store = SetupTestStore();
var root = store.StoreRoot;
root.AddScript(new SystemSet { argCount = 0 });

var child = store.CreateEntity();
root.AddChild(child);
for (int n = 3; n <= 1000; n++) {
child = child.Archetype.CreateEntity();
root.AddChild(child);
}
CreateSystems(store);
int count = 10; // 10_000_000 ~ #PC: 1575 ms
ExecuteSystems(store, count);
}

[Test]
public static void Test_ScriptSystems_query_arg_count_1()
{
Expand Down

0 comments on commit 46ca92a

Please sign in to comment.