Entity.cs 4.8 KB
Newer Older
1 2 3 4 5 6
using System;
using System.Collections.Generic;
using System.Linq;
using Base;
using MongoDB.Bson.Serialization.Attributes;

7
namespace Base
8
{
T
tanghai 已提交
9
	public abstract class Entity: Object
10 11
	{
		[BsonElement, BsonIgnoreIfNull]
T
tanghai 已提交
12 13 14
		private HashSet<Component> components = new HashSet<Component>();
		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
		
15 16
		protected Entity()
		{
17 18 19
			ObjectManager.Add(this);
		}

T
tanghai 已提交
20
		protected Entity(long id): base(id)
21
		{
22 23 24
			ObjectManager.Add(this);
		}

T
tanghai 已提交
25
		public T Clone<T>() where T: Entity
26 27 28 29 30 31 32 33 34 35 36 37 38
		{
			return MongoHelper.FromBson<T>(MongoHelper.ToBson(this));
		}

		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
			}

			base.Dispose();

T
tanghai 已提交
39
			foreach (Component component in this.GetComponents())
40 41 42 43 44 45 46 47 48 49 50 51 52
			{
				try
				{
					component.Dispose();
				}
				catch (Exception e)
				{
					Log.Error(e.ToString());
				}
			}
			ObjectManager.Remove(this.Id);
		}

T
tanghai 已提交
53
		public K AddComponent<K>() where K : Component, new()
54 55
		{
			K component = (K) Activator.CreateInstance(typeof (K));
T
tanghai 已提交
56
			component.SetOwner(this);
57 58 59 60 61 62 63 64

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

			if (this.components == null)
			{
T
tanghai 已提交
65
				this.components = new HashSet<Component>();
66 67 68 69 70 71 72 73
			}

			this.components.Add(component);
			this.componentDict.Add(component.GetType(), component);
			ObjectManager.Awake(component.Id);
			return component;
		}

T
tanghai 已提交
74
		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
75 76
		{
			K component = (K)Activator.CreateInstance(typeof(K));
T
tanghai 已提交
77
			component.SetOwner(this);
78 79 80 81 82 83 84 85

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

			if (this.components == null)
			{
T
tanghai 已提交
86
				this.components = new HashSet<Component>();
87 88 89 90 91 92 93 94
			}

			this.components.Add(component);
			this.componentDict.Add(component.GetType(), component);
			ObjectManager.Awake(component.Id, p1);
			return component;
		}

T
tanghai 已提交
95
		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
96 97
		{
			K component = (K)Activator.CreateInstance(typeof(K));
T
tanghai 已提交
98
			component.SetOwner(this);
99 100 101 102 103 104 105 106

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

			if (this.components == null)
			{
T
tanghai 已提交
107
				this.components = new HashSet<Component>();
108 109 110 111 112 113 114 115 116
			}

			this.components.Add(component);
			this.componentDict.Add(component.GetType(), component);
			ObjectManager.Awake(component.Id, p1, p2);
			return component;
		}


T
tanghai 已提交
117
		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
118 119
		{
			K component = (K)Activator.CreateInstance(typeof(K));
T
tanghai 已提交
120
			component.SetOwner(this);
121 122 123 124 125 126 127 128

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

			if (this.components == null)
			{
T
tanghai 已提交
129
				this.components = new HashSet<Component>();
130 131 132 133 134 135 136 137
			}

			this.components.Add(component);
			this.componentDict.Add(component.GetType(), component);
			ObjectManager.Awake(component.Id, p1, p2, p3);
			return component;
		}

T
tanghai 已提交
138
		public void AddComponent(Component component)
139 140 141 142 143 144 145 146
		{
			if (this.componentDict.ContainsKey(component.GetType()))
			{
				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
			}

			if (this.components == null)
			{
T
tanghai 已提交
147
				this.components = new HashSet<Component>();
148 149 150 151 152 153
			}
			this.components.Add(component);
			this.componentDict.Add(component.GetType(), component);
			ObjectManager.Awake(component.Id);
		}

T
tanghai 已提交
154
		public void RemoveComponent<K>() where K : Component
155
		{
T
tanghai 已提交
156
			Component component;
157 158 159 160 161 162 163 164 165 166 167 168 169 170
			if (!this.componentDict.TryGetValue(typeof (K), out component))
			{
				return;
			}

			this.components.Remove(component);
			this.componentDict.Remove(typeof (K));
			if (this.components.Count == 0)
			{
				this.components = null;
			}
			component.Dispose();
		}

T
tanghai 已提交
171
		public K GetComponent<K>() where K : Component
172
		{
T
tanghai 已提交
173
			Component component;
174 175 176 177 178 179 180
			if (!this.componentDict.TryGetValue(typeof (K), out component))
			{
				return default(K);
			}
			return (K) component;
		}

T
tanghai 已提交
181
		public Component[] GetComponents()
182 183 184 185 186 187 188
		{
			return components.ToArray();
		}

		public override void BeginInit()
		{
			base.BeginInit();
T
tanghai 已提交
189 190
			this.components = new HashSet<Component>();
			this.componentDict = new Dictionary<Type, Component>();
191 192 193 194 195 196 197 198 199 200
		}

		public override void EndInit()
		{
			base.EndInit();
			if (this.components.Count == 0)
			{
				this.components = null;
				return;
			}
T
tanghai 已提交
201
			foreach (Component component in this.components)
202
			{
T
tanghai 已提交
203
				component.SetOwner(this);
204 205 206 207 208
				this.componentDict.Add(component.GetType(), component);
			}
		}
	}
}