| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MongoDB.Bson.Serialization.Attributes;
- namespace Base
- {
- public class Entity: Object
- {
- public string Type { get; set; }
- [BsonElement]
- [BsonIgnoreIfNull]
- private HashSet<Component> components = new HashSet<Component>();
- [BsonIgnore]
- private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
- protected Entity(string entityType)
- {
- this.Type = entityType;
- }
- protected Entity(long id, string entityType) : base(id)
- {
- this.Type = entityType;
- }
-
- public override void Dispose()
- {
- if (this.Id == 0)
- {
- return;
- }
-
- base.Dispose();
- foreach (Component component in this.GetComponents())
- {
- try
- {
- component.Dispose();
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- }
- public K AddComponent<K>() where K : Component, new()
- {
- K component = (K) Activator.CreateInstance(typeof (K));
- component.Owner = this;
- if (this.componentDict.ContainsKey(component.GetType()))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof (K).Name}");
- }
- if (this.components == null)
- {
- this.components = new HashSet<Component>();
- }
- this.components.Add(component);
- this.componentDict.Add(component.GetType(), component);
- ObjectManager.Awake(component.Id);
- return component;
- }
- public K AddComponent<K, P1>(P1 p1) where K : Component, new()
- {
- K component = (K)Activator.CreateInstance(typeof(K));
- component.Owner = this;
- if (this.componentDict.ContainsKey(component.GetType()))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- if (this.components == null)
- {
- this.components = new HashSet<Component>();
- }
- this.components.Add(component);
- this.componentDict.Add(component.GetType(), component);
- ObjectManager.Awake(component.Id, p1);
- return component;
- }
- public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
- {
- K component = (K)Activator.CreateInstance(typeof(K));
- component.Owner = this;
- if (this.componentDict.ContainsKey(component.GetType()))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- if (this.components == null)
- {
- this.components = new HashSet<Component>();
- }
- this.components.Add(component);
- this.componentDict.Add(component.GetType(), component);
- ObjectManager.Awake(component.Id, p1, p2);
- return component;
- }
- 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;
- if (this.componentDict.ContainsKey(component.GetType()))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
- }
- if (this.components == null)
- {
- this.components = new HashSet<Component>();
- }
- this.components.Add(component);
- this.componentDict.Add(component.GetType(), component);
- ObjectManager.Awake(component.Id, p1, p2, p3);
- return component;
- }
- public void AddComponent(Component component)
- {
- if (this.componentDict.ContainsKey(component.GetType()))
- {
- throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {component.GetType().Name}");
- }
- if (this.components == null)
- {
- this.components = new HashSet<Component>();
- }
- this.components.Add(component);
- this.componentDict.Add(component.GetType(), component);
- ObjectManager.Awake(component.Id);
- }
- public void RemoveComponent<K>() where K : Component
- {
- Component component;
- 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();
- }
- 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[] GetComponents()
- {
- return components.ToArray();
- }
- public override void BeginInit()
- {
- base.BeginInit();
- this.components = new HashSet<Component>();
- this.componentDict = new Dictionary<Type, Component>();
- }
- public override void EndInit()
- {
- 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);
- }
- }
- }
- }
|