Component.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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; 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.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. this.Id = this.InstanceId;
  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. }