Newer
Older
DungeonShooting / src / weapon / bullet / OrdinaryBullets.cs
@小李xl 小李xl on 9 Jun 2022 2 KB 实现投抛物体
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 普通的子弹
  5. /// </summary>
  6. public class OrdinaryBullets : 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 Vector2 StartPosition;
  21. // 最大飞行距离
  22. private float MaxDistance;
  23. // 子弹飞行速度
  24. private float FlySpeed = 1500;
  25. //当前子弹已经飞行的距离
  26. private float CurrFlyDistance = 0;
  27. //射线碰撞节点
  28. private RayCast2D RayCast;
  29. //子弹的精灵
  30. private Sprite BulletSprite;
  31.  
  32. private int frame = 0;
  33.  
  34. public void Init(CampEnum target, Gun gun, Node2D master)
  35. {
  36. TargetCamp = target;
  37. Gun = gun;
  38. Master = master;
  39.  
  40. MaxDistance = MathUtils.RandRange(gun.Attribute.MinDistance, gun.Attribute.MaxDistance);
  41. StartPosition = GlobalPosition;
  42. BulletSprite = GetNode<Sprite>("Bullet");
  43. BulletSprite.Visible = false;
  44. RayCast = GetNode<RayCast2D>("RayCast2D");
  45. }
  46.  
  47. public override void _PhysicsProcess(float delta)
  48. {
  49. if (frame++ == 0)
  50. {
  51. BulletSprite.Visible = true;
  52. }
  53. //碰到墙壁
  54. if (RayCast.IsColliding())
  55. {
  56. //var target = RayCast.GetCollider();
  57. var pos = RayCast.GetCollisionPoint();
  58. //播放受击动画
  59. Node2D hit = Hit.Instance<Node2D>();
  60. hit.RotationDegrees = MathUtils.RandRangeInt(0, 360);
  61. hit.GlobalPosition = pos;
  62. GetTree().CurrentScene.AddChild(hit);
  63. QueueFree();
  64. }
  65. else //没有碰到, 继续移动
  66. {
  67. Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation);
  68.  
  69. CurrFlyDistance += FlySpeed * delta;
  70. if (CurrFlyDistance >= MaxDistance)
  71. {
  72. QueueFree();
  73. }
  74. }
  75. }
  76.  
  77. }