Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / gun / Shotgun.cs
  1. using Godot;
  2.  
  3. [RegisterWeapon(ActivityIdPrefix.Weapon + "0002", typeof(ShotgunAttribute))]
  4. public partial class Shotgun : Weapon
  5. {
  6.  
  7. private class ShotgunAttribute : WeaponAttribute
  8. {
  9. public ShotgunAttribute()
  10. {
  11. Name = "霰弹枪";
  12. Sprite2D = ResourcePath.resource_sprite_gun_gun2_png;
  13. Weight = 40;
  14. CenterPosition = new Vector2(0.4f, -2.6f);
  15. StartFiringSpeed = 400;
  16. StartScatteringRange = 30;
  17. FinalScatteringRange = 90;
  18. ScatteringRangeAddValue = 50f;
  19. ScatteringRangeBackSpeed = 50;
  20. //连发
  21. ContinuousShoot = false;
  22. AmmoCapacity = 7;
  23. StandbyAmmoCapacity = 42;
  24. MaxAmmoCapacity = 42;
  25. AloneReload = true;
  26. AloneReloadCanShoot = true;
  27. ReloadTime = 0.6f;
  28. //连发数量
  29. MinContinuousCount = 1;
  30. MaxContinuousCount = 1;
  31. //开火前延时
  32. DelayedTime = 0f;
  33. //攻击距离
  34. MinDistance = 200;
  35. MaxDistance = 250;
  36. //发射子弹数量
  37. MinFireBulletCount = 5;
  38. MaxFireBulletCount = 5;
  39. //抬起角度
  40. UpliftAngle = 15;
  41. MaxBacklash = 6;
  42. MinBacklash = 5;
  43. //开火位置
  44. FirePosition = new Vector2(18, 4);
  45.  
  46. AiUseAttribute = Clone();
  47. AiUseAttribute.AiTargetLockingTime = 0.2f;
  48. AiUseAttribute.TriggerInterval = 3.5f;
  49. }
  50. }
  51. /// <summary>
  52. /// 弹壳预制体
  53. /// </summary>
  54. public PackedScene ShellPack;
  55.  
  56. public override void OnInit()
  57. {
  58. base.OnInit();
  59. ShellPack = ResourceManager.Load<PackedScene>(ResourcePath.prefab_weapon_shell_ShellCase_tscn);
  60. }
  61.  
  62. protected override void OnFire()
  63. {
  64. //创建一个弹壳
  65. var startPos = Master.GlobalPosition;
  66. var startHeight = 6;
  67. var direction = GlobalRotationDegrees + Utils.RandomRangeInt(-30, 30) + 180;
  68. var verticalSpeed = Utils.RandomRangeInt(60, 120);
  69. var velocity = new Vector2(Utils.RandomRangeInt(20, 60), 0).Rotated(direction * Mathf.Pi / 180);
  70. var rotate = Utils.RandomRangeInt(-720, 720);
  71. var shell = Create<ShellCase>(ActivityIdPrefix.Shell + "0001");
  72. shell.InheritVelocity(Master);
  73. shell.Throw(startPos, startHeight, verticalSpeed, velocity, rotate);
  74. if (Master == GameApplication.Instance.RoomManager.Player)
  75. {
  76. //创建抖动
  77. GameCamera.Main.DirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
  78. }
  79. //创建开火特效
  80. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_ShotFire_tscn);
  81. var sprite = packedScene.Instantiate<Sprite2D>();
  82. sprite.GlobalPosition = FirePoint.GlobalPosition;
  83. sprite.GlobalRotation = FirePoint.GlobalRotation;
  84. sprite.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  85. //播放射击音效
  86. SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet3_mp3, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), -15);
  87. }
  88.  
  89. protected override void OnShoot(float fireRotation)
  90. {
  91. //创建子弹
  92. const string bulletId = ActivityIdPrefix.Bullet + "0001";
  93. var bullet = ActivityObject.Create<Bullet>(bulletId);
  94. bullet.Init(
  95. this,
  96. Utils.RandomRangeInt(280, 380),
  97. Utils.RandomRangeFloat(Attribute.MinDistance, Attribute.MaxDistance),
  98. FirePoint.GlobalPosition,
  99. fireRotation + Utils.RandomRangeFloat(-20 / 180f * Mathf.Pi, 20 / 180f * Mathf.Pi),
  100. GetAttackLayer()
  101. );
  102. bullet.PutDown(RoomLayerEnum.YSortLayer);
  103. }
  104. }