GamePlayerInputAssistance.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using Core;
  3. using LitJson;
  4. using UnityEngine;
  5. public class GamePlayerInputAssistance : BaseBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.pAttr = R.Player.Attribute;
  10. this.pAction = base.GetComponent<PlayerAction>();
  11. base.GetComponent<StateMachine>().OnTransfer += this.OnMyStateTransfer;
  12. this.atkData = JsonMapper.ToObject(this.atkParm.text);
  13. }
  14. private void Update()
  15. {
  16. if (!InputSetting.Assistant)
  17. {
  18. return;
  19. }
  20. if (!this.openAssistant)
  21. {
  22. return;
  23. }
  24. GameObject nearestAim = this.GetNearestAim(this.assisData.assistanceCheckType);
  25. if (nearestAim != null)
  26. {
  27. this.aimObj = nearestAim;
  28. }
  29. if (this.aimObj == null)
  30. {
  31. return;
  32. }
  33. Vector2 vector = this.GetAssistanceSpeed();
  34. bool flag = InputSetting.JudgeDir(base.transform.position.x, this.aimObj.transform.position.x) * this.pAttr.faceDir > 0;
  35. bool flag2 = (this.pAttr.faceDir != -1) ? Core.Input.Game.MoveRight.Pressed : Core.Input.Game.MoveLeft.Pressed;
  36. if (flag)
  37. {
  38. if (flag2)
  39. {
  40. this.MovePlayer(vector);
  41. }
  42. else
  43. {
  44. vector = ((!this.pAttr.isOnGround) ? (vector / 2f) : Vector2.zero);
  45. this.MovePlayer(vector);
  46. }
  47. }
  48. }
  49. private void OnMyStateTransfer(object sender, StateMachine.TransferEventArgs args)
  50. {
  51. if (this.atkData.Contains(args.nextState))
  52. {
  53. JsonData data = this.atkData[args.nextState];
  54. this.assisData = new GamePlayerInputAssistance.InputAssistanceData(data);
  55. if (this.assisData.assistanceCheckType != GamePlayerInputAssistance.InputAssistanceData.CheckType.None)
  56. {
  57. this.StartAssistance();
  58. }
  59. else
  60. {
  61. this.StopAssistance();
  62. }
  63. }
  64. else
  65. {
  66. this.StopAssistance();
  67. }
  68. }
  69. private void MovePlayer(Vector2 speed)
  70. {
  71. GamePlayerInputAssistance.InputAssistanceData.AssistanceType assistanceType = this.assisData.assistanceType;
  72. if (assistanceType != GamePlayerInputAssistance.InputAssistanceData.AssistanceType.X)
  73. {
  74. if (assistanceType != GamePlayerInputAssistance.InputAssistanceData.AssistanceType.Y)
  75. {
  76. if (assistanceType == GamePlayerInputAssistance.InputAssistanceData.AssistanceType.XY)
  77. {
  78. speed.y = Mathf.Clamp(speed.y, float.MinValue, 0f);
  79. }
  80. }
  81. else
  82. {
  83. speed.y = Mathf.Clamp(speed.y, float.MinValue, 0f);
  84. speed.x = R.Player.TimeController.GetCurrentSpeed().x;
  85. }
  86. }
  87. else
  88. {
  89. speed.y = 0f;
  90. }
  91. this.pAction.pab.move.addSpeed = speed;
  92. }
  93. private Vector2 GetAssistanceSpeed()
  94. {
  95. if (this.aimObj != null)
  96. {
  97. float num = Vector3.Distance(this.aimObj.transform.position, base.transform.position);
  98. num -= this.aimObj.GetComponent<EnemyAttribute>().bounds.size.x - 1f;
  99. num = Mathf.Clamp(num, 0f, float.MaxValue);
  100. Vector2 a = (this.aimObj.transform.position - base.transform.position).normalized;
  101. Vector2 result = Vector2.zero;
  102. if (this.pAttr.isOnGround)
  103. {
  104. if (num <= 4f)
  105. {
  106. result = 0.8f * a * num * num;
  107. }
  108. }
  109. else if (num <= 8f && num > 3f)
  110. {
  111. result = 0.5f * a * num * num;
  112. }
  113. return result;
  114. }
  115. return Vector2.zero;
  116. }
  117. public void StartAssistance()
  118. {
  119. GameObject nearestAim = this.GetNearestAim(this.assisData.assistanceCheckType);
  120. if (nearestAim != null)
  121. {
  122. this.aimObj = nearestAim;
  123. this.openAssistant = true;
  124. }
  125. else
  126. {
  127. this.StopAssistance();
  128. }
  129. }
  130. public void StopAssistance()
  131. {
  132. this.openAssistant = false;
  133. this.pAction.pab.move.addSpeed = Vector2.zero;
  134. }
  135. private GameObject GetNearestAim(GamePlayerInputAssistance.InputAssistanceData.CheckType _animType)
  136. {
  137. GameObject nearestRangeEnemyWithDir = R.Enemy.GetNearestRangeEnemyWithDir(base.transform.position + new Vector3((float)(this.pAttr.faceDir * 3), 2f, 0f), 6f, 6f, this.pAttr.faceDir, true, true);
  138. GameObject nearestRangeEnemyWithDir2 = R.Enemy.GetNearestRangeEnemyWithDir(base.transform.position + new Vector3((float)(this.pAttr.faceDir * 3), 2f, 0f), 6f, 6f, this.pAttr.faceDir, true, false);
  139. switch (_animType)
  140. {
  141. case GamePlayerInputAssistance.InputAssistanceData.CheckType.GroundOnly:
  142. return nearestRangeEnemyWithDir;
  143. case GamePlayerInputAssistance.InputAssistanceData.CheckType.AirOnly:
  144. return nearestRangeEnemyWithDir2;
  145. case GamePlayerInputAssistance.InputAssistanceData.CheckType.GroundFirst:
  146. return (!(nearestRangeEnemyWithDir == null)) ? nearestRangeEnemyWithDir : nearestRangeEnemyWithDir2;
  147. case GamePlayerInputAssistance.InputAssistanceData.CheckType.AirFirst:
  148. return (!(nearestRangeEnemyWithDir2 == null)) ? nearestRangeEnemyWithDir2 : nearestRangeEnemyWithDir;
  149. case GamePlayerInputAssistance.InputAssistanceData.CheckType.DisFirst:
  150. {
  151. if (nearestRangeEnemyWithDir == null)
  152. {
  153. return nearestRangeEnemyWithDir2;
  154. }
  155. if (nearestRangeEnemyWithDir2 == null)
  156. {
  157. return nearestRangeEnemyWithDir;
  158. }
  159. float num = Vector2.Distance(base.transform.position, nearestRangeEnemyWithDir.transform.position);
  160. float num2 = Vector2.Distance(base.transform.position, nearestRangeEnemyWithDir2.transform.position);
  161. return (num >= num2) ? nearestRangeEnemyWithDir2 : nearestRangeEnemyWithDir;
  162. }
  163. default:
  164. return null;
  165. }
  166. }
  167. private void OnDrawGizmos()
  168. {
  169. Rect rect = new Rect(this.localJudgeRect);
  170. rect.min = this.LocalToWorldDir(rect.min);
  171. rect.max = this.LocalToWorldDir(rect.max);
  172. rect.position += (Vector2)base.transform.position;
  173. DebugX.DrawRect(rect, this.gizmosColor, 0f);
  174. }
  175. private Vector2 LocalToWorldDir(Vector2 loc)
  176. {
  177. Vector2 result = Vector2.Scale(loc, base.transform.localScale);
  178. result.Scale(new Vector2(-1f, 1f));
  179. return result;
  180. }
  181. private PlayerAttribute pAttr;
  182. private PlayerAction pAction;
  183. public Rect localJudgeRect = new Rect(-0.5f, -2.5f, 6f, 5f);
  184. public GameObject aimObj;
  185. private bool openAssistant;
  186. [SerializeField]
  187. private TextAsset atkParm;
  188. private JsonData atkData;
  189. private GamePlayerInputAssistance.InputAssistanceData assisData;
  190. private Color gizmosColor = Color.blue;
  191. private Color gizmosOffsetPointColor = Color.blue;
  192. public class InputAssistanceData
  193. {
  194. public InputAssistanceData(JsonData data)
  195. {
  196. this.assistanceType = (GamePlayerInputAssistance.InputAssistanceData.AssistanceType)data.Get<int>("assistanceType", 0);
  197. this.assistanceCheckType = (GamePlayerInputAssistance.InputAssistanceData.CheckType)data.Get<int>("assistanceCheckType", 0);
  198. this.autoTurn = data.Get<bool>("autoTurn", false);
  199. this.keepDis = data.Get<bool>("keepDis", false);
  200. }
  201. private InputAssistanceData()
  202. {
  203. }
  204. public GamePlayerInputAssistance.InputAssistanceData.AssistanceType assistanceType;
  205. public GamePlayerInputAssistance.InputAssistanceData.CheckType assistanceCheckType;
  206. public bool autoTurn;
  207. public bool keepDis;
  208. public enum AssistanceType
  209. {
  210. X,
  211. Y,
  212. XY
  213. }
  214. public enum CheckType
  215. {
  216. None,
  217. GroundOnly,
  218. AirOnly,
  219. GroundFirst,
  220. AirFirst,
  221. DisFirst
  222. }
  223. }
  224. }