Component.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using ETModel;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace ETHotfix
  4. {
  5. [BsonIgnoreExtraElements]
  6. public abstract class Component : Object, IDisposable, IComponentSerialize
  7. {
  8. // 只有Game.EventSystem.Add方法中会设置该值,如果new出来的对象不想加入Game.EventSystem中,则需要自己在构造函数中设置
  9. [BsonIgnore]
  10. public long InstanceId { get; protected set; }
  11. [BsonIgnore]
  12. private bool isFromPool;
  13. [BsonIgnore]
  14. public bool IsFromPool
  15. {
  16. get
  17. {
  18. return this.isFromPool;
  19. }
  20. set
  21. {
  22. this.isFromPool = value;
  23. if (!this.isFromPool)
  24. {
  25. return;
  26. }
  27. this.InstanceId = IdGenerater.GenerateId();
  28. Game.EventSystem.Add(this);
  29. }
  30. }
  31. [BsonIgnore]
  32. public bool IsDisposed
  33. {
  34. get
  35. {
  36. return this.InstanceId == 0;
  37. }
  38. }
  39. [BsonIgnore]
  40. public Component Parent { get; set; }
  41. public T GetParent<T>() where T : Component
  42. {
  43. return this.Parent as T;
  44. }
  45. [BsonIgnore]
  46. public Entity Entity
  47. {
  48. get
  49. {
  50. return this.Parent as Entity;
  51. }
  52. }
  53. protected Component()
  54. {
  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 virtual void BeginSerialize()
  72. {
  73. }
  74. public virtual void EndDeSerialize()
  75. {
  76. }
  77. }
  78. }