EnemyAtk.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using GameWorld;
  3. using LitJson;
  4. using UnityEngine;
  5. public class EnemyAtk : BaseBehaviour
  6. {
  7. public JsonData atkData
  8. {
  9. private get
  10. {
  11. return this._atkData;
  12. }
  13. set
  14. {
  15. this._atkData = value;
  16. this.hitTimes = value.Get<int>("hitTimes", 0);
  17. this.hitInterval = value.Get<float>("hitInterval", 0f);
  18. this.hitType = value.Get<int>("hitType", 1);
  19. }
  20. }
  21. private void Start()
  22. {
  23. this.eAttr = base.transform.parent.GetComponent<EnemyAttribute>();
  24. }
  25. private void Update()
  26. {
  27. if (this.atkStart)
  28. {
  29. this.hitInterval = Mathf.Clamp(this.hitInterval - Time.deltaTime, 0f, float.PositiveInfinity);
  30. }
  31. }
  32. private void OnTriggerEnter2D(Collider2D other)
  33. {
  34. if (other.name == "PlayerHurtBox")
  35. {
  36. PlayerHurtAtkEventArgs args = new PlayerHurtAtkEventArgs(other.transform.parent.gameObject, base.transform.parent.gameObject, base.transform.parent.gameObject, this.eAttr.atk, this.atkId, this.atkData, false);
  37. EventManager.PostEvent<Transform, PlayerHurtAtkEventArgs>("PlayerHurtAtk", base.transform, args);
  38. this.atkStart = true;
  39. }
  40. }
  41. private void OnTriggerStay2D(Collider2D other)
  42. {
  43. if (SingletonMono<WorldTime>.Instance.IsFrozen)
  44. {
  45. return;
  46. }
  47. if (this.atkData == null)
  48. {
  49. return;
  50. }
  51. if (other.name == "PlayerHurtBox")
  52. {
  53. if (this.hitType == 0)
  54. {
  55. this.UnlimitedAttack(other);
  56. }
  57. else if (this.hitType == 1)
  58. {
  59. this.LimitedAttack(other);
  60. }
  61. }
  62. }
  63. private void OnTriggerExit2D(Collider2D other)
  64. {
  65. if (other.name == "PlayerHurtBox")
  66. {
  67. this.atkStart = false;
  68. }
  69. }
  70. private void UnlimitedAttack(Collider2D other)
  71. {
  72. if (this.hitInterval <= 0f)
  73. {
  74. this.atkId = Incrementor.GetNextId();
  75. this.hitInterval = this.atkData.Get<float>("hitInterval", 0f);
  76. PlayerHurtAtkEventArgs args = new PlayerHurtAtkEventArgs(other.transform.parent.gameObject, base.transform.parent.gameObject, base.transform.parent.gameObject, this.eAttr.atk, this.atkId, this.atkData, false);
  77. EventManager.PostEvent<Transform, PlayerHurtAtkEventArgs>("PlayerHurtAtk", base.transform, args);
  78. }
  79. }
  80. private void LimitedAttack(Collider2D other)
  81. {
  82. if (this.hitTimes > 0)
  83. {
  84. this.hitInterval -= Time.deltaTime;
  85. if (this.hitInterval <= 0f)
  86. {
  87. this.atkId = Incrementor.GetNextId();
  88. this.hitInterval = this.atkData.Get<float>("hitInterval", 0f);
  89. PlayerHurtAtkEventArgs args = new PlayerHurtAtkEventArgs(other.transform.parent.gameObject, base.transform.parent.gameObject, base.transform.parent.gameObject, this.eAttr.atk, this.atkId, this.atkData, false);
  90. EventManager.PostEvent<Transform, PlayerHurtAtkEventArgs>("PlayerHurtAtk", base.transform, args);
  91. this.hitTimes--;
  92. }
  93. }
  94. }
  95. private JsonData _atkData;
  96. private EnemyAttribute eAttr;
  97. public int atkId;
  98. private int hitTimes;
  99. private float hitInterval;
  100. private int hitType;
  101. public bool atkStart;
  102. }