Component.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  81. }
  82. }