Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / gun / Gun.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 普通的枪
  5. /// </summary>
  6. [RegisterWeapon(ActivityIdPrefix.Weapon + "0001", typeof(RifleAttribute))]
  7. [RegisterWeapon(ActivityIdPrefix.Weapon + "0003", typeof(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. protected override void OnFire()
  98. {
  99. //创建一个弹壳
  100. var startHeight = 6;
  101. var direction = GlobalRotationDegrees + Utils.RandomRangeInt(-30, 30) + 180;
  102. var xf = Utils.RandomRangeInt(20, 60);
  103. var yf = Utils.RandomRangeInt(60, 120);
  104. var rotate = Utils.RandomRangeInt(-720, 720);
  105. var shell = ActivityObject.Create<ShellCase>(ActivityIdPrefix.Shell + "0001");;
  106. shell.Throw(new Vector2(10, 5), Master.GlobalPosition, startHeight, direction, xf, yf, rotate, true);
  107. if (Master == GameApplication.Instance.RoomManager.Player)
  108. {
  109. //创建抖动
  110. GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
  111. }
  112.  
  113. //创建开火特效
  114. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_ShotFire_tscn);
  115. var sprite = packedScene.Instantiate<Sprite2D>();
  116. sprite.GlobalPosition = FirePoint.GlobalPosition;
  117. sprite.GlobalRotation = FirePoint.GlobalRotation;
  118. sprite.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  119.  
  120. //播放射击音效
  121. SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet2_mp3, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), -8);
  122. }
  123.  
  124. protected override void OnShoot(float fireRotation)
  125. {
  126. //创建子弹
  127. const string bulletId = ActivityIdPrefix.Bullet + "0001";
  128. var bullet = ActivityObject.Create<Bullet>(bulletId);
  129. bullet.Init(
  130. 350,
  131. Utils.RandomRangeFloat(Attribute.MinDistance, Attribute.MaxDistance),
  132. FirePoint.GlobalPosition,
  133. fireRotation,
  134. GetAttackLayer()
  135. );
  136. bullet.PutDown(RoomLayerEnum.YSortLayer);
  137. }
  138. }