Created
February 22, 2017 14:40
-
-
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(+))
This file contains 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