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