Newer
Older
DungeonShooting / src / weapon / bullet / HighSpeedBullet.cs
@小李xl 小李xl on 9 Jun 2022 1 KB 实现投抛物体
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 高速子弹
  5. /// </summary>
  6. public class HighSpeedBullet : Node2D, IBullet
  7. {
  8. public CampEnum TargetCamp { get; private set; }
  9.  
  10. public Gun Gun { get; private set; }
  11.  
  12. public Node2D Master { get; private set; }
  13.  
  14. /// <summary>
  15. /// 碰撞物体后产生的火花
  16. /// </summary>
  17. [Export] public PackedScene Hit;
  18.  
  19. //射线检测节点
  20. private RayCast2D RayCast2D;
  21. private Line2D Line;
  22. private float ca = 1;
  23.  
  24. public void Init(CampEnum target, Gun gun, Node2D master)
  25. {
  26. TargetCamp = target;
  27. Gun = gun;
  28. Master = master;
  29.  
  30. //飞行距离
  31. var distance = MathUtils.RandRange(gun.Attribute.MinDistance, gun.Attribute.MaxDistance);
  32.  
  33. //初始化子弹数据
  34. RayCast2D = GetNode<RayCast2D>("RayCast2D");
  35. Line = GetNode<Line2D>("Line");
  36. Modulate = Colors.White;
  37.  
  38. // 目标点
  39. Vector2 targetPos = new Vector2(distance, 0);
  40. RayCast2D.CastTo = targetPos;
  41. RayCast2D.ForceRaycastUpdate();
  42. if (RayCast2D.IsColliding())
  43. {
  44. //碰到物体
  45. Vector2 collPosition = RayCast2D.GetCollisionPoint();
  46. Node2D hit = Hit.Instance<Node2D>();
  47. hit.RotationDegrees = MathUtils.RandRangeInt(0, 360);
  48. hit.GlobalPosition = collPosition;
  49. GetTree().CurrentScene.AddChild(hit);
  50. //划线的点坐标
  51. Line.SetPointPosition(1, new Vector2(Line.GlobalPosition.DistanceTo(collPosition), 0));
  52. }
  53. else
  54. {
  55. //划线的点坐标
  56. Line.SetPointPosition(1, targetPos);
  57. }
  58. RayCast2D.Enabled = false;
  59. }
  60.  
  61. public override void _Process(float delta)
  62. {
  63. ca -= 12 * delta;
  64. if (ca <= 0) {
  65. QueueFree();
  66. return;
  67. }
  68. Color c = Modulate;
  69. c.a = ca;
  70. Modulate = c;
  71. }
  72. }