Component.cs 1.4 KB

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