Skip to content

Instantly share code, notes, and snippets.

@bddckr
Created February 22, 2017 14:40
Show Gist options
  • Save bddckr/b2aeab1471beadab6336fd8f1d8a4d6b to your computer and use it in GitHub Desktop.
Save bddckr/b2aeab1471beadab6336fd8f1d8a4d6b to your computer and use it in GitHub Desktop.
Example of a generic system for multiple typed entities. (Entitas 0.37.0(+))
using System.Collections.Generic;
using Components.GameObjects;
using Components.Miscellaneous;
using Entitas;
using Entitas.Unity.VisualDebugging;
using EntitasHelpers;
using JetBrains.Annotations;
public sealed class CleanUpGameObjectSystem<TEntity> : ReactiveSystem<TEntity> where TEntity : class, IEntity, new()
{
private readonly Context<TEntity> _context;
private IMatcher<TEntity> _triggerMatcher;
public CleanUpGameObjectSystem([NotNull] Context<TEntity> context) : base(context)
{
_context = context;
}
protected override Collector<TEntity> GetTrigger(IContext<TEntity> context)
{
_triggerMatcher = Matcher<TEntity>.AllOf(context.IndicesOfComponentTypes(new[]
{
typeof(DestroyRequestedComponent),
typeof(GameObjectComponent)
}));
return context.CreateCollector(_triggerMatcher);
}
protected override bool Filter(TEntity entity) => _triggerMatcher.Matches(entity);
protected override void Execute(List<TEntity> entities)
{
foreach (var entity in entities)
{
var gameObjectComponent = (GameObjectComponent)entity.GetComponent(_context.IndexOfComponentType<TEntity, GameObjectComponent>());
gameObjectComponent.Value.DestroyGameObject();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment