SoftCollidedImpact.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using ExtensionMethods;
  3. using LitJson;
  4. using UnityEngine;
  5. public class SoftCollidedImpact : BaseBehaviour
  6. {
  7. private void Start()
  8. {
  9. this._parent = base.transform.parent;
  10. this.colliderSize = base.GetComponent<Collider2D>().bounds.size;
  11. this._physics = this._parent.GetComponent<IPlatformPhysics>();
  12. this._defautValue = this.defautValue;
  13. this.isOpen = this._defautValue;
  14. this.state = this._parent.GetComponent<StateMachine>();
  15. if (this.myType != SoftCollidedImpact.Type.Player)
  16. {
  17. this.EnemyAction = this._parent.GetComponent<EnemyBaseAction>();
  18. }
  19. if (this.softCollidedParam != null)
  20. {
  21. this.softCollidedData = JsonMapper.ToObject(this.softCollidedParam.text);
  22. }
  23. this.state.OnTransfer += this.OnStateTransfer;
  24. }
  25. private void OnStateTransfer(object sender, StateMachine.TransferEventArgs args)
  26. {
  27. bool flag = this._defautValue;
  28. if (this.softCollidedData != null && this.softCollidedData.Contains(args.nextState))
  29. {
  30. flag = this.softCollidedData[args.nextState].Get<bool>("isOpen", this._defautValue);
  31. }
  32. this.isOpen = flag;
  33. }
  34. private void FixedUpdate()
  35. {
  36. if (this.center != null && base.transform.localPosition != this.center.localPosition)
  37. {
  38. base.transform.localPosition = this.center.localPosition;
  39. }
  40. int num = Physics2D.OverlapBoxNonAlloc(base.transform.position, this.colliderSize, 0f, this._others, LayerManager.ColliderimpactMask);
  41. if (num <= 1)
  42. {
  43. return;
  44. }
  45. float num2 = 0f;
  46. for (int i = 0; i < num; i++)
  47. {
  48. SoftCollidedImpact component = this._others[i].GetComponent<SoftCollidedImpact>();
  49. if (!(this._others[i].transform == base.transform) && this.Active(component))
  50. {
  51. if (this.myType == SoftCollidedImpact.Type.Player)
  52. {
  53. num2 = this.Impact(component);
  54. }
  55. else
  56. {
  57. num2 += this.Impact(component);
  58. }
  59. }
  60. }
  61. Vector3 position = this._parent.position;
  62. position.x = this._parent.position.x + num2;
  63. this._parent.position = position;
  64. }
  65. private bool Active(SoftCollidedImpact other)
  66. {
  67. return (this.myType != SoftCollidedImpact.Type.Player) ? this.EnemyEnemyActive(other) : this.PlayerEnemyActive(other);
  68. }
  69. private float Impact(SoftCollidedImpact other)
  70. {
  71. return (this.myType != SoftCollidedImpact.Type.Player) ? this.EnemyEnemyImpact(other) : this.PlayerEnemyImpact(other);
  72. }
  73. private bool PlayerEnemyActive(SoftCollidedImpact other)
  74. {
  75. if (other.myType != SoftCollidedImpact.Type.Player && other.EnemyAction.eAttr.isDead)
  76. {
  77. return false;
  78. }
  79. if (this.softCollidedData != null && this.softCollidedData.Contains(this.state.currentState))
  80. {
  81. return this.isOpen;
  82. }
  83. if (this.state.currentState.IsInArray(PlayerAction.NormalSta))
  84. {
  85. return other.myType != SoftCollidedImpact.Type.Small && other.isOpen;
  86. }
  87. if (this.state.currentState.IsInArray(PlayerAction.FlySta))
  88. {
  89. return other.myType != SoftCollidedImpact.Type.Small && other.isOpen;
  90. }
  91. return this.state.currentState.IsInArray(PlayerAction.AirLightAttackSta) || (this.state.currentState.IsInArray(PlayerAction.AttackSta) && (other.myType != SoftCollidedImpact.Type.Small || other.isOpen));
  92. }
  93. private float PlayerEnemyImpact(SoftCollidedImpact softCollidedImpact)
  94. {
  95. float num = (this.colliderSize.x + softCollidedImpact.colliderSize.x - 0.1f) / 2f;
  96. float num2 = Mathf.Abs(base.transform.position.x - softCollidedImpact.transform.position.x);
  97. if (softCollidedImpact.myType > this.myType)
  98. {
  99. int num3 = InputSetting.JudgeDir(softCollidedImpact.transform.position, base.transform.position);
  100. Vector2 velocity = this._physics.velocity;
  101. if (velocity.x * (float)num3 < 0f)
  102. {
  103. velocity.x = 0f;
  104. this._physics.velocity = velocity;
  105. }
  106. return Mathf.Clamp(num - num2, 0f, float.MaxValue) * (float)num3;
  107. }
  108. if (softCollidedImpact.myType < this.myType)
  109. {
  110. int num4 = InputSetting.JudgeDir(base.transform.position, softCollidedImpact.transform.position);
  111. float num5 = Mathf.Clamp(num - num2, 0f, float.MaxValue) * (float)num4;
  112. Vector3 position = softCollidedImpact._parent.position;
  113. position.x = softCollidedImpact._parent.position.x + num5;
  114. softCollidedImpact._parent.position = position;
  115. Vector2 velocity2 = softCollidedImpact._physics.velocity;
  116. if (velocity2.x * (float)num4 < 0f)
  117. {
  118. velocity2.x = 0f;
  119. softCollidedImpact._physics.velocity = velocity2;
  120. }
  121. }
  122. return 0f;
  123. }
  124. private bool EnemyEnemyActive(SoftCollidedImpact other)
  125. {
  126. return other.myType != SoftCollidedImpact.Type.Player && this.EnemyAction.IsInIdle() && other.EnemyAction.IsInIdle();
  127. }
  128. private float EnemyEnemyImpact(SoftCollidedImpact other)
  129. {
  130. float num = (this.colliderSize.x + other.colliderSize.x) / 8f;
  131. float num2 = Mathf.Abs(base.transform.position.x - other.transform.position.x);
  132. if (other.myType > this.myType)
  133. {
  134. int num3 = InputSetting.JudgeDir(other.transform.position, base.transform.position);
  135. return Mathf.Clamp(num - num2, 0f, float.MaxValue) * (float)num3;
  136. }
  137. if (other.myType == this.myType)
  138. {
  139. int num4 = InputSetting.JudgeDir(other.transform.position, base.transform.position);
  140. return Mathf.Clamp(num - num2, 0f, float.MaxValue) * (float)num4 / 2f;
  141. }
  142. return 0f;
  143. }
  144. [Header("是否开启,如果为true则开启排斥功能")]
  145. public bool isOpen = true;
  146. public SoftCollidedImpact.Type myType;
  147. public EnemyBaseAction EnemyAction;
  148. [Header("默认值")]
  149. public bool defautValue;
  150. private bool _defautValue;
  151. private Vector2 colliderSize;
  152. private IPlatformPhysics _physics;
  153. private StateMachine state;
  154. [SerializeField]
  155. private Transform center;
  156. [SerializeField]
  157. private TextAsset softCollidedParam;
  158. private JsonData softCollidedData;
  159. private Collider2D[] _others = new Collider2D[20];
  160. private Transform _parent;
  161. public enum Type
  162. {
  163. Big = 3,
  164. Player = 2,
  165. Medium = 1,
  166. Small = 0
  167. }
  168. }