-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityManager.cs
68 lines (60 loc) · 1.54 KB
/
EntityManager.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2017 Robert A. Wallis, All Rights Reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ECSLight
{
public class EntityManager : IEntityManager
{
private readonly ICollection<IEntity> _entities;
private readonly IComponentManager _componentManager;
public EntityManager(ICollection<IEntity> entities, IComponentManager componentManager)
{
_entities = entities;
_componentManager = componentManager;
}
/// <summary>
/// Make a new entity, or recycle an unused entity.
/// </summary>
/// <returns>new empty entity</returns>
public IEntity CreateEntity(string name = "")
{
var entity = new Entity(this, _componentManager, name);
_entities.Add(entity);
return entity;
}
/// <summary>
/// Release the entity back to be reused later.
/// </summary>
/// <param name="entity">IEntity to be released.</param>
public void ReleaseEntity(IEntity entity)
{
var types = new List<Type>();
foreach (var component in entity) {
types.Add(component.GetType());
}
foreach (var type in types) {
_componentManager.RemoveComponent(entity, type);
}
_entities.Remove(entity);
}
/// <summary>
/// Release all the entities.
/// </summary>
public void ReleaseAll()
{
foreach (var entity in _entities.ToList()) {
ReleaseEntity(entity);
}
}
public IEnumerator<IEntity> GetEnumerator()
{
return _entities.ToList().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}