123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using MongoDB.Bson.Serialization.Attributes;
- namespace KYFramework
- {
- [BsonIgnoreExtraElements]
- public class Entity : ComponentWithId
- {
- [BsonElement("C")]
- [BsonIgnoreIfNull]
- private HashSet<Component> components = new HashSet<Component>();
- [BsonIgnore]
- private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
- public Entity()
- {
- }
- protected Entity(long id) : base(id)
- {
- }
- public override void Dispose()
- {
- if (this.IsDisposed)
- {
- return;
- }
- base.Dispose();
- foreach (Component component in this.componentDict.Values)
- {
- try
- {
- component.Dispose();
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- this.components.Clear();
- this.componentDict.Clear();
- }
- public virtual Component AddComponent(Component component)
- {
- Type type = component.GetType();
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
- }
- component.Parent = this;
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual Component AddComponent(Type type)
- {
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
- }
- Component component = ComponentFactory.CreateWithParent(type, this, this.IsFromPool);
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual K AddComponent<K>() where K : Component, new()
- {
- Type type = typeof(K);
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- K component = ComponentFactory.CreateWithParent<K>(this, this.IsFromPool);
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual K AddComponent<K, P1>(P1 p1) where K : Component, new()
- {
- Type type = typeof(K);
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- K component = ComponentFactory.CreateWithParent<K, P1>(this, p1, this.IsFromPool);
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
- {
- Type type = typeof(K);
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2, this.IsFromPool);
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
- {
- Type type = typeof(K);
- if (this.componentDict.ContainsKey(type))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3, this.IsFromPool);
- this.componentDict.Add(type, component);
- if (component is ISerializeToEntity)
- {
- this.components.Add(component);
- }
- return component;
- }
- public virtual void RemoveComponent<K>() where K : Component
- {
- if (this.IsDisposed)
- {
- return;
- }
- Type type = typeof(K);
- Component component;
- if (!this.componentDict.TryGetValue(type, out component))
- {
- return;
- }
- this.componentDict.Remove(type);
- this.components.Remove(component);
- component.Dispose();
- }
- public virtual void RemoveComponent(Type type)
- {
- if (this.IsDisposed)
- {
- return;
- }
- Component component;
- if (!this.componentDict.TryGetValue(type, out component))
- {
- return;
- }
- this.componentDict.Remove(type);
- this.components.Remove(component);
- component.Dispose();
- }
- public K GetComponent<K>() where K : Component
- {
- Component component;
- if (!this.componentDict.TryGetValue(typeof(K), out component))
- {
- return default(K);
- }
- return (K)component;
- }
- public Component GetComponent(Type type)
- {
- Component component;
- if (!this.componentDict.TryGetValue(type, out component))
- {
- return null;
- }
- return component;
- }
- public Component[] GetComponents()
- {
- return this.componentDict.Values.ToArray();
- }
- public override void EndInit()
- {
- try
- {
- base.EndInit();
- this.componentDict.Clear();
- if (this.components != null)
- {
- foreach (Component component in this.components)
- {
- component.Parent = this;
- this.componentDict.Add(component.GetType(), component);
- }
- }
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- }
|