Component.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using ETModel;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. namespace ETHotfix
  5. {
  6. [BsonIgnoreExtraElements]
  7. public abstract partial class Component : Object, IDisposable2
  8. {
  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.InstanceId == 0)
  24. {
  25. this.InstanceId = IdGenerater.GenerateId();
  26. Game.EventSystem.Add(this);
  27. }
  28. }
  29. }
  30. [BsonIgnore]
  31. public bool IsDisposed
  32. {
  33. get
  34. {
  35. return this.InstanceId == 0;
  36. }
  37. }
  38. [BsonIgnoreIfDefault]
  39. [BsonDefaultValue(0L)]
  40. [BsonElement]
  41. [BsonId]
  42. public long Id { get; set; }
  43. [BsonIgnore]
  44. public Component Parent { get; set; }
  45. public T GetParent<T>() where T : Component
  46. {
  47. return this.Parent as T;
  48. }
  49. [BsonIgnore]
  50. public Entity Entity
  51. {
  52. get
  53. {
  54. return this.Parent as Entity;
  55. }
  56. }
  57. protected Component()
  58. {
  59. this.InstanceId = IdGenerater.GenerateId();
  60. Game.EventSystem.Add(this);
  61. this.Id = this.InstanceId;
  62. }
  63. protected Component(long instanceId)
  64. {
  65. this.InstanceId = instanceId;
  66. Game.EventSystem.Add(this);
  67. }
  68. public virtual void Dispose()
  69. {
  70. if (this.IsDisposed)
  71. {
  72. return;
  73. }
  74. Game.EventSystem.Remove(this.InstanceId);
  75. this.InstanceId = 0;
  76. if (this.IsFromPool)
  77. {
  78. Game.ObjectPool.Recycle(this);
  79. }
  80. // 触发Desdroy事件
  81. Game.EventSystem.Desdroy(this);
  82. }
  83. }
  84. }