Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / gun / Gun.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 普通的枪
  5. /// </summary>
  6. [RegisterWeapon("1001", typeof(Gun.RifleAttribute))]
  7. [RegisterWeapon("1003", typeof(Gun.PistolAttribute))]
  8. public class Gun : Weapon
  9. {
  10. //步枪属性数据
  11. private class RifleAttribute : WeaponAttribute
  12. {
  13. public RifleAttribute()
  14. {
  15. Name = "步枪";
  16. Sprite = ResourcePath.resource_sprite_gun_gun4_png;
  17. Weight = 40;
  18. CenterPosition = new Vector2(0.4f, -2.6f);
  19. StartFiringSpeed = 480;
  20. StartScatteringRange = 30;
  21. FinalScatteringRange = 90;
  22. ScatteringRangeAddValue = 2f;
  23. ScatteringRangeBackSpeed = 40;
  24. //连发
  25. ContinuousShoot = true;
  26. AmmoCapacity = 30;
  27. StandbyAmmoCapacity = 30 * 3;
  28. MaxAmmoCapacity = 30 * 3;
  29. //扳机检测间隔
  30. TriggerInterval = 0f;
  31. //连发数量
  32. MinContinuousCount = 3;
  33. MaxContinuousCount = 3;
  34. //开火前延时
  35. DelayedTime = 0f;
  36. //攻击距离
  37. MinDistance = 300;
  38. MaxDistance = 400;
  39. //发射子弹数量
  40. MinFireBulletCount = 1;
  41. MaxFireBulletCount = 1;
  42. //抬起角度
  43. UpliftAngle = 10;
  44. //开火位置
  45. FirePosition = new Vector2(16, 2);
  46. }
  47. }
  48.  
  49. //手枪属性数据
  50. private class PistolAttribute : WeaponAttribute
  51. {
  52. public PistolAttribute()
  53. {
  54. Name = "手枪";
  55. Sprite = ResourcePath.resource_sprite_gun_gun3_png;
  56. Weight = 20;
  57. CenterPosition = new Vector2(0.4f, -2.6f);
  58. WeightType = WeaponWeightType.DeputyWeapon;
  59. StartFiringSpeed = 300;
  60. StartScatteringRange = 5;
  61. FinalScatteringRange = 60;
  62. ScatteringRangeAddValue = 8f;
  63. ScatteringRangeBackSpeed = 40;
  64. //连发
  65. ContinuousShoot = false;
  66. AmmoCapacity = 12;
  67. StandbyAmmoCapacity = 72;
  68. MaxAmmoCapacity = 72;
  69. //扳机检测间隔
  70. TriggerInterval = 0.1f;
  71. //连发数量
  72. MinContinuousCount = 1;
  73. MaxContinuousCount = 1;
  74. //开火前延时
  75. DelayedTime = 0f;
  76. //攻击距离
  77. MinDistance = 250;
  78. MaxDistance = 300;
  79. //发射子弹数量
  80. MinFireBulletCount = 1;
  81. MaxFireBulletCount = 1;
  82. //抬起角度
  83. UpliftAngle = 30;
  84. //开火位置
  85. FirePosition = new Vector2(10, 2);
  86. }
  87. }
  88.  
  89. public Gun(string typeId, WeaponAttribute attribute): base(typeId, attribute)
  90. {
  91. }
  92.  
  93. protected override void OnFire()
  94. {
  95. //创建一个弹壳
  96. var startHeight = 6;
  97. var direction = GlobalRotationDegrees + Utils.RandRangeInt(-30, 30) + 180;
  98. var xf = Utils.RandRangeInt(20, 60);
  99. var yf = Utils.RandRangeInt(60, 120);
  100. var rotate = Utils.RandRangeInt(-720, 720);
  101. var shell = new ShellCase();
  102. shell.Throw(new Vector2(10, 5), Master.GlobalPosition, startHeight, direction, xf, yf, rotate, true);
  103. if (Master == GameApplication.Instance.Room.Player)
  104. {
  105. //创建抖动
  106. GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
  107. }
  108.  
  109. //创建开火特效
  110. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_ShotFire_tscn);
  111. var sprite = packedScene.Instance<Sprite>();
  112. sprite.GlobalPosition = FirePoint.GlobalPosition;
  113. sprite.GlobalRotation = FirePoint.GlobalRotation;
  114. GameApplication.Instance.Room.GetRoot(true).AddChild(sprite);
  115.  
  116. //播放射击音效
  117. SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet2_mp3, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), -8);
  118. }
  119.  
  120. protected override void OnShoot(float fireRotation)
  121. {
  122. //创建子弹
  123. //CreateBullet(BulletPack, FirePoint.GlobalPosition, fireRotation);
  124. var bullet = new Bullet(
  125. ResourcePath.prefab_weapon_bullet_Bullet_tscn,
  126. 350,
  127. Utils.RandRange(Attribute.MinDistance, Attribute.MaxDistance),
  128. FirePoint.GlobalPosition,
  129. fireRotation,
  130. GetAttackLayer()
  131. );
  132. bullet.PutDown();
  133. }
  134. }