Component.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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; private 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. }
  62. protected Component(long instanceId)
  63. {
  64. this.InstanceId = instanceId;
  65. Game.EventSystem.Add(this);
  66. }
  67. public virtual void Dispose()
  68. {
  69. if (this.IsDisposed)
  70. {
  71. return;
  72. }
  73. Game.EventSystem.Remove(this.InstanceId);
  74. this.InstanceId = 0;
  75. if (this.IsFromPool)
  76. {
  77. Game.ObjectPool.Recycle(this);
  78. }
  79. }
  80. }
  81. }