Last active
July 14, 2021 07:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
For the implementations of the methods
IndicesOfComponentTypes
andIndexOfComponentType
onIContext
see this other gist.