Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / weapon / knife / Knife.cs
@小李xl 小李xl on 4 Jul 2023 2 KB 优化相机
  1.  
  2. using Godot;
  3.  
  4. [Tool]
  5. public partial class Knife : Weapon
  6. {
  7. private Area2D _hitArea;
  8. private int _attackIndex = 0;
  9. public override void OnInit()
  10. {
  11. base.OnInit();
  12. _hitArea = GetNode<Area2D>("HitArea");
  13. _hitArea.Monitoring = false;
  14. _hitArea.Monitorable = false;
  15. _hitArea.BodyEntered += OnBodyEntered;
  16. //禁用自动播放动画
  17. IsAutoPlaySpriteFrames = false;
  18. }
  19.  
  20. protected override void Process(float delta)
  21. {
  22. base.Process(delta);
  23. if (IsActive)
  24. {
  25. //让碰撞节点与武器挂载节点位置保持一致, 而不跟着武器走
  26. _hitArea.GlobalPosition = Master.MountPoint.GlobalPosition;
  27. }
  28. }
  29.  
  30. protected override void PhysicsProcess(float delta)
  31. {
  32. base.PhysicsProcess(delta);
  33. //过去两个物理帧后就能关闭碰撞了
  34. if (++_attackIndex >= 2)
  35. {
  36. _hitArea.Monitoring = false;
  37. }
  38. }
  39.  
  40. protected override void OnBeginCharge()
  41. {
  42. //开始蓄力时武器角度上抬120度
  43. RotationDegrees = -120;
  44. }
  45.  
  46. protected override void OnFire()
  47. {
  48. GD.Print("近战武器攻击! 蓄力时长: " + GetTriggerChargeTime() + ", 扳机按下时长: " + GetTriggerDownTime());
  49. //更新碰撞层级
  50. _hitArea.CollisionMask = GetAttackLayer();
  51. //启用碰撞
  52. _hitArea.Monitoring = true;
  53. _attackIndex = 0;
  54.  
  55. if (IsActive) //被使用
  56. {
  57. //播放挥刀特效
  58. SpecialEffectManager.Play(
  59. ResourcePath.resource_spriteFrames_effect_KnifeHit1_tres, "default",
  60. Master.MountPoint.GlobalPosition, GlobalRotation + Mathf.Pi * 0.5f, new Vector2((int)Master.Face, 1) * AnimatedSprite.Scale,
  61. new Vector2(17, 4), 1
  62. );
  63. }
  64.  
  65.  
  66. if (Master == Player.Current)
  67. {
  68. //创建抖动
  69. //GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation - Mathf.Pi * 0.5f) * 1.5f);
  70. }
  71. }
  72.  
  73. protected override void OnShoot(float fireRotation)
  74. {
  75. }
  76.  
  77. protected override int UseAmmoCount()
  78. {
  79. //这里要做判断, 如果没有碰到敌人, 则不消耗弹药 (耐久)
  80. return 0;
  81. }
  82.  
  83. private void OnBodyEntered(Node2D body)
  84. {
  85. GD.Print("碰到物体: " + body.Name);
  86. var activityObject = body.AsActivityObject();
  87. if (activityObject != null)
  88. {
  89. if (activityObject is Role role)
  90. {
  91. role.CallDeferred(nameof(Role.Hurt),
  92. Utils.RandomRangeInt(Attribute.BulletMinHarm, Attribute.BulletMaxHarm), (role.GetCenterPosition() - GlobalPosition).Angle());
  93. }
  94. }
  95. }
  96. }