blob: 2c951072f7810ac3afce08c12a417f684927f031 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using MonoGame.Extended.Collections;
namespace MonoGame.Extended.Entities.Systems
{
public abstract class EntitySystem : ISystem
{
protected EntitySystem(AspectBuilder aspectBuilder)
{
_aspectBuilder = aspectBuilder;
}
public void Dispose()
{
if (_world != null)
{
_world.EntityManager.EntityAdded -= OnEntityAdded;
_world.EntityManager.EntityRemoved -= OnEntityRemoved;
}
}
private readonly AspectBuilder _aspectBuilder;
private EntitySubscription _subscription;
private World _world;
protected virtual void OnEntityChanged(int entityId) { }
protected virtual void OnEntityAdded(int entityId) { }
protected virtual void OnEntityRemoved(int entityId) { }
public Bag<int> ActiveEntities => _subscription.ActiveEntities;
public virtual void Initialize(World world)
{
_world = world;
var aspect = _aspectBuilder.Build(_world.ComponentManager);
_subscription = new EntitySubscription(_world.EntityManager, aspect);
_world.EntityManager.EntityAdded += OnEntityAdded;
_world.EntityManager.EntityRemoved += OnEntityRemoved;
_world.EntityManager.EntityChanged += OnEntityChanged;
Initialize(world.ComponentManager);
}
public abstract void Initialize(IComponentMapperService mapperService);
protected void DestroyEntity(int entityId) => _world.DestroyEntity(entityId);
protected Entity CreateEntity() => _world.CreateEntity();
protected Entity GetEntity(int entityId) => _world.GetEntity(entityId);
}
}
|