Component.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ETModel;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace ETHotfix
  4. {
  5. [BsonIgnoreExtraElements]
  6. public abstract partial 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.InstanceId == 0)
  23. {
  24. this.InstanceId = IdGenerater.GenerateId();
  25. Game.EventSystem.Add(this);
  26. }
  27. }
  28. }
  29. [BsonIgnore]
  30. public bool IsDisposed
  31. {
  32. get
  33. {
  34. return this.InstanceId == 0;
  35. }
  36. }
  37. [BsonIgnore]
  38. public Component Parent { get; set; }
  39. public T GetParent<T>() where T : Component
  40. {
  41. return this.Parent as T;
  42. }
  43. [BsonIgnore]
  44. public Entity Entity
  45. {
  46. get
  47. {
  48. return this.Parent as Entity;
  49. }
  50. }
  51. protected Component()
  52. {
  53. this.InstanceId = IdGenerater.GenerateId();
  54. Game.EventSystem.Add(this);
  55. }
  56. public virtual void Dispose()
  57. {
  58. if (this.IsDisposed)
  59. {
  60. return;
  61. }
  62. Game.EventSystem.Remove(this.InstanceId);
  63. this.InstanceId = 0;
  64. if (this.IsFromPool)
  65. {
  66. Game.ObjectPool.Recycle(this);
  67. }
  68. // 触发Destroy事件
  69. Game.EventSystem.Destroy(this);
  70. }
  71. }
  72. }