Skip to content

Commit

Permalink
Systems: Made default OnAddStore() / OnRemoveStore() methods a NOP op…
Browse files Browse the repository at this point in the history
…eration
  • Loading branch information
friflo committed Jan 2, 2025
1 parent a6d3950 commit 8d5da55
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 23 deletions.
11 changes: 7 additions & 4 deletions src/ECS/Systems/BaseSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ internal static void CastSystemRemoved(BaseSystem system, SystemRoot oldRoot, Sy
#endregion

#region virtual - store: add / remove
protected internal virtual void OnRemoveStore(EntityStore store) { }
protected internal virtual void OnAddStore (EntityStore store) { }
internal virtual void AddStoreInternal (EntityStore store) => OnAddStore(store);
internal virtual void RemoveStoreInternal(EntityStore store) => OnRemoveStore(store);

protected virtual void OnAddStore (EntityStore store) { }
protected virtual void OnRemoveStore(EntityStore store) { }
#endregion

#region virtual - system: update
Expand Down Expand Up @@ -207,7 +210,7 @@ internal void SetParentAndRoot(SystemGroup group)
system.systemRoot = newRoot;
newRoot.AddSystemToRoot(system);
foreach (var store in newRoot.stores) {
system.OnAddStore(store);
system.AddStoreInternal(store);
}
}
}
Expand All @@ -225,7 +228,7 @@ internal void ClearParentAndRoot()
system.systemRoot = null;
currentRoot.RemoveSystemFromRoot(system);
foreach (var store in currentRoot.stores) {
system.OnRemoveStore(store);
system.RemoveStoreInternal(store);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/ECS/Systems/QueryBaseSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ internal QuerySystemBase(in ComponentTypes componentTypes) {
#endregion

#region store: add / remove
protected internal override void OnAddStore(EntityStore entityStore)
internal override void AddStoreInternal(EntityStore entityStore)
{
var query = CreateQuery(entityStore);
queries.Add(query);
OnAddStore(entityStore);
}

protected internal override void OnRemoveStore(EntityStore entityStore)
internal override void RemoveStoreInternal(EntityStore entityStore)
{
OnRemoveStore(entityStore);
foreach (var query in queries) {
if (query.Store != entityStore) {
continue;
Expand Down
6 changes: 4 additions & 2 deletions src/ECS/Systems/SystemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,17 @@ public void SetName(string name) {
#endregion

#region store: add / remove
protected internal override void OnAddStore(EntityStore entityStore)
internal override void AddStoreInternal(EntityStore entityStore)
{
var commandBuffer = entityStore.GetCommandBuffer();
commandBuffer.ReuseBuffer = true;
commandBuffers.Add(commandBuffer);
OnAddStore(entityStore);
}

protected internal override void OnRemoveStore(EntityStore entityStore)
internal override void RemoveStoreInternal(EntityStore entityStore)
{
OnRemoveStore(entityStore);
foreach (var commandBuffer in commandBuffers) {
if (commandBuffer.EntityStore != entityStore) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/Systems/SystemRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void AddStore(EntityStore entityStore)
stores.Add(entityStore);
var rootSystems = GetSubSystems(ref systemBuffer);
foreach (var system in rootSystems) {
system.OnAddStore(entityStore);
system.AddStoreInternal(entityStore);
}
}

Expand All @@ -80,7 +80,7 @@ public void RemoveStore(EntityStore entityStore)
stores.Remove(entityStore);
var rootSystems = GetSubSystems(ref systemBuffer);
foreach (var system in rootSystems) {
system.OnRemoveStore(entityStore);
system.RemoveStoreInternal(entityStore);
}
}
#endregion
Expand Down
1 change: 0 additions & 1 deletion src/Tests/ECS/Examples/HelloSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class CustomQuerySystem : QuerySystem

protected override void OnAddStore(EntityStore store) {
customQuery = store.Query<Position>();
base.OnAddStore(store); // must be called to ensure execution of OnUpdate()
}

/// Executes the customQuery instead of the base class Query.
Expand Down
51 changes: 44 additions & 7 deletions src/Tests/ECS/Systems/Systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,60 @@ public class MySystem1 : BaseSystem {
public string value; // used to test serialization
}

// A custom System class with all possible overrides
// A custom BaseSystem class with all possible overrides
public class MySystem2 : BaseSystem {
public bool storeAdded;
public bool storeRemoved;
public int storeAdded;
public int storeRemoved;

public override string Name => "MySystem2 - custom name";

protected override void OnAddStore (EntityStore store) {
base.OnAddStore(store);
storeAdded = true;
storeAdded++;
}

protected override void OnRemoveStore(EntityStore store) {
base.OnRemoveStore(store);
storeRemoved = true;
storeRemoved++;
}

protected override void OnUpdateGroupBegin() { }
protected override void OnUpdateGroupEnd() { }
protected override void OnUpdateGroup() { }
}

// QuerySystem class with all possible overrides
public class MyQuerySystem : QuerySystem {
public int storeAdded;
public int storeRemoved;

protected override void OnUpdate() { }

protected override void OnAddStore (EntityStore store) {
storeAdded++;
}

protected override void OnRemoveStore(EntityStore store) {
storeRemoved++;
}

protected override void OnUpdateGroupBegin() { }
protected override void OnUpdateGroupEnd() { }
protected override void OnUpdateGroup() { }
}

// SystemGroup class with all possible overrides
public class MyGroup : SystemGroup {
public int storeAdded;
public int storeRemoved;

public MyGroup() : base("MyGroup") { }

protected override void OnAddStore (EntityStore store) {
storeAdded++;
}

protected override void OnRemoveStore(EntityStore store) {
storeRemoved++;
}

protected override void OnUpdateGroupBegin() { }
protected override void OnUpdateGroupEnd() { }
Expand Down
23 changes: 18 additions & 5 deletions src/Tests/ECS/Systems/Test_SystemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,29 @@ public static void Test_SystemGroup_FindSystem()
[Test]
public static void Test_SystemGroup_OnAddRemoveStore()
{
var root = new SystemRoot("Systems");
var store = new EntityStore();
var mySystem = new MySystem2();
root.Add(mySystem);
var myQuery = new MyQuerySystem();
var myGroup = new MyGroup();
var root = new SystemRoot(store) {
mySystem,
myQuery,
myGroup
};
AreEqual(1, mySystem.storeAdded);
AreEqual(1, myQuery.storeAdded);
AreEqual(1, myGroup.storeAdded);

root.AddStore(store);
IsTrue(mySystem.storeAdded);
var store2 = new EntityStore();
root.AddStore(store2);
AreEqual(2, mySystem.storeAdded);
AreEqual(2, myQuery.storeAdded);
AreEqual(2, myGroup.storeAdded);

root.RemoveStore(store);
IsTrue(mySystem.storeRemoved);
AreEqual(1, mySystem.storeRemoved);
AreEqual(1, myQuery.storeRemoved);
AreEqual(1, myGroup.storeRemoved);
}

[Test]
Expand Down

0 comments on commit 8d5da55

Please sign in to comment.