Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / gun / Gun.cs
@小李xl 小李xl on 27 Aug 2022 4 KB 抽象出ThrowComponent组件
  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 = "res://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. //扳机检测间隔
  27. TriggerInterval = 0f;
  28. //连发数量
  29. MinContinuousCount = 3;
  30. MaxContinuousCount = 3;
  31. //开火前延时
  32. DelayedTime = 0f;
  33. //攻击距离
  34. MinDistance = 500;
  35. MaxDistance = 600;
  36. //发射子弹数量
  37. MinFireBulletCount = 1;
  38. MaxFireBulletCount = 1;
  39. //抬起角度
  40. UpliftAngle = 10;
  41. //枪身长度
  42. FirePosition = new Vector2(16, 1.5f);
  43. }
  44. }
  45.  
  46. //手枪属性数据
  47. private class PistolAttribute : WeaponAttribute
  48. {
  49. public PistolAttribute()
  50. {
  51. Name = "手枪";
  52. Sprite = "res://resource/sprite/gun/gun3.png";
  53. Weight = 20;
  54. CenterPosition = new Vector2(0.4f, -2.6f);
  55. WeightType = WeaponWeightType.DeputyWeapon;
  56. StartFiringSpeed = 300;
  57. StartScatteringRange = 5;
  58. FinalScatteringRange = 60;
  59. ScatteringRangeAddValue = 8f;
  60. ScatteringRangeBackSpeed = 40;
  61. //连发
  62. ContinuousShoot = false;
  63. AmmoCapacity = 12;
  64. MaxAmmoCapacity = 72;
  65. //扳机检测间隔
  66. TriggerInterval = 0.1f;
  67. //连发数量
  68. MinContinuousCount = 1;
  69. MaxContinuousCount = 1;
  70. //开火前延时
  71. DelayedTime = 0f;
  72. //攻击距离
  73. MinDistance = 500;
  74. MaxDistance = 600;
  75. //发射子弹数量
  76. MinFireBulletCount = 1;
  77. MaxFireBulletCount = 1;
  78. //抬起角度
  79. UpliftAngle = 30;
  80. //枪身长度
  81. FirePosition = new Vector2(10, 1.5f);
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// 子弹预制体
  87. /// </summary>
  88. public PackedScene BulletPack;
  89. /// <summary>
  90. /// 弹壳预制体
  91. /// </summary>
  92. public PackedScene ShellPack;
  93.  
  94. public Gun(string id, WeaponAttribute attribute): base(id, attribute)
  95. {
  96. BulletPack = ResourceManager.Load<PackedScene>("res://prefab/weapon/bullet/OrdinaryBullets.tscn");
  97. ShellPack = ResourceManager.Load<PackedScene>("res://prefab/weapon/shell/ShellCase.tscn");
  98. }
  99.  
  100. protected override void OnFire()
  101. {
  102. //创建一个弹壳
  103. var startPos = GlobalPosition + new Vector2(0, 5);
  104. var startHeight = 6;
  105. var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-30, 30) + 180;
  106. var xf = MathUtils.RandRangeInt(20, 60);
  107. var yf = MathUtils.RandRangeInt(60, 120);
  108. var rotate = MathUtils.RandRangeInt(-720, 720);
  109. var sprite = ShellPack.Instance<Sprite>();
  110. sprite.StartThrow<ThrowWeapon>(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, sprite);
  111. //创建抖动
  112. MainCamera.Main.ProssesDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f);
  113. }
  114.  
  115. protected override void OnShoot()
  116. {
  117. //创建子弹
  118. CreateBullet(BulletPack, FirePoint.GlobalPosition, (FirePoint.GlobalPosition - OriginPoint.GlobalPosition).Angle());
  119. }
  120.  
  121. protected override void OnReload()
  122. {
  123. }
  124.  
  125. protected override void OnReloadFinish()
  126. {
  127. }
  128.  
  129. protected override void OnDownTrigger()
  130. {
  131. }
  132.  
  133. protected override void OnUpTrigger()
  134. {
  135. }
  136.  
  137. protected override void OnPickUp(Role master)
  138. {
  139. }
  140.  
  141. protected override void OnThrowOut()
  142. {
  143.  
  144. }
  145.  
  146. protected override void OnActive()
  147. {
  148. }
  149.  
  150. protected override void OnConceal()
  151. {
  152. }
  153.  
  154. }