-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComponentManager.cs
120 lines (108 loc) · 4.1 KB
/
ComponentManager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2017 Robert A. Wallis, All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ECSLight
{
public class ComponentManager : IComponentManager
{
private readonly Dictionary<IEntity, Dictionary<Type, object>> _components;
private readonly ISetManager _setManager;
private static readonly IEnumerator<object> EmptyComponents = new List<object>(0).GetEnumerator();
public ComponentManager(ISetManager setManager)
{
_components = new Dictionary<IEntity, Dictionary<Type, object>>();
_setManager = setManager;
}
/// <summary>
/// Attach a component, or replace a component, with the new component.
/// </summary>
/// <typeparam name="TComponent">Type of component to attach.</typeparam>
/// <param name="entity">IEntity to which the component should be attached.</param>
/// <param name="component">Component to attach to the entity.</param>
public void AddComponent<TComponent>(IEntity entity, TComponent component) where TComponent : class
{
var type = typeof(TComponent);
// add component to entity
if (!_components.ContainsKey(entity))
_components[entity] = new Dictionary<Type, object>(1);
_components[entity][type] = component;
_setManager.UpdateSets(entity);
}
/// <summary>
/// Check if an entity has a component.
/// </summary>
/// <typeparam name="TComponent">Type of component that may be attached to the entity.</typeparam>
/// <param name="entity">IEntity to check if component is attached.</param>
/// <returns>`true` if the entity has a component of that type attached</returns>
public bool ContainsComponent<TComponent>(IEntity entity)
{
var type = typeof(TComponent);
return ContainsComponent(entity, type);
}
/// <summary>
/// Check if an entity has a component.
/// </summary>
/// <param name="entity">IEntity to check if component is attached.</param>
/// <param name="type">Type of component that may be attached to the entity.</param>
/// <returns>`true` if the entity has a component of that type attached</returns>
public bool ContainsComponent(IEntity entity, Type type)
{
if (!_components.ContainsKey(entity))
return false;
var entityComponents = _components[entity];
return entityComponents.ContainsKey(type);
}
/// <summary>
/// Gets the component attached to the entity.
/// </summary>
/// <typeparam name="TComponent">Type of component to remove.</typeparam>
/// <param name="entity">Which entity owns the component?</param>
/// <returns>`null` if no component is attached</returns>
public TComponent ComponentFrom<TComponent>(IEntity entity) where TComponent : class
{
if (!_components.ContainsKey(entity))
return null;
if (!_components[entity].ContainsKey(typeof(TComponent)))
return null;
return _components[entity][typeof(TComponent)] as TComponent;
}
/// <summary>
/// Removes the component from the entity.
/// </summary>
/// <typeparam name="TComponent">Type of component to remove from entitiy.</typeparam>
/// <param name="entity">IEntity from which component is removed.</param>
public void RemoveComponent<TComponent>(IEntity entity) where TComponent : class
{
var type = typeof(TComponent);
RemoveComponent(entity, type);
}
/// <summary>
/// Removes the component from the entity.
/// </summary>
/// <param name="entity">IEntity from which component is removed.</param>
/// <param name="type">Type of component to remove from entitiy.</param>
public void RemoveComponent(IEntity entity, Type type)
{
if (!_components.ContainsKey(entity))
return;
var components = _components[entity];
if (!components.ContainsKey(type))
return;
var disposable = components[type] as IDisposable;
if (disposable != null)
disposable.Dispose();
components.Remove(type);
_setManager.UpdateSets(entity);
}
/// <summary>
/// Enumerate through the components in an entity.
/// </summary>
public IEnumerator<object> GetEnumerator(IEntity entity)
{
if (!_components.ContainsKey(entity))
return EmptyComponents;
return _components[entity].Values.ToList().GetEnumerator();
}
}
}