Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / weapon / Weapon_Bullet.cs
@小李xl 小李xl on 8 Nov 2023 5 KB 子弹穿透计算属性
  1.  
  2. using System;
  3. using Config;
  4. using Godot;
  5.  
  6. public partial class Weapon
  7. {
  8. //-------------------------------- 子弹相关 -----------------------------
  9.  
  10. /// <summary>
  11. /// 投抛弹壳的默认实现方式, shellId为弹壳id
  12. /// </summary>
  13. protected ActivityObject ThrowShell(ExcelConfig.ActivityBase shell, float speedScale = 1)
  14. {
  15. var startPos = ShellPoint.GlobalPosition;
  16. float startHeight;
  17. if (Master != null)
  18. {
  19. var shellPosition = (Master != null ? Master.MountPoint.Position : Position) + ShellPoint.Position;
  20. startHeight = -shellPosition.Y;
  21. startPos.Y += startHeight;
  22. }
  23. else
  24. {
  25. startHeight = Altitude;
  26. }
  27.  
  28. var direction = GlobalRotationDegrees + Utils.Random.RandomRangeInt(-30, 30) + 180;
  29. var verticalSpeed = Utils.Random.RandomRangeInt((int)(60 * speedScale), (int)(120 * speedScale));
  30. var velocity = new Vector2(Utils.Random.RandomRangeInt((int)(20 * speedScale), (int)(60 * speedScale)), 0).Rotated(direction * Mathf.Pi / 180);
  31. var rotate = Utils.Random.RandomRangeInt((int)(-720 * speedScale), (int)(720 * speedScale));
  32. var shellInstance = Create(shell);
  33. shellInstance.Rotation = (Master != null ? Master.MountPoint.RealRotation : Rotation);
  34. shellInstance.Throw(startPos, startHeight, verticalSpeed, velocity, rotate);
  35. shellInstance.InheritVelocity(Master != null ? Master : this);
  36. if (Master == null)
  37. {
  38. AffiliationArea.InsertItem(shellInstance);
  39. }
  40. else
  41. {
  42. Master.AffiliationArea.InsertItem(shellInstance);
  43. }
  44. return shellInstance;
  45. }
  46.  
  47. /// <summary>
  48. /// 发射子弹
  49. /// </summary>
  50. protected IBullet ShootBullet(float fireRotation, ExcelConfig.BulletBase bullet)
  51. {
  52. if (bullet.Type == 1) //实体子弹
  53. {
  54. return ShootSolidBullet(fireRotation, bullet);
  55. }
  56. else if (bullet.Type == 2) //激光子弹
  57. {
  58. return ShootLaser(fireRotation, bullet);
  59. }
  60.  
  61. return null;
  62. }
  63.  
  64. /// <summary>
  65. /// 发射子弹的默认实现方式
  66. /// </summary>
  67. private Bullet ShootSolidBullet(float fireRotation, ExcelConfig.BulletBase bullet)
  68. {
  69. var data = new BulletData()
  70. {
  71. Weapon = this,
  72. BulletBase = bullet,
  73. TriggerRole = TriggerRole,
  74. Harm = Utils.Random.RandomConfigRange(bullet.HarmRange),
  75. Repel = Utils.Random.RandomConfigRange(bullet.RepelRange),
  76. MaxDistance = Utils.Random.RandomConfigRange(bullet.DistanceRange),
  77. FlySpeed = Utils.Random.RandomConfigRange(bullet.SpeedRange),
  78. VerticalSpeed = Utils.Random.RandomConfigRange(bullet.VerticalSpeed),
  79. BounceCount = Utils.Random.RandomConfigRange(bullet.BounceCount),
  80. Penetration = Utils.Random.RandomConfigRange(bullet.Penetration),
  81. Position = FirePoint.GlobalPosition,
  82. };
  83. var deviationAngle = Utils.Random.RandomConfigRange(bullet.DeviationAngleRange);
  84. if (TriggerRole != null)
  85. {
  86. var roleState = TriggerRole.RoleState;
  87. data.Harm = roleState.CalcDamage(data.Harm);
  88. data.Repel = roleState.CalcBulletRepel(this, data.Repel);
  89. data.FlySpeed = roleState.CalcBulletSpeed(this, data.FlySpeed);
  90. data.MaxDistance = roleState.CalcBulletDistance(this, data.MaxDistance);
  91. data.BounceCount = roleState.CalcBulletBounceCount(this, data.BounceCount);
  92. data.Penetration = roleState.CalcBulletPenetration(this, data.Penetration);
  93. deviationAngle = roleState.CalcBulletDeviationAngle(this, deviationAngle);
  94. if (TriggerRole.IsAi) //只有玩家使用该武器才能获得正常速度的子弹
  95. {
  96. data.FlySpeed *= AiUseAttribute.AiAttackAttr.BulletSpeedScale;
  97. }
  98. }
  99.  
  100. data.Rotation = fireRotation + Mathf.DegToRad(deviationAngle);
  101. //创建子弹
  102. var bulletInstance = ObjectManager.GetBullet(bullet.Prefab);
  103. bulletInstance.InitData(data, GetAttackLayer());
  104. return bulletInstance;
  105. }
  106.  
  107. /// <summary>
  108. /// 发射射线的默认实现方式
  109. /// </summary>
  110. private Laser ShootLaser(float fireRotation, ExcelConfig.BulletBase bullet)
  111. {
  112. var data = new BulletData()
  113. {
  114. Weapon = this,
  115. BulletBase = bullet,
  116. TriggerRole = TriggerRole,
  117. Harm = Utils.Random.RandomConfigRange(bullet.HarmRange),
  118. Repel = Utils.Random.RandomConfigRange(bullet.RepelRange),
  119. MaxDistance = Utils.Random.RandomConfigRange(bullet.DistanceRange),
  120. BounceCount = Utils.Random.RandomConfigRange(bullet.BounceCount),
  121. LifeTime = Utils.Random.RandomConfigRange(bullet.LifeTimeRange),
  122. Position = FirePoint.GlobalPosition,
  123. };
  124.  
  125. var deviationAngle = Utils.Random.RandomConfigRange(bullet.DeviationAngleRange);
  126. if (TriggerRole != null)
  127. {
  128. var roleState = TriggerRole.RoleState;
  129. data.Harm = roleState.CalcDamage(data.Harm);
  130. data.Repel = roleState.CalcBulletRepel(this, data.Repel);
  131. data.BounceCount = roleState.CalcBulletBounceCount(this, data.BounceCount);
  132. deviationAngle = roleState.CalcBulletDeviationAngle(this, deviationAngle);
  133. }
  134.  
  135. data.Rotation = fireRotation + Mathf.DegToRad(deviationAngle);
  136. //创建激光
  137. var laser = ObjectManager.GetLaser(bullet.Prefab);
  138. laser.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  139. laser.InitData(data, GetAttackLayer(), 3);
  140. return laser;
  141. }
  142. }