Component.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. this.InstanceId = IdGenerater.GenerateId();
  27. Game.EventSystem.Add(this);
  28. }
  29. }
  30. [BsonIgnore]
  31. public bool IsDisposed
  32. {
  33. get
  34. {
  35. return this.InstanceId == 0;
  36. }
  37. }
  38. [BsonIgnore]
  39. public Component Parent { get; set; }
  40. public T GetParent<T>() where T : Component
  41. {
  42. return this.Parent as T;
  43. }
  44. [BsonIgnore]
  45. public Entity Entity
  46. {
  47. get
  48. {
  49. return this.Parent as Entity;
  50. }
  51. }
  52. protected Component()
  53. {
  54. this.InstanceId = IdGenerater.GenerateId();
  55. }
  56. public virtual void Dispose()
  57. {
  58. if (this.IsDisposed)
  59. {
  60. return;
  61. }
  62. // 触发Destroy事件
  63. Game.EventSystem.Destroy(this);
  64. Game.EventSystem.Remove(this.InstanceId);
  65. this.InstanceId = 0;
  66. if (this.IsFromPool)
  67. {
  68. Game.ObjectPool.Recycle(this);
  69. }
  70. }
  71. public override void EndInit()
  72. {
  73. Game.EventSystem.Deserialize(this);
  74. }
  75. }
  76. }