IExperienceSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.IO;
  3. public abstract class IExperienceSystem : ICloneable
  4. {
  5. protected IExperienceSystem()
  6. {
  7. }
  8. protected IExperienceSystem(IExperienceSystem other)
  9. {
  10. this.current_exp_ = other.current_exp_;
  11. this.total_exp_ = other.total_exp_;
  12. this.next_exp_ = other.next_exp_;
  13. this.level_ = other.level_;
  14. }
  15. public abstract int GetMaxLevel();
  16. public virtual int GetNextLevelExp(int level)
  17. {
  18. level++;
  19. if (level <= 0 || this.GetMaxLevel() < level)
  20. {
  21. return 0;
  22. }
  23. return this.GetNeedExp(level);
  24. }
  25. public virtual int GetCurrentLevel()
  26. {
  27. return this.level_;
  28. }
  29. public virtual int GetCurrentExp()
  30. {
  31. return this.current_exp_;
  32. }
  33. public virtual int GetNextLevelRestExp()
  34. {
  35. return this.next_exp_;
  36. }
  37. public virtual int GetMaxLevelNeedExp()
  38. {
  39. int num = 0;
  40. for (int i = 0; i < this.GetMaxLevel(); i++)
  41. {
  42. num += this.GetNextLevelExp(i);
  43. }
  44. return num;
  45. }
  46. public virtual int GetTotalExp()
  47. {
  48. return this.total_exp_;
  49. }
  50. public virtual void AddExp(int exp)
  51. {
  52. this.total_exp_ += exp;
  53. this.Update();
  54. }
  55. public virtual void SetLevel(int level)
  56. {
  57. this.total_exp_ = 0;
  58. if (level < 0)
  59. {
  60. level = 0;
  61. }
  62. int maxLevel = this.GetMaxLevel();
  63. if (maxLevel < level)
  64. {
  65. level = maxLevel;
  66. }
  67. if (1 <= level)
  68. {
  69. level = Math.Min(this.GetMaxLevel(), level);
  70. for (int i = 1; i <= level; i++)
  71. {
  72. this.total_exp_ += this.GetNeedExp(i);
  73. }
  74. }
  75. this.Update();
  76. }
  77. public virtual void SetTotalExp(int total_exp)
  78. {
  79. this.total_exp_ = total_exp;
  80. this.Update();
  81. }
  82. public virtual void Update()
  83. {
  84. int maxLevelNeedExp = this.GetMaxLevelNeedExp();
  85. if (maxLevelNeedExp < this.total_exp_)
  86. {
  87. this.total_exp_ = maxLevelNeedExp;
  88. }
  89. if (this.total_exp_ < 0)
  90. {
  91. this.total_exp_ = 0;
  92. }
  93. int num = this.total_exp_;
  94. int maxLevel = this.GetMaxLevel();
  95. for (int i = 1; i <= maxLevel; i++)
  96. {
  97. int needExp = this.GetNeedExp(i);
  98. num -= this.GetNeedExp(i);
  99. if (num < 0)
  100. {
  101. this.level_ = i - 1;
  102. this.next_exp_ = Math.Abs(num);
  103. this.current_exp_ = needExp + num;
  104. return;
  105. }
  106. }
  107. this.level_ = maxLevel;
  108. this.current_exp_ = (this.next_exp_ = 0);
  109. }
  110. public virtual void Serialize(BinaryWriter binary)
  111. {
  112. binary.Write(this.current_exp_);
  113. binary.Write(this.total_exp_);
  114. binary.Write(this.next_exp_);
  115. binary.Write(this.level_);
  116. }
  117. public virtual void Deserialize(BinaryReader binary, int version)
  118. {
  119. this.current_exp_ = binary.ReadInt32();
  120. this.total_exp_ = binary.ReadInt32();
  121. this.next_exp_ = binary.ReadInt32();
  122. this.level_ = binary.ReadInt32();
  123. }
  124. protected abstract int GetNeedExp(int level);
  125. public abstract object Clone();
  126. protected int current_exp_;
  127. protected int total_exp_;
  128. protected int next_exp_;
  129. protected int level_;
  130. }