Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / knife / Knife.cs
  1.  
  2. using Godot;
  3.  
  4. [RegisterWeapon(ActivityIdPrefix.Weapon + "0004", typeof(KnifeAttribute))]
  5. public partial class Knife : Weapon
  6. {
  7. private class KnifeAttribute : WeaponAttribute
  8. {
  9. public KnifeAttribute()
  10. {
  11. Sprite2D = 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. HoldPosition = 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.  
  46. _hitArea.BodyEntered += OnBodyEntered;
  47. }
  48.  
  49. protected override void Process(float delta)
  50. {
  51. base.Process(delta);
  52. if (IsActive)
  53. {
  54. //让碰撞节点与武器挂载节点位置保持一致, 而不跟着武器走
  55. _hitArea.GlobalPosition = Master.MountPoint.GlobalPosition;
  56. }
  57. }
  58.  
  59. protected override void PhysicsProcess(float delta)
  60. {
  61. base.PhysicsProcess(delta);
  62. //过去两个物理帧后就能关闭碰撞了
  63. if (++_attackIndex >= 2)
  64. {
  65. _hitArea.Monitoring = false;
  66. }
  67. }
  68.  
  69. protected override void OnStartCharge()
  70. {
  71. //开始蓄力时武器角度上抬120度
  72. RotationDegrees = -120;
  73. }
  74.  
  75. protected override void OnFire()
  76. {
  77. GD.Print("近战武器攻击! 蓄力时长: " + GetTriggerChargeTime() + ", 扳机按下时长: " + GetTriggerDownTime());
  78. //更新碰撞层级
  79. _hitArea.CollisionMask = GetAttackLayer();
  80. //启用碰撞
  81. _hitArea.Monitoring = true;
  82. _attackIndex = 0;
  83.  
  84. if (IsActive) //被使用
  85. {
  86. //播放挥刀特效
  87. SpecialEffectManager.Play(
  88. ResourcePath.resource_effects_KnifeHit1_tres, "default",
  89. Master.MountPoint.GlobalPosition, GlobalRotation + Mathf.Pi * 0.5f, new Vector2((int)Master.Face, 1) * AnimatedSprite.Scale,
  90. new Vector2(17, 4), 1
  91. );
  92. }
  93.  
  94.  
  95. if (Master == Player.Current)
  96. {
  97. //创建抖动
  98. //GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation - Mathf.Pi * 0.5f) * 1.5f);
  99. }
  100. }
  101.  
  102. protected override void OnShoot(float fireRotation)
  103. {
  104. }
  105.  
  106. protected override int UseAmmoCount()
  107. {
  108. //这里要做判断, 如果没有碰到敌人, 则不消耗弹药 (耐久)
  109. return 0;
  110. }
  111.  
  112. private void OnBodyEntered(Node2D body)
  113. {
  114. GD.Print("碰到物体: " + body.Name);
  115. var activityObject = body.AsActivityObject();
  116. if (activityObject != null)
  117. {
  118. if (activityObject is Role role)
  119. {
  120. role.CallDeferred(nameof(Role.Hurt), 10, (role.GetCenterPosition() - GlobalPosition).Angle());
  121. }
  122. }
  123. }
  124. }