Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / bullet / Bullet.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 子弹类
  5. /// </summary>
  6. [RegisterActivity(ActivityIdPrefix.Bullet + "0001", ResourcePath.prefab_weapon_bullet_Bullet_tscn)]
  7. public partial class Bullet : ActivityObject
  8. {
  9. /// <summary>
  10. /// 碰撞区域
  11. /// </summary>
  12. public Area2D CollisionArea { get; private set; }
  13.  
  14. // 最大飞行距离
  15. private float MaxDistance;
  16.  
  17. // 子弹飞行速度
  18. private float FlySpeed;
  19.  
  20. //当前子弹已经飞行的距离
  21. private float CurrFlyDistance = 0;
  22.  
  23. public void Init(float speed, float maxDistance, Vector2 position, float rotation, uint targetLayer)
  24. {
  25. CollisionArea = GetNode<Area2D>("CollisionArea");
  26. CollisionArea.CollisionMask = targetLayer;
  27. CollisionArea.AreaEntered += OnArea2dEntered;
  28.  
  29. FlySpeed = speed;
  30. MaxDistance = maxDistance;
  31. Position = position;
  32. Rotation = rotation;
  33. ShadowOffset = new Vector2(0, 5);
  34.  
  35. BasisVelocity = new Vector2(FlySpeed, 0).Rotated(Rotation);
  36. }
  37.  
  38. protected override void PhysicsProcessOver(float delta)
  39. {
  40. //移动
  41. var lastSlideCollision = GetLastSlideCollision();
  42. if (lastSlideCollision != null)
  43. {
  44. //创建粒子特效
  45. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_BulletSmoke_tscn);
  46. var smoke = packedScene.Instantiate<GpuParticles2D>();
  47. smoke.GlobalPosition = lastSlideCollision.GetPosition();
  48. smoke.GlobalRotation = lastSlideCollision.GetNormal().Angle();
  49. smoke.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  50.  
  51. Destroy();
  52. return;
  53. }
  54. //距离太大, 自动销毁
  55. CurrFlyDistance += FlySpeed * delta;
  56. if (CurrFlyDistance >= MaxDistance)
  57. {
  58. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_BulletDisappear_tscn);
  59. var node = packedScene.Instantiate<Node2D>();
  60. node.GlobalPosition = GlobalPosition;
  61. node.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  62. Destroy();
  63. }
  64. }
  65.  
  66. private void OnArea2dEntered(Area2D other)
  67. {
  68. var role = other.AsActivityObject<Role>();
  69. if (role != null)
  70. {
  71. role.CallDeferred(nameof(Role.Hurt), 4, Rotation);
  72. Destroy();
  73. }
  74. }
  75. }