Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / bullet / explode / Explode.cs
@小李xl 小李xl on 21 Nov 2023 5 KB 添加击退函数
  1.  
  2. using Config;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 爆炸
  7. /// </summary>
  8. public partial class Explode : Area2D, IPoolItem
  9. {
  10. public bool IsRecycled { get; set; }
  11. public string Logotype { get; set; }
  12.  
  13. public bool IsDestroyed { get; private set; }
  14.  
  15. /// <summary>
  16. /// 动画播放器
  17. /// </summary>
  18. public AnimationPlayer AnimationPlayer { get; private set; }
  19. /// <summary>
  20. /// 碰撞器
  21. /// </summary>
  22. public CollisionShape2D CollisionShape { get; private set; }
  23. /// <summary>
  24. /// 碰撞器形状对象
  25. /// </summary>
  26. public CircleShape2D CircleShape { get; private set; }
  27.  
  28. /// <summary>
  29. /// 爆炸攻击的层级
  30. /// </summary>
  31. public uint AttackLayer { get; private set; }
  32.  
  33. /// <summary>
  34. /// 产生爆炸的子弹数据
  35. /// </summary>
  36. public BulletData BulletData { get; private set; }
  37.  
  38. private bool _init = false;
  39. private float _hitRadius;
  40. private int _harm;
  41. private float _repelledRadius;
  42. private float _maxRepelled;
  43. public void Destroy()
  44. {
  45. if (IsDestroyed)
  46. {
  47. return;
  48. }
  49.  
  50. IsDestroyed = true;
  51. QueueFree();
  52. }
  53.  
  54. /// <summary>
  55. /// 初始化爆炸数据
  56. /// </summary>
  57. /// <param name="bulletData">产生爆炸的子弹数据</param>
  58. /// <param name="attackLayer">攻击的层级</param>
  59. /// <param name="hitRadius">伤害半径</param>
  60. /// <param name="harm">造成的伤害</param>
  61. /// <param name="repelledRadius">击退半径</param>
  62. /// <param name="maxRepelled">最大击退速度</param>
  63. public void Init(BulletData bulletData, uint attackLayer, float hitRadius, int harm, float repelledRadius, float maxRepelled)
  64. {
  65. if (!_init)
  66. {
  67. _init = true;
  68. AnimationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
  69. CollisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
  70. CircleShape = (CircleShape2D)CollisionShape.Shape;
  71. AnimationPlayer.AnimationFinished += OnAnimationFinish;
  72. BodyEntered += OnBodyEntered;
  73. }
  74.  
  75. BulletData = bulletData;
  76. AttackLayer = attackLayer;
  77. _hitRadius = hitRadius;
  78. _harm = harm;
  79. _repelledRadius = repelledRadius;
  80. _maxRepelled = maxRepelled;
  81. CollisionMask = attackLayer | PhysicsLayer.Prop | PhysicsLayer.Debris;
  82. CircleShape.Radius = Mathf.Max(hitRadius, maxRepelled);
  83.  
  84. //冲击波
  85. var affiliationArea = bulletData.TriggerRole?.AffiliationArea;
  86. if (affiliationArea != null)
  87. {
  88. ShockWave(affiliationArea);
  89. }
  90. }
  91. /// <summary>
  92. /// 播放爆炸, triggerRole 为触发该爆炸的角色
  93. /// </summary>
  94. public void RunPlay(Role triggerRole = null)
  95. {
  96. GameCamera.Main.CreateShake(new Vector2(6, 6), 0.7f, true);
  97. AnimationPlayer.Play(AnimatorNames.Play);
  98. //播放爆炸音效
  99. SoundManager.PlaySoundByConfig("explosion0002", Position, triggerRole);
  100. }
  101.  
  102. //爆炸冲击波
  103. private void ShockWave(AffiliationArea affiliationArea)
  104. {
  105. var position = Position;
  106. var freezeSprites = affiliationArea.RoomInfo.StaticSprite.CollisionCircle(position, _repelledRadius, true);
  107. foreach (var freezeSprite in freezeSprites)
  108. {
  109. var temp = freezeSprite.Position - position;
  110. freezeSprite.ActivityObject.MoveController.AddForce(temp.Normalized() * _maxRepelled * (_repelledRadius - temp.Length()) / _repelledRadius);
  111. }
  112. }
  113.  
  114. public void OnReclaim()
  115. {
  116. GetParent().CallDeferred(Node.MethodName.RemoveChild, this);
  117. }
  118.  
  119. public void OnLeavePool()
  120. {
  121. }
  122.  
  123. private void OnAnimationFinish(StringName name)
  124. {
  125. if (name == AnimatorNames.Play)
  126. {
  127. ObjectPool.Reclaim(this);
  128. }
  129. }
  130.  
  131. private void OnBodyEntered(Node2D node)
  132. {
  133. var o = node.AsActivityObject();
  134. if (o != null)
  135. {
  136. var temp = o.Position - Position;
  137. var len = temp.Length();
  138. var angle = temp.Angle();
  139.  
  140. if (len <= _hitRadius) //在伤害半径内
  141. {
  142. if (o is Role role) //是角色
  143. {
  144. role.CallDeferred(nameof(role.Hurt), BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole, _harm, angle);
  145. }
  146. else if (o is Bullet bullet) //是子弹
  147. {
  148. if (bullet is BoomBullet boomBullet) //如果是爆炸子弹, 则直接销毁
  149. {
  150. boomBullet.PlayBoom();
  151. }
  152. bullet.Destroy();
  153. return;
  154. }
  155. }
  156. if (len <= _repelledRadius) //击退半径内
  157. {
  158. var repelled = (_repelledRadius - len) / _repelledRadius * _maxRepelled;
  159. //o.MoveController.SetAllVelocity(Vector2.Zero);
  160. o.AddRepelForce(Vector2.FromAngle(angle) * repelled);
  161. }
  162. }
  163. }
  164. }