Skip to content

Instantly share code, notes, and snippets.

@bddckr
Last active July 14, 2021 07:55
Show Gist options
  • Save bddckr/4e5035998618e39040909be1f0e9c55d to your computer and use it in GitHub Desktop.
Save bddckr/4e5035998618e39040909be1f0e9c55d to your computer and use it in GitHub Desktop.
A small example on how to use the same kind of system for multiple contexts in Entitas 0.37.0(+) while making usage of a shared component.
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();
}
}
}
@bddckr
Copy link
Author

bddckr commented Feb 22, 2017

For the implementations of the methods IndicesOfComponentTypes and IndexOfComponentType on IContext see this other gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment