Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / prop / active / ActiveProp.cs
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 主动使用道具
  6. /// </summary>
  7. public abstract partial class ActiveProp : Prop
  8. {
  9. /// <summary>
  10. /// 道具可使用次数
  11. /// </summary>
  12. public int Count
  13. {
  14. get => _count;
  15. set
  16. {
  17. var temp = _count;
  18. _count = Mathf.Clamp(value, 0, _maxCount);
  19. if (temp != _count)
  20. {
  21. OnChangeCount();
  22. }
  23. }
  24. }
  25.  
  26. private int _count = 1;
  27.  
  28. /// <summary>
  29. /// 道具最大可使用次数
  30. /// </summary>
  31. public int MaxCount
  32. {
  33. get => _maxCount;
  34. set
  35. {
  36. var temp = _maxCount;
  37. _maxCount = Mathf.Max(1, value);
  38. if (temp != _maxCount)
  39. {
  40. OnChangeMaxCount();
  41. }
  42.  
  43. if (Count > _maxCount)
  44. {
  45. Count = _maxCount;
  46. }
  47. }
  48. }
  49.  
  50. private int _maxCount = 1;
  51.  
  52. /// <summary>
  53. /// 使用一次后的冷却时间, 单位: 秒
  54. /// </summary>
  55. public float CooldownTime { get; set; } = 2f;
  56. /// <summary>
  57. /// 当道具使用完后是否自动销毁
  58. /// </summary>
  59. public bool AutoDestroy { get; set; } = false;
  60.  
  61. /// <summary>
  62. /// 道具充能进度, 必须要充能完成才能使用, 值范围: 0 - 1
  63. /// </summary>
  64. public float ChargeProgress
  65. {
  66. get => _chargeProgress;
  67. set
  68. {
  69. var temp = _chargeProgress;
  70. _chargeProgress = Mathf.Clamp(value, 0, 1);
  71. if (temp != _chargeProgress)
  72. {
  73. OnChangeChargeProgress();
  74. }
  75. }
  76. }
  77.  
  78. private float _chargeProgress = 1;
  79.  
  80.  
  81. //冷却计时器
  82. private float _cooldownTimer = 0;
  83. /// <summary>
  84. /// 当检测是否可以使用时调用
  85. /// </summary>
  86. public abstract bool OnCheckUse();
  87.  
  88. /// <summary>
  89. /// 当道具被使用时调用, 函数返回值为消耗数量
  90. /// </summary>
  91. protected abstract int OnUse();
  92.  
  93. protected override void OnPickUp(Role master)
  94. {
  95. }
  96.  
  97. protected override void OnRemove(Role master)
  98. {
  99. }
  100.  
  101. /// <summary>
  102. /// 道具数量改变时调用
  103. /// </summary>
  104. protected virtual void OnChangeCount()
  105. {
  106. }
  107.  
  108. /// <summary>
  109. /// 道具最大数量改变时调用
  110. /// </summary>
  111. protected virtual void OnChangeMaxCount()
  112. {
  113. }
  114.  
  115. /// <summary>
  116. /// 道具充能进度改变时调用
  117. /// </summary>
  118. protected virtual void OnChangeChargeProgress()
  119. {
  120. }
  121.  
  122. /// <summary>
  123. /// 冷却完成时调用
  124. /// </summary>
  125. protected virtual void OnCooldownFinish()
  126. {
  127. }
  128. public override void PackProcess(float delta)
  129. {
  130. if (_cooldownTimer > 0)
  131. {
  132. _cooldownTimer -= delta;
  133.  
  134. //冷却完成
  135. if (_cooldownTimer <= 0)
  136. {
  137. _cooldownTimer = 0;
  138. OnCooldownFinish();
  139. }
  140. }
  141. }
  142.  
  143. /// <summary>
  144. /// 检测是否可以使用当前道具
  145. /// </summary>
  146. public bool CheckUse()
  147. {
  148. return ChargeProgress >= 1 && _cooldownTimer <= 0 && Count > 0 && OnCheckUse();
  149. }
  150. /// <summary>
  151. /// 触发使用道具
  152. /// </summary>
  153. public void Use()
  154. {
  155. if (Master == null)
  156. {
  157. return;
  158. }
  159. if (CheckUse()) //可以使用道具
  160. {
  161. var num = OnUse();
  162. if (num != 0)
  163. {
  164. Count -= num;
  165. }
  166.  
  167. //冷却计时器
  168. _cooldownTimer = CooldownTime;
  169. if (Count == 0 && AutoDestroy) //用光了, 自动销毁
  170. {
  171. Master.RemoveProp(this);
  172. Destroy();
  173. }
  174. }
  175. }
  176.  
  177. /// <summary>
  178. /// 获取冷却进度 0 - 1
  179. /// </summary>
  180. public float GetCooldownProgress()
  181. {
  182. if (_cooldownTimer <= 0)
  183. {
  184. return 1;
  185. }
  186.  
  187. return (CooldownTime - _cooldownTimer) / CooldownTime;
  188. }
  189. }