Buff.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Common.Base;
  2. using Common.Event;
  3. using Common.Helper;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.Serialization.Attributes;
  6. using System;
  7. namespace Model
  8. {
  9. public class Buff: Entity<Buff>, IDisposable
  10. {
  11. [BsonElement]
  12. private int configId { get; set; }
  13. [BsonElement]
  14. private ObjectId ownerId;
  15. [BsonElement]
  16. private long expiration;
  17. [BsonIgnore]
  18. private ObjectId timerId;
  19. [BsonIgnore]
  20. public long Expiration
  21. {
  22. get
  23. {
  24. return this.expiration;
  25. }
  26. set
  27. {
  28. this.expiration = value;
  29. }
  30. }
  31. [BsonIgnore]
  32. public ObjectId TimerId
  33. {
  34. get
  35. {
  36. return this.timerId;
  37. }
  38. set
  39. {
  40. this.timerId = value;
  41. }
  42. }
  43. public Buff(int configId, ObjectId ownerId)
  44. {
  45. this.configId = configId;
  46. this.ownerId = ownerId;
  47. if (this.Config.Duration != 0)
  48. {
  49. this.Expiration = TimeHelper.Now() + this.Config.Duration;
  50. }
  51. if (this.Expiration != 0)
  52. {
  53. // 注册Timer回调
  54. Env env = new Env();
  55. env[EnvKey.OwnerId] = this.OwnerId;
  56. env[EnvKey.BuffId] = this.Id;
  57. this.TimerId = World.Instance.GetComponent<TimerComponent>()
  58. .Add(this.Expiration, CallbackType.BuffTimeoutCallback, env);
  59. }
  60. }
  61. public override void BeginInit()
  62. {
  63. base.BeginInit();
  64. }
  65. public override void EndInit()
  66. {
  67. base.EndInit();
  68. if (this.Expiration != 0)
  69. {
  70. // 注册Timer回调
  71. Env env = new Env();
  72. env[EnvKey.OwnerId] = this.OwnerId;
  73. env[EnvKey.BuffId] = this.Id;
  74. this.TimerId = World.Instance.GetComponent<TimerComponent>()
  75. .Add(this.Expiration, CallbackType.BuffTimeoutCallback, env);
  76. }
  77. }
  78. [BsonIgnore]
  79. public BuffConfig Config
  80. {
  81. get
  82. {
  83. return World.Instance.GetComponent<ConfigComponent>().Get<BuffConfig>(this.configId);
  84. }
  85. }
  86. [BsonIgnore]
  87. public ObjectId OwnerId
  88. {
  89. get
  90. {
  91. return ownerId;
  92. }
  93. set
  94. {
  95. this.ownerId = value;
  96. }
  97. }
  98. public void Dispose()
  99. {
  100. if (this.Expiration == 0)
  101. {
  102. return;
  103. }
  104. World.Instance.GetComponent<TimerComponent>().Remove(this.TimerId);
  105. this.expiration = 0;
  106. }
  107. }
  108. }