Component.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace ETModel
  4. {
  5. [BsonIgnoreExtraElements]
  6. public abstract partial class Component : Object, IDisposable
  7. {
  8. [BsonIgnore]
  9. public long InstanceId { get; private 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. [BsonIgnoreIfDefault]
  38. [BsonDefaultValue(0L)]
  39. [BsonElement]
  40. [BsonId]
  41. public long Id { get; set; }
  42. [BsonIgnore]
  43. public Component Parent { get; set; }
  44. public T GetParent<T>() where T : Component
  45. {
  46. return this.Parent as T;
  47. }
  48. [BsonIgnore]
  49. public Entity Entity
  50. {
  51. get
  52. {
  53. return this.Parent as Entity;
  54. }
  55. }
  56. protected Component()
  57. {
  58. this.InstanceId = IdGenerater.GenerateId();
  59. Game.EventSystem.Add(this);
  60. }
  61. protected Component(long instanceId)
  62. {
  63. this.InstanceId = instanceId;
  64. Game.EventSystem.Add(this);
  65. }
  66. public virtual void Dispose()
  67. {
  68. if (this.IsDisposed)
  69. {
  70. return;
  71. }
  72. Game.EventSystem.Remove(this.InstanceId);
  73. this.InstanceId = 0;
  74. if (this.IsFromPool)
  75. {
  76. Game.ObjectPool.Recycle(this);
  77. }
  78. }
  79. }
  80. }