Skip to content

Commit

Permalink
expanding ListComponentSystemGroup to incorporate the ComponentSystem…
Browse files Browse the repository at this point in the history
…Group add/remove API
  • Loading branch information
tbg10101 committed May 2, 2020
1 parent a48edb0 commit 157651e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
8 changes: 3 additions & 5 deletions Runtime/Scripts/MonoBehaviours/WorldBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ private void AddSystemToGroup(ComponentSystemGroup group, ComponentSystemBase sy
switch (group) {
case IList<ComponentSystemBase> lcsg:
lcsg.Add(system);
break;
case ComponentSystemGroup csg:
csg.AddSystemToUpdateList(system);
break;
return;
default:
throw new Exception("Group must be compatible.");
group.AddSystemToUpdateList(system);
return;
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions Runtime/Scripts/Systems/Groups/ListComponentSystemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using Unity.Entities;
using UnityEngine;

namespace Software10101.DOTS.Systems.Groups {
/// <summary>
/// Component system group that acts like a List. Not compatible with unmanaged systems.
/// </summary>
public abstract class ListComponentSystemGroup :
ComponentSystemGroup,
IList<ComponentSystemBase>,
IReadOnlyList<ComponentSystemBase>,
IList {

private List<ComponentSystemBase> _underlyingSystemsToUpdate;
private static readonly FieldInfo UnderlyingSystemsToUpdateField = typeof(ComponentSystemGroup).GetField(
"m_systemsToUpdate",
BindingFlags.Instance | BindingFlags.NonPublic);

private List<ComponentSystemBase> _underlyingSystemsToRemove;
private static readonly FieldInfo UnderlyingSystemsToRemoveField = typeof(ComponentSystemGroup).GetField(
"m_systemsToRemove",
BindingFlags.Instance | BindingFlags.NonPublic);

private ComponentSystemBase[] _systems = new ComponentSystemBase[0];
private readonly List<ComponentSystemBase> _mutableSystemsList = new List<ComponentSystemBase>();
private bool _systemsListDirtyFlag = false;
Expand All @@ -24,12 +38,39 @@ public abstract class ListComponentSystemGroup :

public override IEnumerable<ComponentSystemBase> Systems => _systems;

protected override void OnCreate() {
_underlyingSystemsToUpdate = (List<ComponentSystemBase>)UnderlyingSystemsToUpdateField.GetValue(this);
_underlyingSystemsToRemove = (List<ComponentSystemBase>)UnderlyingSystemsToRemoveField.GetValue(this);
}

protected override void OnUpdate() {
if (_underlyingSystemsToUpdate.Count > 0) {
_mutableSystemsList.AddRange(_underlyingSystemsToUpdate);
_underlyingSystemsToUpdate.Clear();
_systemsListDirtyFlag = true;
}

if (_underlyingSystemsToRemove.Count > 0) {
_underlyingSystemsToRemove.ForEach(system => _mutableSystemsList.Remove(system));
_underlyingSystemsToRemove.Clear();
_systemsListDirtyFlag = true;
}

if (_systemsListDirtyFlag) {
_systemsListDirtyFlag = false;
_systems = _mutableSystemsList.ToArray();
}

if (UpdateCallback == null) {
UpdateAllSystems();
} else {
while (UpdateCallback(this)) {
UpdateAllSystems();
}
}
}

private void UpdateAllSystems() {
int count = _systems.Length;
int index;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.10101software.dots.hybridsimulation",
"description": "A framework for using FixedUpdate in a simulation world which is linked to a GameObject-based presentation layer.",
"version": "0.4.0",
"version": "0.4.1",
"unity": "2020.1",
"displayName": "DOTS Hybrid Simulation Worlds",
"dependencies": {
Expand Down

0 comments on commit 157651e

Please sign in to comment.