Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / bullet / Bullet.cs
@小李xl 小李xl on 13 Feb 2023 2 KB PutDown()参数改为枚举类型
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 子弹类
  5. /// </summary>
  6. public partial 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;
  18.  
  19. //当前子弹已经飞行的距离
  20. private float CurrFlyDistance = 0;
  21.  
  22. public Bullet(string scenePath, float speed, float maxDistance, Vector2 position, float rotation, uint targetLayer) :
  23. base(scenePath)
  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. public override void _Ready()
  39. {
  40. base._Ready();
  41. //绘制阴影
  42. ShowShadowSprite();
  43. }
  44.  
  45. protected override void PhysicsProcessOver(float delta)
  46. {
  47. //移动
  48. var lastSlideCollision = GetLastSlideCollision();
  49. if (lastSlideCollision != null)
  50. {
  51. //创建粒子特效
  52. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_BulletSmoke_tscn);
  53. var smoke = packedScene.Instantiate<GpuParticles2D>();
  54. smoke.GlobalPosition = lastSlideCollision.GetPosition();
  55. smoke.GlobalRotation = lastSlideCollision.GetNormal().Angle();
  56. smoke.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  57.  
  58. Destroy();
  59. return;
  60. }
  61. //距离太大, 自动销毁
  62. CurrFlyDistance += FlySpeed * delta;
  63. if (CurrFlyDistance >= MaxDistance)
  64. {
  65. Destroy();
  66. }
  67. }
  68.  
  69. private void OnArea2dEntered(Area2D other)
  70. {
  71. var role = other.AsActivityObject<Role>();
  72. if (role != null)
  73. {
  74. role.CallDeferred(nameof(Role.Hurt), 4, Rotation);
  75. Destroy();
  76. }
  77. }
  78. }