PlayerAtk.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using ExtensionMethods;
  3. using GameWorld;
  4. using LitJson;
  5. using UnityEngine;
  6. public class PlayerAtk : BaseBehaviour
  7. {
  8. private void Start()
  9. {
  10. this._collider = base.GetComponent<Collider2D>();
  11. }
  12. public void SetData(JsonData atkData, int atkId)
  13. {
  14. this.data = atkData;
  15. this.attackId = atkId;
  16. this._hitTimes = this.data.Get<int>("hitTimes", 0);
  17. this._interval = this.data.Get<float>("interval", 100f);
  18. this._hitType = (PlayerAtk.HitType)this.data.Get<int>("hitType", 0);
  19. }
  20. private void OnTriggerEnter2D(Collider2D other)
  21. {
  22. if (other.CompareTag("EnemyHurtBox"))
  23. {
  24. this.EventTrigger(this.EventArgs(other, true));
  25. }
  26. else if (other.CompareTag("EnemyBullet"))
  27. {
  28. this.BreakBullet(other);
  29. }
  30. }
  31. private void OnTriggerStay2D(Collider2D other)
  32. {
  33. if (SingletonMono<WorldTime>.Instance.IsFrozen || this._hitType == PlayerAtk.HitType.Once || this.data == null)
  34. {
  35. return;
  36. }
  37. this._interval -= Time.deltaTime;
  38. if (this._interval > 0f)
  39. {
  40. return;
  41. }
  42. if (other.CompareTag("EnemyHurtBox"))
  43. {
  44. PlayerAtk.HitType hitType = this._hitType;
  45. if (hitType != PlayerAtk.HitType.Limited)
  46. {
  47. if (hitType == PlayerAtk.HitType.UnLimited)
  48. {
  49. this.UnlimitedAttack(other);
  50. }
  51. }
  52. else
  53. {
  54. this.LimitedAttack(other);
  55. }
  56. }
  57. }
  58. private void UnlimitedAttack(Collider2D other)
  59. {
  60. this._interval = this.data.Get<float>("interval", 0f);
  61. this.attackId = Incrementor.GetNextId();
  62. this.EventTrigger(this.EventArgs(other, false));
  63. }
  64. private void LimitedAttack(Collider2D other)
  65. {
  66. if (this._hitTimes > 0)
  67. {
  68. this._interval = this.data.Get<float>("interval", 0f);
  69. this.attackId = Incrementor.GetNextId();
  70. this.EventTrigger(this.EventArgs(other, false));
  71. this._hitTimes--;
  72. }
  73. }
  74. private void BreakBullet(Collider2D bullet)
  75. {
  76. if (R.Player.Action.stateMachine.currentState.IsInArray(PlayerAtkType.CantBreakBullet))
  77. {
  78. return;
  79. }
  80. EnemyBullet component = bullet.GetComponent<EnemyBullet>();
  81. EnemyBulletLaucher component2 = bullet.GetComponent<EnemyBulletLaucher>();
  82. if (component != null)
  83. {
  84. component.beAtked = true;
  85. component.HitBullet();
  86. }
  87. else if (component2 != null)
  88. {
  89. component2.beAtked = true;
  90. component2.HitBullet();
  91. }
  92. SingletonMono<WorldTime>.Instance.TimeFrozenByFixedFrame(7, WorldTime.FrozenArgs.FrozenType.All, true);
  93. R.Camera.Controller.CameraShake(0.166666672f, 0.2f, CameraController.ShakeTypeEnum.Rect, false);
  94. }
  95. private Vector2 HurtPos(Bounds enemyBound)
  96. {
  97. return MathfX.Intersect2DCenter(enemyBound, this._collider.bounds);
  98. }
  99. private EnemyHurtAtkEventArgs EventArgs(Collider2D enemyBody, bool firstHurt)
  100. {
  101. return new EnemyHurtAtkEventArgs(enemyBody.transform.parent.gameObject, base.gameObject, this.attackId, this.HurtPos(enemyBody.bounds), HurtCheck.BodyType.Body, new EnemyHurtAtkEventArgs.PlayerNormalAtkData(this.data, firstHurt), false);
  102. }
  103. private void EventTrigger(EnemyHurtAtkEventArgs args)
  104. {
  105. EventManager.PostEvent<GameObject, EnemyHurtAtkEventArgs>("EnemyHurtAtk", base.gameObject, args);
  106. }
  107. public JsonData data;
  108. public int attackId;
  109. private float _interval;
  110. private int _hitTimes;
  111. private PlayerAtk.HitType _hitType;
  112. private Collider2D _collider;
  113. public enum HitType
  114. {
  115. Once,
  116. Limited,
  117. UnLimited
  118. }
  119. }