Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / knife / Knife.cs
  1.  
  2. using Godot;
  3.  
  4. [Tool, GlobalClass]
  5. public partial class Knife : Weapon
  6. {
  7. private class KnifeAttribute : WeaponAttribute
  8. {
  9. public KnifeAttribute()
  10. {
  11. Icon = ResourcePath.resource_sprite_gun_knife1_png;
  12. WeaponPrefab = ResourcePath.prefab_weapon_Knife_tscn;
  13. //攻速设置
  14. StartFiringSpeed = 180;
  15. FinalFiringSpeed = StartFiringSpeed;
  16. //关闭连发
  17. ContinuousShoot = false;
  18. //设置成松发开火
  19. LooseShoot = true;
  20. //弹药量, 可以理解为耐久度
  21. AmmoCapacity = 180;
  22. MaxAmmoCapacity = AmmoCapacity;
  23. //握把位置
  24. SpritePosition = new Vector2(10, 0);
  25. MaxDistance = MinDistance = 35;
  26. //后坐力改为向前, 模拟手伸长的效果
  27. MaxBacklash = -8;
  28. MinBacklash = -8;
  29. BacklashRegressionSpeed = 24;
  30. UpliftAngle = -95;
  31.  
  32. //AiUseAttribute = Clone();
  33. //AiUseAttribute.TriggerInterval = 3f;
  34. }
  35. }
  36.  
  37. private Area2D _hitArea;
  38. private int _attackIndex = 0;
  39. public override void OnInit()
  40. {
  41. base.OnInit();
  42. _hitArea = GetNode<Area2D>("HitArea");
  43. _hitArea.Monitoring = false;
  44. _hitArea.Monitorable = false;
  45. _hitArea.BodyEntered += OnBodyEntered;
  46. //禁用自动播放动画
  47. IsAutoPlaySpriteFrames = false;
  48. }
  49.  
  50. protected override void Process(float delta)
  51. {
  52. base.Process(delta);
  53. if (IsActive)
  54. {
  55. //让碰撞节点与武器挂载节点位置保持一致, 而不跟着武器走
  56. _hitArea.GlobalPosition = Master.MountPoint.GlobalPosition;
  57. }
  58. }
  59.  
  60. protected override void PhysicsProcess(float delta)
  61. {
  62. base.PhysicsProcess(delta);
  63. //过去两个物理帧后就能关闭碰撞了
  64. if (++_attackIndex >= 2)
  65. {
  66. _hitArea.Monitoring = false;
  67. }
  68. }
  69.  
  70. protected override void OnStartCharge()
  71. {
  72. //开始蓄力时武器角度上抬120度
  73. RotationDegrees = -120;
  74. }
  75.  
  76. protected override void OnFire()
  77. {
  78. GD.Print("近战武器攻击! 蓄力时长: " + GetTriggerChargeTime() + ", 扳机按下时长: " + GetTriggerDownTime());
  79. //更新碰撞层级
  80. _hitArea.CollisionMask = GetAttackLayer();
  81. //启用碰撞
  82. _hitArea.Monitoring = true;
  83. _attackIndex = 0;
  84.  
  85. if (IsActive) //被使用
  86. {
  87. //播放挥刀特效
  88. SpecialEffectManager.Play(
  89. ResourcePath.resource_spriteFrames_KnifeHit1_tres, "default",
  90. Master.MountPoint.GlobalPosition, GlobalRotation + Mathf.Pi * 0.5f, new Vector2((int)Master.Face, 1) * AnimatedSprite.Scale,
  91. new Vector2(17, 4), 1
  92. );
  93. }
  94.  
  95.  
  96. if (Master == Player.Current)
  97. {
  98. //创建抖动
  99. //GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation - Mathf.Pi * 0.5f) * 1.5f);
  100. }
  101. }
  102.  
  103. protected override void OnShoot(float fireRotation)
  104. {
  105. }
  106.  
  107. protected override int UseAmmoCount()
  108. {
  109. //这里要做判断, 如果没有碰到敌人, 则不消耗弹药 (耐久)
  110. return 0;
  111. }
  112.  
  113. private void OnBodyEntered(Node2D body)
  114. {
  115. GD.Print("碰到物体: " + body.Name);
  116. var activityObject = body.AsActivityObject();
  117. if (activityObject != null)
  118. {
  119. if (activityObject is Role role)
  120. {
  121. role.CallDeferred(nameof(Role.Hurt), 10, (role.GetCenterPosition() - GlobalPosition).Angle());
  122. }
  123. }
  124. }
  125. }