Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / knife / Knife.cs
@小李xl 小李xl on 16 Nov 2022 3 KB 路径标记点
  1.  
  2. using Godot;
  3.  
  4. [RegisterWeapon("1004", typeof(KnifeAttribute))]
  5. public class Knife : Weapon
  6. {
  7. private class KnifeAttribute : WeaponAttribute
  8. {
  9. public KnifeAttribute()
  10. {
  11. Sprite = 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. //后坐力改为向前, 模拟手伸长的效果
  26. MaxBacklash = -8;
  27. MinBacklash = -8;
  28. BacklashRegressionSpeed = 24;
  29. UpliftAngle = -95;
  30. }
  31. }
  32.  
  33. private Area2D _hitArea;
  34.  
  35. private int _attackIndex = 0;
  36. public Knife(string id, WeaponAttribute attribute) : base(id, attribute)
  37. {
  38. _hitArea = GetNode<Area2D>("HitArea");
  39. _hitArea.Monitoring = false;
  40. _hitArea.Monitorable = false;
  41.  
  42. _hitArea.Connect("body_entered", this, nameof(OnBodyEntered));
  43. }
  44.  
  45. public override void _Process(float delta)
  46. {
  47. base._Process(delta);
  48. if (IsActive)
  49. {
  50. //让碰撞节点与武器挂载节点位置保持一致, 而不跟着武器走
  51. _hitArea.GlobalPosition = Master.MountPoint.GlobalPosition;
  52. }
  53. }
  54.  
  55. public override void _PhysicsProcess(float delta)
  56. {
  57. base._PhysicsProcess(delta);
  58.  
  59. //过去两个物理帧后就能关闭碰撞了
  60. if (++_attackIndex >= 2)
  61. {
  62. _hitArea.Monitoring = false;
  63. }
  64. }
  65.  
  66. protected override void OnStartCharge()
  67. {
  68. //开始蓄力时武器角度上抬120度
  69. RotationDegrees = -120;
  70. }
  71.  
  72. protected override void OnFire()
  73. {
  74. GD.Print("近战武器攻击! 蓄力时长: " + GetTriggerChargeTime() + ", 扳机按下时长: " + GetTriggerDownTime());
  75. //更新碰撞层级
  76. _hitArea.CollisionMask = GetAttackLayer();
  77. //启用碰撞
  78. _hitArea.Monitoring = true;
  79. _attackIndex = 0;
  80.  
  81. if (IsActive) //被使用
  82. {
  83. //播放挥刀特效
  84. SpecialEffectManager.Play(
  85. ResourcePath.resource_effects_KnifeHit1_tres, "default",
  86. Master.MountPoint.GlobalPosition, GlobalRotation + Mathf.Pi * 0.5f, new Vector2((int)Master.Face, 1) * AnimatedSprite.Scale,
  87. new Vector2(17, 4), 1
  88. );
  89. }
  90.  
  91.  
  92. if (Master == GameApplication.Instance.Room.Player)
  93. {
  94. //创建抖动
  95. //GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation - Mathf.Pi * 0.5f) * 1.5f);
  96. }
  97. }
  98.  
  99. protected override void OnShoot(float fireRotation)
  100. {
  101. }
  102.  
  103. protected override int UseAmmoCount()
  104. {
  105. //这里要做判断, 如果没有碰到敌人, 则不消耗弹药 (耐久)
  106. return 0;
  107. }
  108.  
  109. private void OnBodyEntered(Node2D body)
  110. {
  111. GD.Print("碰到物体: " + body.Name);
  112. var activityObject = body.AsActivityObject();
  113. if (activityObject != null)
  114. {
  115. if (activityObject is Role role)
  116. {
  117. role.Hurt(1);
  118. }
  119. }
  120. }
  121. }