Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / bullet / Bullet.cs
@小李xl 小李xl on 6 Dec 2022 2 KB 添加子弹碰撞烟雾
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 子弹类
  5. /// </summary>
  6. public class Bullet : ActivityObject
  7. {
  8. /// <summary>
  9. /// 碰撞区域
  10. /// </summary>
  11. public Area2D CollisionArea { get; }
  12.  
  13. // 最大飞行距离
  14. private float MaxDistance;
  15.  
  16. // 子弹飞行速度
  17. private float FlySpeed = 350;
  18.  
  19. //当前子弹已经飞行的距离
  20. private float CurrFlyDistance = 0;
  21.  
  22. public Bullet(string scenePath, float maxDistance, Vector2 position, float rotation, uint targetLayer) :
  23. base(scenePath)
  24. {
  25. CollisionArea = GetNode<Area2D>("CollisionArea");
  26. CollisionArea.CollisionMask = targetLayer;
  27. CollisionArea.Connect("area_entered", this, nameof(OnArea2dEntered));
  28. CollisionArea.Connect("body_entered", this, nameof(OnBodyEntered));
  29.  
  30. Collision.Disabled = true;
  31.  
  32. MaxDistance = maxDistance;
  33. Position = position;
  34. Rotation = rotation;
  35. ShadowOffset = new Vector2(0, 5);
  36. }
  37.  
  38. public override void _Ready()
  39. {
  40. base._Ready();
  41. //绘制阴影
  42. ShowShadowSprite();
  43. }
  44.  
  45. public override void _PhysicsProcess(float delta)
  46. {
  47. base._PhysicsProcess(delta);
  48. //移动
  49. Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation);
  50. //距离太大, 自动销毁
  51. CurrFlyDistance += FlySpeed * delta;
  52. if (CurrFlyDistance >= MaxDistance)
  53. {
  54. Destroy();
  55. }
  56. }
  57.  
  58. private void OnArea2dEntered(Area2D other)
  59. {
  60. var role = other.AsActivityObject<Role>();
  61. if (role != null)
  62. {
  63. role.Hurt(1);
  64. //播放受击动画
  65. // Node2D hit = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_Hit_tscn).Instance<Node2D>();
  66. // hit.RotationDegrees = Utils.RandRangeInt(0, 360);
  67. // hit.GlobalPosition = GlobalPosition;
  68. // GameApplication.Instance.Room.GetRoot(true).AddChild(hit);
  69.  
  70. DoDestroy();
  71. }
  72. }
  73.  
  74. private void OnBodyEntered(Node2D other)
  75. {
  76. if (!(other is Role))
  77. {
  78. DoDestroy();
  79. }
  80. }
  81.  
  82. private void DoDestroy()
  83. {
  84. // SpecialEffectManager.Play(ResourcePath.resource_effects_Hit_tres, "default", GlobalPosition,
  85. // Mathf.Deg2Rad(Utils.RandRangeInt(0, 360)), Vector2.One, new Vector2(1, 11), 0);
  86.  
  87. //创建粒子特效
  88. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_BulletSmoke_tscn);
  89. var smoke = packedScene.Instance<Particles2D>();
  90. smoke.Position = Position;
  91. smoke.Rotation = Rotation - Mathf.Pi;
  92. GameApplication.Instance.Room.GetRoot(true).AddChild(smoke);
  93. Destroy();
  94. }
  95. }