|
@@ -5,19 +5,25 @@ using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
|
|
|
namespace Model
|
|
namespace Model
|
|
|
{
|
|
{
|
|
|
- public class Entity: Disposer
|
|
|
|
|
|
|
+ public class Entity : Disposer
|
|
|
{
|
|
{
|
|
|
|
|
+ [BsonIgnore]
|
|
|
|
|
+ public Entity Parent { get; set; }
|
|
|
|
|
+
|
|
|
|
|
+ public EntityType Type { get; set; }
|
|
|
|
|
+
|
|
|
[BsonElement]
|
|
[BsonElement]
|
|
|
|
|
+ [BsonIgnoreIfNull]
|
|
|
private HashSet<Component> components = new HashSet<Component>();
|
|
private HashSet<Component> components = new HashSet<Component>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
[BsonIgnore]
|
|
[BsonIgnore]
|
|
|
- private readonly Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
|
|
|
|
|
|
|
+ private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
|
|
|
|
|
|
|
|
protected Entity()
|
|
protected Entity()
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected Entity(long id): base(id)
|
|
|
|
|
|
|
+ protected Entity(long id) : base(id)
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,31 +51,33 @@ namespace Model
|
|
|
|
|
|
|
|
public K AddComponent<K>() where K : Component, new()
|
|
public K AddComponent<K>() where K : Component, new()
|
|
|
{
|
|
{
|
|
|
- K component = (K) Activator.CreateInstance(typeof (K));
|
|
|
|
|
- component.Owner = this;
|
|
|
|
|
|
|
+ K component = ComponentFactory.Create<K>(this);
|
|
|
|
|
+
|
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
if (this.components == null)
|
|
if (this.components == null)
|
|
|
{
|
|
{
|
|
|
this.components = new HashSet<Component>();
|
|
this.components = new HashSet<Component>();
|
|
|
}
|
|
}
|
|
|
- this.components.Add(component);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (component is ComponentDB)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.components.Add(component);
|
|
|
|
|
+ }
|
|
|
this.componentDict.Add(component.GetType(), component);
|
|
this.componentDict.Add(component.GetType(), component);
|
|
|
- IAwake awake = component as IAwake;
|
|
|
|
|
- awake?.Awake();
|
|
|
|
|
return component;
|
|
return component;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public K AddComponent<K, P1>(P1 p1) where K : Component, new()
|
|
public K AddComponent<K, P1>(P1 p1) where K : Component, new()
|
|
|
{
|
|
{
|
|
|
- K component = (K) Activator.CreateInstance(typeof (K));
|
|
|
|
|
- component.Owner = this;
|
|
|
|
|
|
|
+ K component = ComponentFactory.Create<K, P1>(this, p1);
|
|
|
|
|
|
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (this.components == null)
|
|
if (this.components == null)
|
|
@@ -77,21 +85,21 @@ namespace Model
|
|
|
this.components = new HashSet<Component>();
|
|
this.components = new HashSet<Component>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.components.Add(component);
|
|
|
|
|
|
|
+ if (component is ComponentDB)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.components.Add(component);
|
|
|
|
|
+ }
|
|
|
this.componentDict.Add(component.GetType(), component);
|
|
this.componentDict.Add(component.GetType(), component);
|
|
|
- IAwake<P1> awake = component as IAwake<P1>;
|
|
|
|
|
- awake?.Awake(p1);
|
|
|
|
|
return component;
|
|
return component;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
|
|
public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
|
|
|
{
|
|
{
|
|
|
- K component = (K) Activator.CreateInstance(typeof (K));
|
|
|
|
|
- component.Owner = this;
|
|
|
|
|
|
|
+ K component = ComponentFactory.Create<K, P1, P2>(this, p1, p2);
|
|
|
|
|
|
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (this.components == null)
|
|
if (this.components == null)
|
|
@@ -99,21 +107,21 @@ namespace Model
|
|
|
this.components = new HashSet<Component>();
|
|
this.components = new HashSet<Component>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.components.Add(component);
|
|
|
|
|
|
|
+ if (component is ComponentDB)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.components.Add(component);
|
|
|
|
|
+ }
|
|
|
this.componentDict.Add(component.GetType(), component);
|
|
this.componentDict.Add(component.GetType(), component);
|
|
|
- IAwake<P1, P2> awake = component as IAwake<P1, P2>;
|
|
|
|
|
- awake?.Awake(p1, p2);
|
|
|
|
|
return component;
|
|
return component;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
|
|
public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
|
|
|
{
|
|
{
|
|
|
- K component = (K) Activator.CreateInstance(typeof (K));
|
|
|
|
|
- component.Owner = this;
|
|
|
|
|
|
|
+ K component = ComponentFactory.Create<K, P1, P2, P3>(this, p1, p2, p3);
|
|
|
|
|
|
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
if (this.componentDict.ContainsKey(component.GetType()))
|
|
|
{
|
|
{
|
|
|
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
|
|
|
|
|
|
|
+ throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (this.components == null)
|
|
if (this.components == null)
|
|
@@ -121,24 +129,25 @@ namespace Model
|
|
|
this.components = new HashSet<Component>();
|
|
this.components = new HashSet<Component>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.components.Add(component);
|
|
|
|
|
|
|
+ if (component is ComponentDB)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.components.Add(component);
|
|
|
|
|
+ }
|
|
|
this.componentDict.Add(component.GetType(), component);
|
|
this.componentDict.Add(component.GetType(), component);
|
|
|
- IAwake<P1, P2, P3> awake = component as IAwake<P1, P2, P3>;
|
|
|
|
|
- awake?.Awake(p1, p2, p3);
|
|
|
|
|
return component;
|
|
return component;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void RemoveComponent<K>() where K : Component
|
|
public void RemoveComponent<K>() where K : Component
|
|
|
{
|
|
{
|
|
|
Component component;
|
|
Component component;
|
|
|
- if (!this.componentDict.TryGetValue(typeof (K), out component))
|
|
|
|
|
|
|
+ if (!this.componentDict.TryGetValue(typeof(K), out component))
|
|
|
{
|
|
{
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.components.Remove(component);
|
|
|
|
|
- this.componentDict.Remove(typeof (K));
|
|
|
|
|
- if (this.components.Count == 0)
|
|
|
|
|
|
|
+ this.components?.Remove(component);
|
|
|
|
|
+ this.componentDict.Remove(typeof(K));
|
|
|
|
|
+ if (this.components != null && this.components.Count == 0)
|
|
|
{
|
|
{
|
|
|
this.components = null;
|
|
this.components = null;
|
|
|
}
|
|
}
|
|
@@ -153,9 +162,9 @@ namespace Model
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.components.Remove(component);
|
|
|
|
|
|
|
+ this.components?.Remove(component);
|
|
|
this.componentDict.Remove(type);
|
|
this.componentDict.Remove(type);
|
|
|
- if (this.components.Count == 0)
|
|
|
|
|
|
|
+ if (this.components != null && this.components.Count == 0)
|
|
|
{
|
|
{
|
|
|
this.components = null;
|
|
this.components = null;
|
|
|
}
|
|
}
|
|
@@ -165,24 +174,38 @@ namespace Model
|
|
|
public K GetComponent<K>() where K : Component
|
|
public K GetComponent<K>() where K : Component
|
|
|
{
|
|
{
|
|
|
Component component;
|
|
Component component;
|
|
|
- if (!this.componentDict.TryGetValue(typeof (K), out component))
|
|
|
|
|
|
|
+ if (!this.componentDict.TryGetValue(typeof(K), out component))
|
|
|
{
|
|
{
|
|
|
return default(K);
|
|
return default(K);
|
|
|
}
|
|
}
|
|
|
- return (K) component;
|
|
|
|
|
|
|
+ return (K)component;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Component[] GetComponents()
|
|
public Component[] GetComponents()
|
|
|
{
|
|
{
|
|
|
- return components.ToArray();
|
|
|
|
|
|
|
+ return this.componentDict.Values.ToArray();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public override void BeginInit()
|
|
|
|
|
+ {
|
|
|
|
|
+ base.BeginInit();
|
|
|
|
|
+ this.components = new HashSet<Component>();
|
|
|
|
|
+ this.componentDict = new Dictionary<Type, Component>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public override void EndInit()
|
|
public override void EndInit()
|
|
|
{
|
|
{
|
|
|
base.EndInit();
|
|
base.EndInit();
|
|
|
|
|
|
|
|
|
|
+ ObjectEvents.Instance.Add(this);
|
|
|
|
|
+
|
|
|
|
|
+ if (this.components != null && this.components.Count == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.components = null;
|
|
|
|
|
+ }
|
|
|
foreach (Component component in this.components)
|
|
foreach (Component component in this.components)
|
|
|
{
|
|
{
|
|
|
|
|
+ component.Owner = this;
|
|
|
this.componentDict.Add(component.GetType(), component);
|
|
this.componentDict.Add(component.GetType(), component);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|