Entity.cs 4.2 KB
Newer Older
1 2 3
using System;
using System.Collections.Generic;
using System.Linq;
4
using MongoDB.Bson.Serialization.Attributes;
T
tanghai 已提交
5

6
namespace Model
7
{
T
tanghai 已提交
8
	[BsonIgnoreExtraElements]
9
	public partial class Entity : Disposer
10
	{
11 12
		[BsonIgnore]
		public Entity Parent { get; set; }
13

14
		[BsonElement]
15
		[BsonIgnoreIfNull]
16
		private HashSet<Component> components = new HashSet<Component>();
17

18
		[BsonIgnore]
19
		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
20

21
		protected Entity()
22
		{
T
tanghai 已提交
23
			this.Id = IdGenerater.GenerateId();
24 25
		}

T
tanghai 已提交
26
		protected Entity(long id)
27
		{
T
tanghai 已提交
28
			this.Id = id;
29
		}
T
tanghai 已提交
30

31 32 33 34 35 36
		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
			}
37

38 39
			base.Dispose();

40
			foreach (Component component in this.GetComponents())
41 42 43 44 45 46 47 48 49 50
			{
				try
				{
					component.Dispose();
				}
				catch (Exception e)
				{
					Log.Error(e.ToString());
				}
			}
51 52 53

			this.components.Clear();
			this.componentDict.Clear();
54 55
		}

56
		public K AddComponent<K>() where K : Component, new()
57
		{
58 59
			K component = ComponentFactory.Create<K>(this);

60 61
			if (this.componentDict.ContainsKey(component.GetType()))
			{
62
				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
63
			}
64

65
			if (component is ISerializeToEntity)
66 67 68
			{
				this.components.Add(component);
			}
69 70 71 72
			this.componentDict.Add(component.GetType(), component);
			return component;
		}

73
		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
74
		{
75
			K component = ComponentFactory.Create<K, P1>(this, p1);
76 77 78

			if (this.componentDict.ContainsKey(component.GetType()))
			{
79
				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
80
			}
81
			
82
			if (component is ISerializeToEntity)
83 84 85
			{
				this.components.Add(component);
			}
86 87 88 89
			this.componentDict.Add(component.GetType(), component);
			return component;
		}

90
		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
91
		{
92
			K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
93 94 95

			if (this.componentDict.ContainsKey(component.GetType()))
			{
96
				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
97
			}
98
			
99
			if (component is ISerializeToEntity)
100 101 102
			{
				this.components.Add(component);
			}
103 104 105 106
			this.componentDict.Add(component.GetType(), component);
			return component;
		}

107
		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
108
		{
109
			K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
110 111 112

			if (this.componentDict.ContainsKey(component.GetType()))
			{
113
				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
114 115
			}

116
			if (component is ISerializeToEntity)
117 118 119
			{
				this.components.Add(component);
			}
120 121 122 123
			this.componentDict.Add(component.GetType(), component);
			return component;
		}

124
		public void RemoveComponent<K>() where K : Component
125
		{
126
			Component component;
127
			if (!this.componentDict.TryGetValue(typeof(K), out component))
128 129 130 131
			{
				return;
			}

132
			this.components.Remove(component);
133
			this.componentDict.Remove(typeof(K));
134

135 136 137
			component.Dispose();
		}

T
tanghai 已提交
138 139
		public void RemoveComponent(Type type)
		{
140
			Component component;
T
tanghai 已提交
141 142 143 144 145
			if (!this.componentDict.TryGetValue(type, out component))
			{
				return;
			}

146
			this.components?.Remove(component);
T
tanghai 已提交
147
			this.componentDict.Remove(type);
148

T
tanghai 已提交
149 150 151
			component.Dispose();
		}

152
		public K GetComponent<K>() where K : Component
153
		{
154
			Component component;
155
			if (!this.componentDict.TryGetValue(typeof(K), out component))
156 157 158
			{
				return default(K);
			}
159
			return (K)component;
160 161
		}

162
		public Component[] GetComponents()
163
		{
164 165 166
			return this.componentDict.Values.ToArray();
		}

167
		public override void BeginInit()
168 169 170
		{
			this.components = new HashSet<Component>();
			this.componentDict = new Dictionary<Type, Component>();
171
		}
T
tanghai 已提交
172

173
		public override void EndInit()
T
tanghai 已提交
174
		{
175
			try
T
tanghai 已提交
176
			{
177
				this.componentDict.Clear();
178

179 180 181 182 183 184 185 186 187 188 189 190
				if (this.components != null)
				{
					foreach (Component component in this.components)
					{
						component.Entity = this;
						this.componentDict.Add(component.GetType(), component);
					}
				}
			}
			catch (Exception e)
			{
				Log.Error(e.ToString());
T
tanghai 已提交
191 192
			}
		}
193 194
	}
}