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 startPos = Master.GlobalPosition;
  101. var startHeight = 6;
  102. var direction = GlobalRotationDegrees + Utils.RandomRangeInt(-30, 30) + 180;
  103. var verticalSpeed = Utils.RandomRangeInt(60, 120);
  104. var velocity = new Vector2(Utils.RandomRangeInt(20, 60), 0).Rotated(direction * Mathf.Pi / 180);
  105. var rotate = Utils.RandomRangeInt(-720, 720);
  106. var shell = Create<ShellCase>(ActivityIdPrefix.Shell + "0001");
  107. shell.Throw(startPos, startHeight, verticalSpeed, velocity, rotate);
  108. if (Master == GameApplication.Instance.RoomManager.Player)
  109. {
  110. //创建抖动
  111. GameCamera.Main.DirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
  112. }
  113.  
  114. //创建开火特效
  115. var packedScene = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_ShotFire_tscn);
  116. var sprite = packedScene.Instantiate<Sprite2D>();
  117. sprite.GlobalPosition = FirePoint.GlobalPosition;
  118. sprite.GlobalRotation = FirePoint.GlobalRotation;
  119. sprite.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  120.  
  121. //播放射击音效
  122. SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet2_mp3, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), -8);
  123. }
  124.  
  125. protected override void OnShoot(float fireRotation)
  126. {
  127. //创建子弹
  128. const string bulletId = ActivityIdPrefix.Bullet + "0001";
  129. var bullet = ActivityObject.Create<Bullet>(bulletId);
  130. bullet.Init(
  131. 350,
  132. Utils.RandomRangeFloat(Attribute.MinDistance, Attribute.MaxDistance),
  133. FirePoint.GlobalPosition,
  134. fireRotation,
  135. GetAttackLayer()
  136. );
  137. bullet.PutDown(RoomLayerEnum.YSortLayer);
  138. }
  139. }