Skip to content

Commit

Permalink
Merge pull request #188 from sschmid/feature/#168-rename-group-observer
Browse files Browse the repository at this point in the history
Rename GroupObserver to EntityCollector
  • Loading branch information
sschmid authored Sep 26, 2016
2 parents 59e53dd + 4ef1bf1 commit 922cae9
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Entitas/Entitas.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Compile Include="Entitas\Interfaces\ISystem.cs" />
<Compile Include="Entitas\Pool.cs" />
<Compile Include="Entitas\Extensions\PoolExtension.cs" />
<Compile Include="Entitas\GroupObserver.cs" />
<Compile Include="Entitas\EntityCollector.cs" />
<Compile Include="Entitas\Extensions\GroupExtension.cs" />
<Compile Include="Entitas\Interfaces\IReactiveSystem.cs" />
<Compile Include="Entitas\ReactiveSystem.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

namespace Entitas {

public enum GroupEventType : byte {
OnEntityAdded,
OnEntityRemoved,
OnEntityAddedOrRemoved
}

/// A GroupObserver can observe one or more groups and collects changed entities based on the specified eventType.
public class GroupObserver {
/// An EntityCollector can observe one or more groups and collects changed entities based on the specified eventType.
public class EntityCollector {

/// Returns all collected entities. Call observer.ClearCollectedEntities() once you processed all entities.
/// Returns all collected entities. Call collector.ClearCollectedEntities() once you processed all entities.
public HashSet<Entity> collectedEntities { get { return _collectedEntities; } }

readonly HashSet<Entity> _collectedEntities;
Expand All @@ -21,19 +15,19 @@ public class GroupObserver {
Group.GroupChanged _addEntityCache;
string _toStringCache;

/// Creates a GroupObserver and will collect changed entities based on the specified eventType.
public GroupObserver(Group group, GroupEventType eventType)
/// Creates an EntityCollector and will collect changed entities based on the specified eventType.
public EntityCollector(Group group, GroupEventType eventType)
: this(new [] { group }, new [] { eventType }) {
}

/// Creates a GroupObserver and will collect changed entities based on the specified eventTypes.
public GroupObserver(Group[] groups, GroupEventType[] eventTypes) {
/// Creates an EntityCollector and will collect changed entities based on the specified eventTypes.
public EntityCollector(Group[] groups, GroupEventType[] eventTypes) {
_groups = groups;
_collectedEntities = new HashSet<Entity>(EntityEqualityComparer.comparer);
_eventTypes = eventTypes;

if(groups.Length != eventTypes.Length) {
throw new GroupObserverException("Unbalanced count with groups (" + groups.Length +
throw new EntityCollectorException("Unbalanced count with groups (" + groups.Length +
") and event types (" + eventTypes.Length + ").",
"Group and event type count must be equal.");
}
Expand All @@ -42,7 +36,7 @@ public GroupObserver(Group[] groups, GroupEventType[] eventTypes) {
Activate();
}

/// Activates the GroupObserver (GroupObserver are activated by default) and will start collecting changed entities.
/// Activates the EntityCollector (EntityCollectors are activated by default) and will start collecting changed entities.
public void Activate() {
for (int i = 0; i < _groups.Length; i++) {
var group = _groups[i];
Expand All @@ -62,7 +56,7 @@ public void Activate() {
}
}

/// Deactivates the GroupObserver (GroupObserver are activated by default).
/// Deactivates the EntityCollector (EntityCollectors are activated by default).
/// This will also clear all collected entities.
public void Deactivate() {
for (int i = 0; i < _groups.Length; i++) {
Expand Down Expand Up @@ -90,7 +84,7 @@ void addEntity(Group group, Entity entity, int index, IComponent component) {

public override string ToString() {
if(_toStringCache == null) {
var sb = new StringBuilder().Append("GroupObserver(");
var sb = new StringBuilder().Append("Collector(");

const string separator = ", ";
var lastSeparator = _groups.Length - 1;
Expand All @@ -108,13 +102,13 @@ public override string ToString() {
return _toStringCache;
}

~GroupObserver () {
~EntityCollector () {
Deactivate();
}
}

public class GroupObserverException : EntitasException {
public GroupObserverException(string message, string hint) : base(message, hint) {
public class EntityCollectorException : EntitasException {
public EntityCollectorException(string message, string hint) : base(message, hint) {
}
}
}
6 changes: 3 additions & 3 deletions Entitas/Entitas/Extensions/GroupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Entitas {

public static class GroupExtension {

/// Creates a GroupObserver for this group.
public static GroupObserver CreateObserver(this Group group, GroupEventType eventType = GroupEventType.OnEntityAdded) {
return new GroupObserver(group, eventType);
/// Creates an EntityCollector for this group.
public static EntityCollector CreateCollector(this Group group, GroupEventType eventType = GroupEventType.OnEntityAdded) {
return new EntityCollector(group, eventType);
}
}
}
40 changes: 20 additions & 20 deletions Entitas/Entitas/Extensions/PoolExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Entity[] GetEntities(this Pool pool, IMatcher matcher) {
/// This is the recommended way to create systems.
/// It will inject the pool if ISetPool is implemented.
/// It will inject the Pools.sharedInstance if ISetPools is implemented.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
public static ISystem CreateSystem(this Pool pool, ISystem system) {
return CreateSystem(pool, system, Pools.sharedInstance);
}
Expand All @@ -45,15 +45,15 @@ public static ISystem CreateSystem(this Pool pool, ISystem system, Pools pools)
/// This is the recommended way to create systems.
/// It will inject the pool if ISetPool is implemented.
/// It will inject the pools if ISetPools is implemented.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
public static ISystem CreateSystem(this Pool pool, IReactiveExecuteSystem system) {
return CreateSystem(pool, system, Pools.sharedInstance);
}

/// This is the recommended way to create systems.
/// It will inject the pool if ISetPool is implemented.
/// It will inject the pools if ISetPools is implemented.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// It will automatically create a ReactiveSystem if it is a IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
public static ISystem CreateSystem(this Pool pool, IReactiveExecuteSystem system, Pools pools) {
SetPool(system, pool);
SetPools(system, pools);
Expand All @@ -66,14 +66,14 @@ public static ISystem CreateSystem(this Pool pool, IReactiveExecuteSystem system
if(multiReactiveSystem != null) {
return new ReactiveSystem(pool, multiReactiveSystem);
}
var groupObserverSystem = system as IGroupObserverSystem;
if(groupObserverSystem != null) {
return new ReactiveSystem(groupObserverSystem);
var entityCollectorSystem = system as IEntityCollectorSystem;
if(entityCollectorSystem != null) {
return new ReactiveSystem(entityCollectorSystem);
}

throw new EntitasException(
"Could not create ReactiveSystem for " + system + "!",
"The system has to implement IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem."
"The system has to implement IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem."
);
}

Expand All @@ -86,42 +86,42 @@ public static ISystem CreateSystem(this Pools pools, ISystem system) {

/// This is the recommended way to create systems.
/// It will inject the pools if ISetPools is implemented.
/// It will automatically create a ReactiveSystem if it is a IGroupObserverSystem.
/// It will automatically create a ReactiveSystem if it is a IEntityCollectorSystem.
public static ISystem CreateSystem(this Pools pools, IReactiveExecuteSystem system) {
SetPools(system, pools);

var groupObserverSystem = system as IGroupObserverSystem;
if(groupObserverSystem != null) {
return new ReactiveSystem(groupObserverSystem);
var entityCollectorSystem = system as IEntityCollectorSystem;
if(entityCollectorSystem != null) {
return new ReactiveSystem(entityCollectorSystem);
}

throw new EntitasException(
"Could not create ReactiveSystem for " + system + "!",
"Only IGroupObserverSystem is supported for pools.CreateSystem(system)."
"Only IEntityCollectorSystem is supported for pools.CreateSystem(system)."
);
}

[Obsolete("pools.CreateSystem(system) can not infer which pool to set for ISetPool!", true)]
public static ISystem CreateSystem(this Pools pools, ISetPool system) {
throw new EntitasException(
"pools.CreateSystem(" + system + ") can not infer which pool to set for ISetPool!",
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IGroupObserverSystem."
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IEntityCollectorSystem."
);
}

[Obsolete("pools.CreateSystem(system) can not infer which pool to use to create a ReactiveSystem!", true)]
public static ISystem CreateSystem(this Pools pools, IReactiveSystem system) {
throw new EntitasException(
"pools.CreateSystem(" + system + ") can not infer which pool to use to create a ReactiveSystem!",
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IGroupObserverSystem."
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IEntityCollectorSystem."
);
}

[Obsolete("pools.CreateSystem(system) can not infer which pool to use to create a ReactiveSystem!", true)]
public static ISystem CreateSystem(this Pools pools, IMultiReactiveSystem system) {
throw new EntitasException(
"pools.CreateSystem(" + system + ") can not infer which pool to use to create a ReactiveSystem!",
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IGroupObserverSystem."
"pools.CreateSystem(system) only supports IInitializeSystem, IExecuteSystem, ICleanupSystem, ITearDownSystem and IEntityCollectorSystem."
);
}

Expand All @@ -141,10 +141,10 @@ public static void SetPools(ISystem system, Pools pools) {
}
}

/// Creates a GroupObserver which observes all specified pools.
/// This is useful when you want to create a GroupObserver for multiple pools
/// which can be used with IGroupObserverSystem.
public static GroupObserver CreateGroupObserver(this Pool[] pools, IMatcher matcher, GroupEventType eventType = GroupEventType.OnEntityAdded) {
/// Creates an EntityCollector which observes all specified pools.
/// This is useful when you want to create an EntityCollector for multiple pools
/// which can be used with IEntityCollectorSystem.
public static EntityCollector CreateEntityCollector(this Pool[] pools, IMatcher matcher, GroupEventType eventType = GroupEventType.OnEntityAdded) {
var groups = new Group[pools.Length];
var eventTypes = new GroupEventType[pools.Length];

Expand All @@ -153,7 +153,7 @@ public static GroupObserver CreateGroupObserver(this Pool[] pools, IMatcher matc
eventTypes[i] = eventType;
}

return new GroupObserver(groups, eventTypes);
return new EntityCollector(groups, eventTypes);
}

/// Creates a new entity and adds copies of all specified components to it.
Expand Down
6 changes: 6 additions & 0 deletions Entitas/Entitas/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

namespace Entitas {

public enum GroupEventType : byte {
OnEntityAdded,
OnEntityRemoved,
OnEntityAddedOrRemoved
}

/// Use pool.GetGroup(matcher) to get a group of entities which match the specified matcher.
/// Calling pool.GetGroup(matcher) with the same matcher will always return the same instance of the group.
/// The created group is managed by the pool and will always be up to date.
Expand Down
14 changes: 7 additions & 7 deletions Entitas/Entitas/Interfaces/IReactiveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ public interface IMultiReactiveSystem : IReactiveExecuteSystem {
TriggerOnEvent[] triggers { get; }
}

/// Implement this interface if you want to create a reactive system which is triggered by a GroupObserver.
/// Implement this interface if you want to create a reactive system which is triggered by an EntityCollector.
/// This is useful when you want to react to changes in multiple groups from different pools.
public interface IGroupObserverSystem : IReactiveExecuteSystem {
GroupObserver groupObserver { get; }
public interface IEntityCollectorSystem : IReactiveExecuteSystem {
EntityCollector entityCollector { get; }
}

/// Not meant to be implemented. Use IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// Not meant to be implemented. Use IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
public interface IReactiveExecuteSystem : ISystem {
void Execute(List<Entity> entities);
}

/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
/// It will ensure that all entities will match the specified matcher.
/// This is useful when a component triggered the reactive system, but once the system gets executed the component already has been removed.
/// Implementing IEnsureComponents can filter these enities.
public interface IEnsureComponents {
IMatcher ensureComponents { get; }
}

/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
/// It will exclude all entities which match the specified matcher.
/// To exclude multiple components use Matcher.AnyOf(ComponentX, ComponentY, ComponentZ).
public interface IExcludeComponents {
IMatcher excludeComponents { get; }
}

/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IGroupObserverSystem.
/// Implement this interface in combination with IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
/// If a system changes entities which in turn would trigger itself consider implementing IClearReactiveSystem
/// which will ignore the changes made by the system.
public interface IClearReactiveSystem {
Expand Down
2 changes: 1 addition & 1 deletion Entitas/Entitas/Interfaces/ISystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Entitas {

/// This is the base interface for all systems. It's not meant to be implemented.
/// Use IInitializeSystem, IExecuteSystem, IReactiveSystem, IMultiReactiveSystem, IGroupObserverSystem, ICleanupSystem or ITearDownSystem.
/// Use IInitializeSystem, IExecuteSystem, IReactiveSystem, IMultiReactiveSystem, IEntityCollectorSystem, ICleanupSystem or ITearDownSystem.
public interface ISystem {
}
}
Loading

0 comments on commit 922cae9

Please sign in to comment.