|
|
@@ -5,50 +5,56 @@ using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
|
namespace Common.Base
|
|
|
{
|
|
|
- public abstract class Entity : AMongo
|
|
|
+ public abstract class Entity : Object
|
|
|
{
|
|
|
- [BsonElement]
|
|
|
- private HashSet<Component> Components { get; set; }
|
|
|
+ [BsonElement, BsonIgnoreIfNull]
|
|
|
+ private HashSet<Component> components;
|
|
|
|
|
|
- private Dictionary<Type, Component> ComponentDict { get; set; }
|
|
|
+ private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
|
|
|
|
|
|
- protected Entity()
|
|
|
+ public T AddComponent<T>() where T : Component, new()
|
|
|
{
|
|
|
- this.Components = new HashSet<Component>();
|
|
|
- this.ComponentDict = new Dictionary<Type, Component>();
|
|
|
- }
|
|
|
-
|
|
|
- public void AddComponent<T>() where T : Component, new()
|
|
|
- {
|
|
|
- if (this.ComponentDict.ContainsKey(typeof (T)))
|
|
|
+ if (this.componentDict.ContainsKey(typeof (T)))
|
|
|
{
|
|
|
throw new Exception(
|
|
|
string.Format("AddComponent, component already exist, id: {0}, component: {1}",
|
|
|
this.Id, typeof(T).Name));
|
|
|
}
|
|
|
+
|
|
|
+ if (this.components == null)
|
|
|
+ {
|
|
|
+ this.components = new HashSet<Component>();
|
|
|
+ }
|
|
|
+
|
|
|
T t = new T { Owner = this };
|
|
|
- this.Components.Add(t);
|
|
|
- this.ComponentDict.Add(typeof (T), t);
|
|
|
+ this.components.Add(t);
|
|
|
+ this.componentDict.Add(typeof (T), t);
|
|
|
+ return t;
|
|
|
}
|
|
|
|
|
|
public void RemoveComponent<T>() where T : Component
|
|
|
{
|
|
|
Component t;
|
|
|
- if (!this.ComponentDict.TryGetValue(typeof (T), out t))
|
|
|
+ if (!this.componentDict.TryGetValue(typeof (T), out t))
|
|
|
{
|
|
|
throw new Exception(
|
|
|
string.Format("RemoveComponent, component not exist, id: {0}, component: {1}",
|
|
|
this.Id, typeof(T).Name));
|
|
|
}
|
|
|
|
|
|
- this.Components.Remove(t);
|
|
|
- this.ComponentDict.Remove(typeof(T));
|
|
|
+ this.components.Remove(t);
|
|
|
+ this.componentDict.Remove(typeof(T));
|
|
|
+
|
|
|
+ if (this.components.Count == 0)
|
|
|
+ {
|
|
|
+ this.components = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public T GetComponent<T>() where T : Component
|
|
|
{
|
|
|
Component t;
|
|
|
- if (!this.ComponentDict.TryGetValue(typeof (T), out t))
|
|
|
+ if (!this.componentDict.TryGetValue(typeof (T), out t))
|
|
|
{
|
|
|
throw new Exception(
|
|
|
string.Format("GetComponent, component not exist, id: {0}, component: {1}",
|
|
|
@@ -59,15 +65,28 @@ namespace Common.Base
|
|
|
|
|
|
public Component[] GetComponents()
|
|
|
{
|
|
|
- return this.Components.ToArray();
|
|
|
+ return this.components.ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void BeginInit()
|
|
|
+ {
|
|
|
+ base.BeginInit();
|
|
|
+ this.components = new HashSet<Component>();
|
|
|
+ this.componentDict = new Dictionary<Type, Component>();
|
|
|
}
|
|
|
|
|
|
public override void EndInit()
|
|
|
{
|
|
|
- foreach (Component component in this.Components)
|
|
|
+ base.EndInit();
|
|
|
+ if (this.components.Count == 0)
|
|
|
+ {
|
|
|
+ this.components = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ foreach (Component component in this.components)
|
|
|
{
|
|
|
component.Owner = this;
|
|
|
- this.ComponentDict.Add(component.GetType(), component);
|
|
|
+ this.componentDict.Add(component.GetType(), component);
|
|
|
}
|
|
|
}
|
|
|
}
|