Newer
Older
DungeonShooting / src / bullet / HighSpeedBullet.cs
@小李xl 小李xl on 30 May 2022 1 KB 更改目录结构
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 高速子弹
  5. /// </summary>
  6. public class HighSpeedBullet : Bullet
  7. {
  8. [Export] public PackedScene Hit;
  9. //射线检测节点
  10. private RayCast2D RayCast2D;
  11. //最大飞行距离
  12. private float Distance;
  13. private Line2D Line;
  14. private float ca = 1;
  15.  
  16. public void InitData(float distance, Color color)
  17. {
  18. RayCast2D = GetNode<RayCast2D>("RayCast2D");
  19. Line = GetNode<Line2D>("Line");
  20. Distance = distance;
  21. Modulate = color;
  22.  
  23. Vector2 targetPos = new Vector2(distance, 0);
  24.  
  25. //
  26. RayCast2D.CastTo = targetPos;
  27. RayCast2D.ForceRaycastUpdate();
  28. if (RayCast2D.IsColliding()) { //碰到物体
  29. Vector2 collPosition = RayCast2D.GetCollisionPoint();
  30. Node2D hit = Hit.Instance<Node2D>();
  31. hit.RotationDegrees = MathUtils.RandRangeInt(0, 360);
  32. hit.GlobalPosition = collPosition;
  33. GetTree().CurrentScene.AddChild(hit);
  34. //划线的点坐标
  35. Line.SetPointPosition(1, new Vector2(Line.GlobalPosition.DistanceTo(collPosition), 0));
  36. }
  37. else
  38. {
  39. //划线的点坐标
  40. Line.SetPointPosition(1, targetPos);
  41. }
  42. RayCast2D.Enabled = false;
  43. }
  44.  
  45. public override void _Process(float delta)
  46. {
  47. ca -= 12 * delta;
  48. if (ca <= 0) {
  49. QueueFree();
  50. return;
  51. }
  52. Color c = Modulate;
  53. c.a = ca;
  54. Modulate = c;
  55. }
  56. }