-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntitySystem.cs
49 lines (39 loc) · 1.59 KB
/
EntitySystem.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ECS
{
public abstract class EntitySystem
{
public EntityManager EntityManager { get; set; }
public List<Entity> Compatible { get; private set; }
protected List<Type> CompatibleTypes { get; private set; }
public EntitySystem(EntityManager manager, params Type[] compatibleTypes)
{
if (compatibleTypes.Any(t => t.IsAssignableFrom(typeof(IComponent))))
{
throw new Exception();
//not a component interface
}
CompatibleTypes = new List<Type>();
CompatibleTypes.AddRange(compatibleTypes);
EntityManager = manager;
EventManager eManager = EventManager.getInstance();
Compatible = GetCompatibleInManager();
eManager.registerListener(ECSEventTypes.EntityAdded, OnManagerEntityChanged);
eManager.registerListener(ECSEventTypes.EntityRemoved, OnManagerEntityChanged);
eManager.registerListener(ECSEventTypes.EntityComponentAdded, OnManagerEntityChanged);
eManager.registerListener(ECSEventTypes.EntityComponentRemoved, OnManagerEntityChanged);
}
private void OnManagerEntityChanged(GameEvent e)
{
Compatible = GetCompatibleInManager();
}
protected virtual List<Entity> GetCompatibleInManager()
{
return EntityManager.Entities.Where(ent => ent.hasComponents(CompatibleTypes)).ToList();
}
}
}