Skip to content

Instantly share code, notes, and snippets.

@bddckr
Last active February 15, 2017 19:12
Show Gist options
  • Save bddckr/5743c492e730858189bf126fc779c3aa to your computer and use it in GitHub Desktop.
Save bddckr/5743c492e730858189bf126fc779c3aa to your computer and use it in GitHub Desktop.
A system to destroy entities in Entitas, for all pools

How to use

Make sure that your DestroyComponent is added to all the pools you want it to be added to. Then add the system using pools.CreateSystem(new DestroySystem()), where pools is an instance of Pools which has all the systems you added the component to.

using System;
using System.Collections.Generic;
using System.Linq;
using Entitas;
public sealed class DestroySystem : ISetPools, IGroupObserverSystem
{
public GroupObserver groupObserver
{
get
{
var poolsCount = _allPools.Length;
var groups = new Group[poolsCount];
var eventTypes = new GroupEventType[poolsCount];
for (var poolIndex = 0; poolIndex < poolsCount; poolIndex++)
{
var pool = _allPools[poolIndex];
var componentIndex = Array.IndexOf(pool.metaData.componentTypes, typeof(DestroyComponent));
if (componentIndex == -1)
{
continue;
}
groups[poolIndex] = pool.GetGroup(Matcher.AllOf(componentIndex));
eventTypes[poolIndex] = GroupEventType.OnEntityAdded;
}
return new GroupObserver(groups, eventTypes);
}
}
private Pool[] _allPools;
public void SetPools(Pools pools)
{
_allPools = pools.allPools;
}
public void Execute(List<Entity> entities)
{
foreach (var entity in entities)
{
_allPools.First(pool => pool.HasEntity(entity)).DestroyEntity(entity);
}
}
}
@bddckr
Copy link
Author

bddckr commented Feb 15, 2017

For Entitas 0.37.0(+) see this gist.

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