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 = 300;
  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 _Ready()
  57. {
  58. base._Ready();
  59. ShellPack = ResourceManager.Load<PackedScene>(ResourcePath.prefab_weapon_shell_ShellCase_tscn);
  60. }
  61.  
  62. protected override void OnFire()
  63. {
  64. //创建一个弹壳
  65. var startPos = GlobalPosition + new Vector2(0, 5);
  66. var startHeight = 6;
  67. var direction = GlobalRotationDegrees + Utils.RandomRangeInt(-30, 30) + 180;
  68. var xf = Utils.RandomRangeInt(20, 60);
  69. var yf = Utils.RandomRangeInt(60, 120);
  70. var rotate = Utils.RandomRangeInt(-720, 720);
  71. var shell = ActivityObject.Create<ShellCase>(ActivityIdPrefix.Shell + "0001");;
  72. shell.Throw(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, true);
  73. if (Master == GameApplication.Instance.RoomManager.Player)
  74. {
  75. //创建抖动
  76. GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
  77. }
  78. //创建开火特效
  79. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_ShotFire_tscn);
  80. var sprite = packedScene.Instantiate<Sprite2D>();
  81. sprite.GlobalPosition = FirePoint.GlobalPosition;
  82. sprite.GlobalRotation = FirePoint.GlobalRotation;
  83. sprite.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  84. //播放射击音效
  85. SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet3_mp3, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), -15);
  86. }
  87.  
  88. protected override void OnShoot(float fireRotation)
  89. {
  90. //创建子弹
  91. const string bulletId = ActivityIdPrefix.Bullet + "0001";
  92. var bullet = ActivityObject.Create<Bullet>(bulletId);
  93. bullet.Init(
  94. Utils.RandomRangeInt(280, 380),
  95. Utils.RandomRangeFloat(Attribute.MinDistance, Attribute.MaxDistance),
  96. FirePoint.GlobalPosition,
  97. fireRotation + Utils.RandomRangeFloat(-20 / 180f * Mathf.Pi, 20 / 180f * Mathf.Pi),
  98. GetAttackLayer()
  99. );
  100. bullet.PutDown(RoomLayerEnum.YSortLayer);
  101. }
  102. }