Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / SubLine.cs
  1.  
  2. using System.Collections;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 瞄准辅助线
  7. /// </summary>
  8. public class SubLine : Component<Role>
  9. {
  10. /// <summary>
  11. /// 是否正在播放警告闪烁动画
  12. /// </summary>
  13. public bool IsPlayWarnAnimation => _cid > 0;
  14. private Line2D _line2D;
  15. private RayCast2D _rayCast2D;
  16.  
  17. private bool _enableSubLine;
  18. private float _range;
  19. private long _cid;
  20. private Color _color = Colors.Orange;
  21. public override void Ready()
  22. {
  23. //初始化瞄准辅助线
  24. _line2D = new Line2D();
  25. _line2D.Width = 1;
  26. AddChild(_line2D);
  27.  
  28. _rayCast2D = new RayCast2D();
  29. _rayCast2D.CollisionMask = PhysicsLayer.Wall | PhysicsLayer.Obstacle;
  30. AddChild(_rayCast2D);
  31. Master.WeaponPack.ChangeActiveItemEvent += OnChangeWeapon;
  32. }
  33.  
  34. public override void OnEnable()
  35. {
  36. OnChangeWeapon(Master.WeaponPack.ActiveItem);
  37. }
  38. public override void OnDisable()
  39. {
  40. _enableSubLine = false;
  41. _line2D.Visible = false;
  42. _rayCast2D.Enabled = false;
  43. if (_cid > 0)
  44. {
  45. StopCoroutine(_cid);
  46. }
  47. }
  48.  
  49. /// <summary>
  50. /// 设置线条颜色
  51. /// </summary>
  52. public void SetColor(Color color)
  53. {
  54. _color = color;
  55. _line2D.DefaultColor = color;
  56. }
  57.  
  58. /// <summary>
  59. /// 播放闪烁警告动画
  60. /// </summary>
  61. /// <param name="time">持续时间</param>
  62. public void PlayWarnAnimation(float time)
  63. {
  64. if (_cid > 0)
  65. {
  66. StopCoroutine(_cid);
  67. }
  68. _cid = StartCoroutine(RunWarnAnimation(time));
  69. }
  70.  
  71. private IEnumerator RunWarnAnimation(float time)
  72. {
  73. var now = 0f;
  74. var t = 0f;
  75. var b = false;
  76. while (now < time)
  77. {
  78. var delta = GetProcessDeltaTime();
  79. now += delta;
  80. t += delta;
  81. if (t >= 0.08f)
  82. {
  83. t %= 0.08f;
  84. _line2D.DefaultColor = b ? Colors.Orange : Colors.Red;
  85. b = !b;
  86. }
  87. yield return null;
  88. }
  89.  
  90. _line2D.DefaultColor = _color;
  91. _cid = 0;
  92. }
  93.  
  94. //切换武器
  95. private void OnChangeWeapon(Weapon weapon)
  96. {
  97. if (!Enable)
  98. {
  99. return;
  100. }
  101. if (weapon == null)
  102. {
  103. _enableSubLine = false;
  104. }
  105. else
  106. {
  107. _enableSubLine = true;
  108. if (_cid > 0)
  109. {
  110. StopCoroutine(_cid);
  111. _cid = 0;
  112. }
  113. _range = Utils.GetConfigRangeEnd(weapon.Attribute.Bullet.DistanceRange);
  114. _line2D.DefaultColor = _color;
  115. }
  116. }
  117.  
  118. public override void PhysicsProcess(float delta)
  119. {
  120. if (_enableSubLine)
  121. {
  122. _line2D.Visible = true;
  123. _rayCast2D.Enabled = true;
  124. UpdateSubLine();
  125. }
  126. else
  127. {
  128. _line2D.Visible = false;
  129. _rayCast2D.Enabled = false;
  130. }
  131. }
  132.  
  133. public override void OnDestroy()
  134. {
  135. _line2D.QueueFree();
  136. _rayCast2D.QueueFree();
  137. Master.WeaponPack.ChangeActiveItemEvent -= OnChangeWeapon;
  138. }
  139.  
  140. private void UpdateSubLine()
  141. {
  142. var master = Master;
  143. var weapon = master.WeaponPack.ActiveItem;
  144. float length;
  145. var firePointGlobalPosition = weapon.FirePoint.GlobalPosition;
  146. if (_rayCast2D.IsColliding())
  147. {
  148. length = firePointGlobalPosition.DistanceTo(_rayCast2D.GetCollisionPoint());
  149. }
  150. else
  151. {
  152. length = _range;
  153. }
  154.  
  155. var r = master.ConvertRotation(master.MountPoint.RealRotation);
  156. //更新 Ray 的位置角度
  157. _rayCast2D.GlobalPosition = firePointGlobalPosition;
  158. _rayCast2D.TargetPosition = new Vector2(_range, 0);
  159. _rayCast2D.Rotation = r;
  160.  
  161. //计算 line2D 的点
  162. var position = _line2D.ToLocal(firePointGlobalPosition);
  163. Vector2 position2 = Vector2.FromAngle(r) * length;
  164.  
  165. _line2D.Points = new Vector2[]
  166. {
  167. position,
  168. position + position2
  169. };
  170. }
  171. }