Component.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using ETModel;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace ETHotfix
  4. {
  5. [BsonIgnoreExtraElements]
  6. public abstract class Component : Object, IDisposable
  7. {
  8. [BsonIgnore]
  9. public long InstanceId { get; protected set; }
  10. [BsonIgnore]
  11. private bool isFromPool;
  12. [BsonIgnore]
  13. public bool IsFromPool
  14. {
  15. get
  16. {
  17. return this.isFromPool;
  18. }
  19. set
  20. {
  21. this.isFromPool = value;
  22. if (!this.isFromPool)
  23. {
  24. return;
  25. }
  26. if (this.InstanceId == 0)
  27. {
  28. this.InstanceId = IdGenerater.GenerateId();
  29. }
  30. Game.EventSystem.Add(this);
  31. }
  32. }
  33. [BsonIgnore]
  34. public bool IsDisposed
  35. {
  36. get
  37. {
  38. return this.InstanceId == 0;
  39. }
  40. }
  41. [BsonIgnore]
  42. public Component Parent { get; set; }
  43. public T GetParent<T>() where T : Component
  44. {
  45. return this.Parent as T;
  46. }
  47. [BsonIgnore]
  48. public Entity Entity
  49. {
  50. get
  51. {
  52. return this.Parent as Entity;
  53. }
  54. }
  55. protected Component()
  56. {
  57. this.InstanceId = IdGenerater.GenerateId();
  58. }
  59. public virtual void Dispose()
  60. {
  61. if (this.IsDisposed)
  62. {
  63. return;
  64. }
  65. Game.EventSystem.Remove(this.InstanceId);
  66. this.InstanceId = 0;
  67. if (this.IsFromPool)
  68. {
  69. Game.ObjectPool.Recycle(this);
  70. }
  71. // 触发Destroy事件
  72. Game.EventSystem.Destroy(this);
  73. }
  74. }
  75. }