-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cs
106 lines (83 loc) · 3.37 KB
/
Entity.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
#region
using System;
using System.Collections.Generic;
using SFMLStart.Utilities;
#endregion
namespace VeeEntity
{
public class Entity
{
private static int _lastUID = -1;
private readonly Dictionary<Type, Component> _componentDictionary;
private readonly List<Component> _components;
private readonly Repository _repository;
private readonly HashSet<string> _tags;
private int _uID;
public Entity(Manager mManager)
{
_lastUID++;
_uID = _lastUID;
_components = new List<Component>();
_componentDictionary = new Dictionary<Type, Component>();
_tags = new HashSet<string>();
Manager = mManager;
_repository = Manager.Repository;
_repository.AddEntity(this);
}
public Manager Manager { get; private set; }
public Action OnDestroy { get; set; }
public bool IsDead { get; private set; }
private void AddTag(string mTag)
{
_tags.Add(mTag);
_repository.TagAdded(this, mTag);
}
private void RemoveTag(string mTag)
{
_tags.Remove(mTag);
_repository.TagRemoved(this, mTag);
}
private void AddComponent(Component mComponent)
{
mComponent.Entity = this;
var componentType = mComponent.GetType();
_components.Add(mComponent);
if (!_componentDictionary.ContainsKey(componentType)) _componentDictionary.Add(componentType, mComponent);
else _componentDictionary[componentType] = mComponent;
_repository.ComponentAdded(this, mComponent);
mComponent.Added();
}
internal void RemoveComponent(Component mComponent)
{
var componentType = mComponent.GetType();
_components.Remove(mComponent);
_componentDictionary.Remove(componentType);
_repository.ComponentRemoved(this, mComponent);
mComponent.Removed();
}
public IEnumerable<string> GetTags() { return _tags; }
public IEnumerable<Component> GetComponents() { return _components; }
public void AddTags(params string[] mTags) { foreach (var tag in mTags) AddTag(tag); }
public void RemoveTags(params string[] mTags) { foreach (var tag in mTags) RemoveTag(tag); }
public bool HasTag(string mTag) { return _tags.Contains(mTag); }
public void AddComponents(params Component[] mComponents) { foreach (var component in mComponents) AddComponent(component); }
public T GetComponent<T>() where T : Component { return _componentDictionary.ContainsKey(typeof (T)) ? (T) _componentDictionary[typeof (T)] : null; }
public T GetComponentUnSafe<T>() where T : Component { return (T) _componentDictionary[typeof (T)]; }
public void Update(float mFrameTime)
{
if (IsDead) return;
foreach (var component in new List<Component>(_components))
{
if (IsDead) return;
component.Update(mFrameTime);
}
}
public void Destroy()
{
IsDead = true;
OnDestroy.SafeInvoke();
_repository.RemoveEntity(this);
foreach (var component in new List<Component>(_components)) component.Destroy();
}
}
}