Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / bullet / explode / Explode.cs
@小李xl 小李xl on 2 Nov 2023 2 KB 完成对象池基础功能
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 爆炸
  6. /// </summary>
  7. public partial class Explode : Area2D, IPoolItem
  8. {
  9. public bool IsRecycled { get; set; }
  10. public string Logotype { get; set; }
  11.  
  12. public bool IsDestroyed { get; private set; }
  13.  
  14. /// <summary>
  15. /// 动画播放器
  16. /// </summary>
  17. public AnimationPlayer AnimationPlayer { get; private set; }
  18. /// <summary>
  19. /// 碰撞器
  20. /// </summary>
  21. public CollisionShape2D CollisionShape { get; private set; }
  22. /// <summary>
  23. /// 碰撞器形状对象
  24. /// </summary>
  25. public CircleShape2D CircleShape { get; private set; }
  26.  
  27. /// <summary>
  28. /// 爆炸攻击的层级
  29. /// </summary>
  30. public uint AttackLayer { get; private set; }
  31. /// <summary>
  32. /// 最小伤害
  33. /// </summary>
  34. public int MinHarm { get; private set; }
  35. /// <summary>
  36. /// 最大伤害
  37. /// </summary>
  38. public int MaxHarm { get; private set; }
  39.  
  40. private bool _init = false;
  41. public void Destroy()
  42. {
  43. if (IsDestroyed)
  44. {
  45. return;
  46. }
  47.  
  48. IsDestroyed = true;
  49. QueueFree();
  50. }
  51.  
  52. public void Init(uint attackLayer, float radius, int minHarm, int maxHarm)
  53. {
  54. if (!_init)
  55. {
  56. _init = true;
  57. AnimationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
  58. CollisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
  59. CircleShape = (CircleShape2D)CollisionShape.Shape;
  60. AnimationPlayer.AnimationFinished += OnAnimationFinish;
  61. BodyEntered += OnBodyEntered;
  62. }
  63. AttackLayer = attackLayer;
  64. MinHarm = minHarm;
  65. MaxHarm = maxHarm;
  66. CollisionMask = attackLayer;
  67. CircleShape.Radius = radius;
  68. }
  69. public void RunPlay()
  70. {
  71. GameCamera.Main.CreateShake(new Vector2(6, 6), 0.7f, true);
  72. AnimationPlayer.Play(AnimatorNames.Play);
  73. }
  74.  
  75. public void OnReclaim()
  76. {
  77. GetParent().RemoveChild(this);
  78. }
  79.  
  80. public void OnLeavePool()
  81. {
  82. }
  83.  
  84. private void OnAnimationFinish(StringName name)
  85. {
  86. if (name == AnimatorNames.Play)
  87. {
  88. ObjectPool.Reclaim(this);
  89. }
  90. }
  91.  
  92. private void OnBodyEntered(Node2D node)
  93. {
  94. var role = node.AsActivityObject<Role>();
  95. if (role != null)
  96. {
  97. var angle = (role.Position - Position).Angle();
  98. role.CallDeferred(nameof(role.Hurt), Utils.Random.RandomRangeInt(MinHarm, MaxHarm), angle);
  99. role.MoveController.AddForce(Vector2.FromAngle(angle) * 150, 300);
  100. }
  101. }
  102. }