PlatformMovement.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. using System;
  2. using System.Collections.Generic;
  3. using ExtensionMethods;
  4. using UnityEngine;
  5. [RequireComponent(typeof(BoxCollider2D))]
  6. [AddComponentMenu("PlatformMovement 2D/CharacterMotor")]
  7. public class PlatformMovement : BaseBehaviour
  8. {
  9. public LayerMask m_rayLayer
  10. {
  11. get
  12. {
  13. string currentState = R.Player.StateMachine.currentState;
  14. if (currentState.IsInArray(PlayerAction.FlashAttackSta) && R.Player.Attribute.flashLevel == 3)
  15. {
  16. return LayerManager.GroundMask | LayerManager.WallMask;
  17. }
  18. return LayerManager.GroundMask | LayerManager.WallMask | LayerManager.ObstacleMask;
  19. }
  20. }
  21. public PlatformMovement.PlatformControllerState State { get; private set; }
  22. public Vector2 ComputeVelocityOnGround()
  23. {
  24. return this.ProjectOnGround(this.velocity);
  25. }
  26. public Vector2 GetGroundNormal()
  27. {
  28. return this.m_groundNormal;
  29. }
  30. public Vector2 GetGroundNormalLs()
  31. {
  32. return this.m_groundNormalLs;
  33. }
  34. public float GetDistanceToGround()
  35. {
  36. return this.m_distToGround;
  37. }
  38. public GameObject GetGroundGameObject()
  39. {
  40. return this.m_groundGameObject;
  41. }
  42. public Vector2 ScaleOffset
  43. {
  44. get
  45. {
  46. return this.m_scaleOffset;
  47. }
  48. set
  49. {
  50. this.m_scaleOffset = value;
  51. }
  52. }
  53. public Vector2 Scale
  54. {
  55. get
  56. {
  57. return this.m_scale;
  58. }
  59. set
  60. {
  61. this.m_scale = value;
  62. }
  63. }
  64. public Vector2 GetRayPositionWs(PlatformMovement.CharacterRay2D ray)
  65. {
  66. return base.transform.TransformPoint(Vector2.Scale(ray.m_position - this.m_scaleOffset, this.m_scale) + this.m_scaleOffset);
  67. }
  68. private void Awake()
  69. {
  70. this.State = new PlatformMovement.PlatformControllerState();
  71. this.m_boxCollider = base.GetComponent<BoxCollider2D>();
  72. this.rigid = base.GetComponent<Rigidbody2D>();
  73. }
  74. private void Start()
  75. {
  76. this.ClearRuntimeValues();
  77. }
  78. public void ClearRuntimeValues()
  79. {
  80. this.velocity = Vector2.zero;
  81. this.m_curPos = Vector2.zero;
  82. this.m_groundNormal = Vector2.up;
  83. this.m_groundNormalLs = Vector2.up;
  84. this.m_distToGround = 0f;
  85. this.m_scaleOffset = Vector2.zero;
  86. this.m_scale = Vector2.one;
  87. this.m_groundGameObject = null;
  88. this.m_ignoreColliders.Clear();
  89. this.position = this.Position2D;
  90. this.lastFramePosition = this.position;
  91. this.isKinematic = false;
  92. }
  93. public void AddIgnoreCollider(Collider2D collider, float duration)
  94. {
  95. PlatformMovement.IgnoreCollider ignoreCollider = new PlatformMovement.IgnoreCollider();
  96. ignoreCollider.m_collider = collider;
  97. ignoreCollider.m_duration = duration;
  98. ignoreCollider.m_curTime = 0f;
  99. this.m_ignoreColliders.Add(ignoreCollider);
  100. }
  101. public bool IgnoreOnOneWayGround()
  102. {
  103. bool result = false;
  104. foreach (PlatformMovement.CharacterRay2D characterRay2D in this.m_RaysGround)
  105. {
  106. if (characterRay2D.m_hitInfo.m_collider != null && (double)characterRay2D.m_hitInfo.m_penetration < 0.1 && this.IsOneWayGround(characterRay2D.m_hitInfo.m_collider))
  107. {
  108. result = true;
  109. this.AddIgnoreCollider(characterRay2D.m_hitInfo.m_collider, 0.5f);
  110. }
  111. }
  112. return result;
  113. }
  114. private void RefreshIgnoreColliders()
  115. {
  116. for (int i = 0; i < this.m_ignoreColliders.Count; i++)
  117. {
  118. this.m_ignoreColliders[i].m_curTime += this.deltaTime;
  119. }
  120. for (int j = 0; j < this.m_ignoreColliders.Count; j++)
  121. {
  122. if (this.m_ignoreColliders[j].m_duration > 0f && this.m_ignoreColliders[j].m_curTime > this.m_ignoreColliders[j].m_duration)
  123. {
  124. this.m_ignoreColliders.RemoveAt(j);
  125. }
  126. }
  127. }
  128. private bool IsIgnoredCollider(Collider2D collider)
  129. {
  130. for (int i = 0; i < this.m_ignoreColliders.Count; i++)
  131. {
  132. if (this.m_ignoreColliders[i].m_collider == collider)
  133. {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. private Vector2 ProjectOnGround(Vector2 proj)
  140. {
  141. Vector2 vector = Vector2.right;
  142. float f = Vector2.Dot(vector, this.m_groundNormal);
  143. if (Mathf.Abs(f) > 1.401298E-45f)
  144. {
  145. Vector3 rhs = Vector3.Cross(vector, this.m_groundNormal);
  146. vector = Vector3.Cross(this.m_groundNormal, rhs);
  147. vector.Normalize();
  148. }
  149. return vector * Vector2.Dot(proj, vector);
  150. }
  151. private LayerMask BuildRayLayer()
  152. {
  153. LayerMask rayLayer = this.m_rayLayer;
  154. if (this.m_oneWayLayer != 0)
  155. {
  156. rayLayer.value |= this.m_oneWayLayer;
  157. }
  158. if (this.m_Ceiling != 0)
  159. {
  160. rayLayer.value |= this.m_Ceiling;
  161. }
  162. return rayLayer;
  163. }
  164. private Vector2 Position2D
  165. {
  166. get
  167. {
  168. return this.rigid.position;
  169. }
  170. set
  171. {
  172. Vector3 vector = value;
  173. vector.z = base.transform.position.z;
  174. base.transform.position = vector;
  175. }
  176. }
  177. private float deltaTime
  178. {
  179. get
  180. {
  181. return Time.fixedDeltaTime;
  182. }
  183. }
  184. private void FixedUpdate()
  185. {
  186. this.RefreshIgnoreColliders();
  187. this.Move();
  188. this.position = this.Position2D;
  189. this.lastFramePosition = this.position;
  190. }
  191. private void Move()
  192. {
  193. Vector2 positionOffset = this.position - this.lastFramePosition;
  194. Vector2 moveOffset;
  195. if (this.isKinematic)
  196. {
  197. moveOffset = Vector2.zero;
  198. }
  199. else
  200. {
  201. if (this.isGravityActive)
  202. {
  203. this.UpdateMoveGround();
  204. }
  205. moveOffset = this.velocity * this.deltaTime;
  206. }
  207. if (this.detecteCollided)
  208. {
  209. this.UpdateMovePosition(positionOffset);
  210. this.UpdateMoveVelocity(moveOffset);
  211. }
  212. this.UpdateState();
  213. this.UpdateMoveGroundFriction();
  214. if (this.m_DrawRays)
  215. {
  216. Vector3 b = this.rigid.velocity;
  217. UnityEngine.Debug.DrawLine(base.transform.position, base.transform.position + b, Color.green);
  218. }
  219. }
  220. private void UpdateMovePosition(Vector2 positionOffset)
  221. {
  222. this.m_curPos = this.Position2D;
  223. Vector2 zero = Vector2.zero;
  224. this.ClearRaysCollisionInfo(this.m_RaysGround);
  225. this.ClearRaysCollisionInfo(this.m_RaysHead);
  226. this.ClearRaysCollisionInfo(this.m_RaysFront);
  227. this.ClearRaysCollisionInfo(this.m_RaysBack);
  228. bool flag = this.MoveVertical(ref positionOffset, false, true, ref zero);
  229. bool flag2 = this.MoveHorizontal(ref positionOffset, true, true, ref zero);
  230. this.MoveHorizontal(ref positionOffset, false, !flag2, ref zero);
  231. this.MoveVertical(ref positionOffset, true, !flag, ref zero);
  232. this.Position2D = this.m_curPos;
  233. }
  234. private void UpdateMoveVelocity(Vector2 moveOffset)
  235. {
  236. this.m_curPos = this.Position2D;
  237. Vector2 position2D = this.Position2D;
  238. Vector2 zero = Vector2.zero;
  239. this.ClearRaysCollisionInfo(this.m_RaysGround);
  240. this.ClearRaysCollisionInfo(this.m_RaysHead);
  241. this.ClearRaysCollisionInfo(this.m_RaysFront);
  242. this.ClearRaysCollisionInfo(this.m_RaysBack);
  243. bool flag = this.MoveVertical(ref moveOffset, false, true, ref zero);
  244. bool flag2 = this.MoveHorizontal(ref moveOffset, true, true, ref zero);
  245. this.MoveHorizontal(ref moveOffset, false, !flag2, ref zero);
  246. this.MoveVertical(ref moveOffset, true, !flag, ref zero);
  247. if (this.deltaTime > Mathf.Epsilon)
  248. {
  249. this.rigid.velocity = (this.m_curPos - position2D) / this.deltaTime;
  250. this.velocity = (this.m_curPos - position2D - zero) / this.deltaTime;
  251. }
  252. else
  253. {
  254. this.rigid.velocity = Vector2.zero;
  255. this.velocity = Vector2.zero;
  256. }
  257. }
  258. private void UpdateMoveGround()
  259. {
  260. float num = 100f;
  261. float num2 = (!this.State.IsDetectedGround) ? float.MaxValue : this.GetDistanceToGround();
  262. float num3 = 0.01f;
  263. if (num2 > num3)
  264. {
  265. float num4 = (Physics2D.gravity * (this.deltaTime * this.rigid.gravityScale)).y * 1.2f;
  266. if (this.velocity.y > -num)
  267. {
  268. this.velocity.y = Mathf.Max(-num, this.velocity.y + num4);
  269. }
  270. }
  271. }
  272. private void UpdateMoveGroundFriction()
  273. {
  274. PlatformMovement.CharacterRay2D collidedRay = this.GetCollidedRay(this.m_RaysGround);
  275. if (this.State.IsGrounded && collidedRay != null)
  276. {
  277. float num;
  278. if (collidedRay.m_hitInfo.m_material == null)
  279. {
  280. num = 0.8f;
  281. }
  282. else
  283. {
  284. num = collidedRay.m_hitInfo.m_material.m_dynFriction;
  285. }
  286. float b = Mathf.Abs(this.velocity.x) - num * this.deltaTime * 10f;
  287. this.velocity.x = Mathf.Max(0f, b) * Mathf.Sign(this.velocity.x);
  288. }
  289. }
  290. private void UpdateState()
  291. {
  292. bool front = this.GetCollidedRay(this.m_RaysFront) != null;
  293. bool back = this.GetCollidedRay(this.m_RaysBack) != null;
  294. bool head = this.GetCollidedRay(this.m_RaysHead) != null;
  295. bool ground = this.GetCollidedRay(this.m_RaysGround) != null;
  296. this.GetCollidedGroundRay();
  297. this.State.Update(front, back, head, ground);
  298. }
  299. private PlatformMovement.CharacterRay2D GetCollidedRay(PlatformMovement.CharacterRay2D[] rays)
  300. {
  301. foreach (PlatformMovement.CharacterRay2D characterRay2D in rays)
  302. {
  303. if (characterRay2D.m_hitInfo.m_collider != null && characterRay2D.m_hitInfo.m_penetration <= characterRay2D.m_extraDistance)
  304. {
  305. return characterRay2D;
  306. }
  307. }
  308. return null;
  309. }
  310. private PlatformMovement.CharacterRay2D GetCollidedGroundRay()
  311. {
  312. PlatformMovement.CharacterRay2D[] raysGround = this.m_RaysGround;
  313. foreach (PlatformMovement.CharacterRay2D characterRay2D in raysGround)
  314. {
  315. if (characterRay2D.m_hitInfo.m_collider != null && characterRay2D.m_hitInfo.m_penetration <= Mathf.Max(characterRay2D.m_extraDistance, 1f))
  316. {
  317. return characterRay2D;
  318. }
  319. }
  320. return null;
  321. }
  322. private void ClearRaysCollisionInfo(PlatformMovement.CharacterRay2D[] rays)
  323. {
  324. foreach (PlatformMovement.CharacterRay2D characterRay2D in rays)
  325. {
  326. characterRay2D.m_hitInfo.m_collider = null;
  327. characterRay2D.m_hitInfo.m_material = null;
  328. }
  329. }
  330. private bool IsOneWayGround(Collider2D collider)
  331. {
  332. int num = 1 << collider.gameObject.layer;
  333. return (this.m_oneWayLayer.value & num) != 0;
  334. }
  335. private bool IsCeiling(Collider2D collider)
  336. {
  337. int num = 1 << collider.gameObject.layer;
  338. return (this.m_Ceiling.value & num) != 0;
  339. }
  340. private bool MoveVertical(ref Vector2 moveOffset, bool bDown, bool bCorrectPos, ref Vector2 m_posError)
  341. {
  342. bool flag = false;
  343. float num = 0f;
  344. Vector2 vector = (!bDown) ? base.transform.up : (-base.transform.up);
  345. float num2 = Vector2.Dot(moveOffset, vector);
  346. float num3 = Mathf.Abs(num2);
  347. bool flag2 = num2 > 0f;
  348. float num4 = float.MaxValue;
  349. Vector2 position2D = this.Position2D;
  350. this.Position2D = this.m_curPos;
  351. if (bDown)
  352. {
  353. this.State.IsDetectedGround = false;
  354. this.m_groundGameObject = null;
  355. this.m_distToGround = 0f;
  356. }
  357. foreach (PlatformMovement.CharacterRay2D characterRay2D in (!bDown) ? this.m_RaysHead : this.m_RaysGround)
  358. {
  359. float num5 = characterRay2D.m_penetration * Mathf.Abs(base.transform.lossyScale.y) * this.m_scale.y;
  360. float num6 = num5 + Mathf.Max(characterRay2D.m_extraDistance, 1f);
  361. if (flag2)
  362. {
  363. num6 += Mathf.Abs(num2);
  364. }
  365. Vector2 rayPositionWs = this.GetRayPositionWs(characterRay2D);
  366. if (this.m_DrawRays)
  367. {
  368. UnityEngine.Debug.DrawLine(rayPositionWs, rayPositionWs + vector * num6);
  369. }
  370. int num7 = Physics2D.RaycastNonAlloc(rayPositionWs, vector, this.m_rayResults, num6, this.BuildRayLayer());
  371. if (num7 > 0)
  372. {
  373. float num8 = float.MaxValue;
  374. for (int j = 0; j < num7; j++)
  375. {
  376. RaycastHit2D raycastHit2D = this.m_rayResults[j];
  377. if (!(raycastHit2D.rigidbody == this.rigid))
  378. {
  379. if (raycastHit2D.collider != null)
  380. {
  381. if (raycastHit2D.collider.isTrigger || this.IsIgnoredCollider(raycastHit2D.collider))
  382. {
  383. goto IL_3D8;
  384. }
  385. if ((!bDown || (bDown && raycastHit2D.normal.y < 0f)) && this.IsOneWayGround(raycastHit2D.collider))
  386. {
  387. goto IL_3D8;
  388. }
  389. if ((bDown || (!bDown && raycastHit2D.normal.y > 0f)) && this.IsCeiling(raycastHit2D.collider))
  390. {
  391. goto IL_3D8;
  392. }
  393. }
  394. if (raycastHit2D.fraction != 0f)
  395. {
  396. Vector2 vector2 = base.transform.InverseTransformDirection(raycastHit2D.normal);
  397. if ((!bDown || vector2.y > 0f) && (bDown || vector2.y < 0f))
  398. {
  399. if (this.m_DrawRays)
  400. {
  401. UnityEngine.Debug.DrawLine(raycastHit2D.point, raycastHit2D.point + raycastHit2D.normal * 0.1f, Color.red);
  402. }
  403. float num9 = raycastHit2D.fraction * num6;
  404. float num10 = num9 - num5;
  405. if (num10 + this.m_allowedPenetration < num)
  406. {
  407. flag = true;
  408. num = num10;
  409. }
  410. if (flag2)
  411. {
  412. num3 = Mathf.Max(0f, Mathf.Min(num3, num9 - num5));
  413. }
  414. if (bDown)
  415. {
  416. if (num10 < num4)
  417. {
  418. this.m_groundNormal = raycastHit2D.normal;
  419. num4 = num10;
  420. this.m_groundGameObject = raycastHit2D.collider.gameObject;
  421. this.m_distToGround = num10;
  422. }
  423. }
  424. if (raycastHit2D.fraction < num8)
  425. {
  426. num8 = raycastHit2D.fraction;
  427. characterRay2D.m_hitInfo.m_collider = raycastHit2D.collider;
  428. characterRay2D.m_hitInfo.m_normal = raycastHit2D.normal;
  429. characterRay2D.m_hitInfo.m_penetration = num10;
  430. }
  431. }
  432. }
  433. }
  434. IL_3D8:;
  435. }
  436. }
  437. }
  438. Vector2 curPos = this.m_curPos;
  439. if (flag2)
  440. {
  441. moveOffset += vector * (num3 - num2);
  442. this.m_curPos += vector * num3;
  443. }
  444. if (flag && bCorrectPos)
  445. {
  446. Vector2 b = Mathf.Max(num, -this.m_PosErrorMaxVel * this.deltaTime) * vector;
  447. this.m_curPos += b;
  448. m_posError += b;
  449. }
  450. if (bDown && this.State.IsDetectedGround)
  451. {
  452. this.m_groundNormalLs = base.transform.InverseTransformDirection(this.m_groundNormal);
  453. this.m_distToGround += Vector2.Dot(curPos - this.m_curPos, vector);
  454. }
  455. if (bDown)
  456. {
  457. for (int k = 0; k < this.m_RaysGround.Length; k++)
  458. {
  459. PlatformMovement.CharacterRay2D characterRay2D2 = this.m_RaysGround[k];
  460. if (characterRay2D2.m_hitInfo.m_collider)
  461. {
  462. float num11 = Vector2.Dot(curPos - this.m_curPos, vector);
  463. PlatformMovement.CharacterRay2D characterRay2D3 = characterRay2D2;
  464. characterRay2D3.m_hitInfo.m_penetration = characterRay2D3.m_hitInfo.m_penetration + num11;
  465. }
  466. }
  467. }
  468. else
  469. {
  470. for (int l = 0; l < this.m_RaysHead.Length; l++)
  471. {
  472. PlatformMovement.CharacterRay2D characterRay2D4 = this.m_RaysHead[l];
  473. if (characterRay2D4.m_hitInfo.m_collider)
  474. {
  475. PlatformMovement.CharacterRay2D characterRay2D5 = characterRay2D4;
  476. characterRay2D5.m_hitInfo.m_penetration = characterRay2D5.m_hitInfo.m_penetration + Vector2.Dot(curPos - this.m_curPos, vector);
  477. }
  478. }
  479. }
  480. this.Position2D = position2D;
  481. return flag;
  482. }
  483. private bool MoveHorizontal(ref Vector2 moveOffset, bool bFront, bool bCorrectPos, ref Vector2 m_posError)
  484. {
  485. bool flag = false;
  486. float num = 0f;
  487. bool flag2 = (this.m_faceRight && bFront) || (!this.m_faceRight && !bFront);
  488. Vector2 vector = (!flag2) ? (-base.transform.right) : base.transform.right;
  489. float num2 = Vector2.Dot(moveOffset, vector);
  490. bool flag3 = num2 > 0f;
  491. float num3 = Mathf.Abs(num2);
  492. Vector2 position2D = this.Position2D;
  493. this.Position2D = this.m_curPos;
  494. foreach (PlatformMovement.CharacterRay2D characterRay2D in (!bFront) ? this.m_RaysBack : this.m_RaysFront)
  495. {
  496. float num4 = characterRay2D.m_penetration * Mathf.Abs(base.transform.lossyScale.x) * this.m_scale.x;
  497. float num5 = num4 + characterRay2D.m_extraDistance;
  498. if (flag3)
  499. {
  500. num5 += Mathf.Abs(num2);
  501. }
  502. Vector2 rayPositionWs = this.GetRayPositionWs(characterRay2D);
  503. if (this.m_DrawRays)
  504. {
  505. UnityEngine.Debug.DrawLine(rayPositionWs, rayPositionWs + vector * num5);
  506. }
  507. int num6 = Physics2D.RaycastNonAlloc(rayPositionWs, vector, this.m_rayResults, num5, this.BuildRayLayer());
  508. if (num6 > 0)
  509. {
  510. float num7 = float.MaxValue;
  511. for (int j = 0; j < num6; j++)
  512. {
  513. RaycastHit2D raycastHit2D = this.m_rayResults[j];
  514. if (!(raycastHit2D.rigidbody == this.rigid))
  515. {
  516. if (!(raycastHit2D.collider != null) || (!raycastHit2D.collider.isTrigger && !this.IsIgnoredCollider(raycastHit2D.collider)))
  517. {
  518. float num8 = raycastHit2D.fraction * num5;
  519. float num9 = num8 - num4;
  520. APMaterial material = null;
  521. if (raycastHit2D.collider != null)
  522. {
  523. material = raycastHit2D.collider.GetComponent<APMaterial>();
  524. if (this.IsOneWayGround(raycastHit2D.collider))
  525. {
  526. goto IL_384;
  527. }
  528. }
  529. if (raycastHit2D.fraction != 0f)
  530. {
  531. Vector2 vector2 = base.transform.InverseTransformDirection(raycastHit2D.normal);
  532. if ((!flag2 || vector2.x <= 0f) && (flag2 || vector2.x >= 0f))
  533. {
  534. if (this.m_DrawRays)
  535. {
  536. UnityEngine.Debug.DrawLine(raycastHit2D.point, raycastHit2D.point + raycastHit2D.normal * 0.1f, Color.red);
  537. }
  538. if (bFront)
  539. {
  540. this.State.IsDetectedFront = true;
  541. }
  542. else
  543. {
  544. this.State.IsDetectedBack = true;
  545. }
  546. if (num9 + this.m_allowedPenetration < num)
  547. {
  548. flag = true;
  549. num = num9;
  550. }
  551. if (raycastHit2D.fraction < num7)
  552. {
  553. num7 = raycastHit2D.fraction;
  554. characterRay2D.m_hitInfo.m_collider = raycastHit2D.collider;
  555. characterRay2D.m_hitInfo.m_normal = raycastHit2D.normal;
  556. characterRay2D.m_hitInfo.m_penetration = num9;
  557. characterRay2D.m_hitInfo.m_material = material;
  558. }
  559. if (flag3)
  560. {
  561. num3 = Mathf.Min(num3, num8 - num4);
  562. num3 = Mathf.Max(0f, num3);
  563. }
  564. }
  565. }
  566. }
  567. }
  568. IL_384:;
  569. }
  570. }
  571. }
  572. Vector2 curPos = this.m_curPos;
  573. if (flag3)
  574. {
  575. moveOffset += vector * (num3 - num2);
  576. this.m_curPos += vector * num3;
  577. }
  578. if (flag && bCorrectPos)
  579. {
  580. Vector2 b = Mathf.Max(num, -this.m_PosErrorMaxVel * this.deltaTime) * vector;
  581. this.m_curPos += b;
  582. m_posError += b;
  583. }
  584. if (bFront)
  585. {
  586. for (int k = 0; k < this.m_RaysFront.Length; k++)
  587. {
  588. PlatformMovement.CharacterRay2D characterRay2D2 = this.m_RaysFront[k];
  589. if (characterRay2D2.m_hitInfo.m_collider)
  590. {
  591. PlatformMovement.CharacterRay2D characterRay2D3 = characterRay2D2;
  592. characterRay2D3.m_hitInfo.m_penetration = characterRay2D3.m_hitInfo.m_penetration + Vector2.Dot(curPos - this.m_curPos, vector);
  593. }
  594. }
  595. }
  596. else
  597. {
  598. for (int l = 0; l < this.m_RaysBack.Length; l++)
  599. {
  600. PlatformMovement.CharacterRay2D characterRay2D4 = this.m_RaysBack[l];
  601. if (characterRay2D4.m_hitInfo.m_collider)
  602. {
  603. PlatformMovement.CharacterRay2D characterRay2D5 = characterRay2D4;
  604. characterRay2D5.m_hitInfo.m_penetration = characterRay2D5.m_hitInfo.m_penetration + Vector2.Dot(curPos - this.m_curPos, vector);
  605. }
  606. }
  607. }
  608. this.Position2D = position2D;
  609. return flag;
  610. }
  611. public PlatformMovement.CharacterRay2D[] m_RaysGround;
  612. public PlatformMovement.CharacterRay2D[] m_RaysHead;
  613. public PlatformMovement.CharacterRay2D[] m_RaysFront;
  614. public PlatformMovement.CharacterRay2D[] m_RaysBack;
  615. public Vector2 velocity = Vector2.zero;
  616. public bool m_faceRight = true;
  617. public LayerMask m_oneWayLayer = 0;
  618. public LayerMask m_Ceiling = 0;
  619. public bool m_DrawRays;
  620. public PlatformMovement.AutoBuilder m_autoBuilder = new PlatformMovement.AutoBuilder();
  621. public Vector2 position;
  622. public bool isKinematic;
  623. public bool isGravityActive = true;
  624. public bool detecteCollided = true;
  625. private RaycastHit2D[] m_rayResults = new RaycastHit2D[8];
  626. private float m_PosErrorMaxVel = 100f;
  627. private float m_allowedPenetration = 0.01f;
  628. private BoxCollider2D m_boxCollider;
  629. private Rigidbody2D rigid;
  630. private Vector2 m_curPos;
  631. private Vector2 lastFramePosition;
  632. private Vector2 m_scaleOffset = Vector2.zero;
  633. private Vector2 m_scale = Vector2.one;
  634. private Vector2 m_groundNormal;
  635. private Vector2 m_groundNormalLs;
  636. private float m_distToGround;
  637. private GameObject m_groundGameObject;
  638. private List<PlatformMovement.IgnoreCollider> m_ignoreColliders = new List<PlatformMovement.IgnoreCollider>(8);
  639. [Serializable]
  640. public class CharacterRay2D
  641. {
  642. public Vector2 m_position = Vector2.zero;
  643. public float m_penetration;
  644. public float m_extraDistance = 0.1f;
  645. public PlatformMovement.RayHitInfo m_hitInfo;
  646. }
  647. [Serializable]
  648. public struct RayHitInfo
  649. {
  650. public Collider2D m_collider;
  651. public APMaterial m_material;
  652. public Vector2 m_normal;
  653. public float m_penetration;
  654. }
  655. [Serializable]
  656. public class AutoBuilder
  657. {
  658. public int m_rayCountX = 2;
  659. public int m_rayCountY = 3;
  660. public float m_extraDistanceFront = 0.1f;
  661. public float m_extraDistanceBack = 0.1f;
  662. public float m_extraDistanceUp = 0.1f;
  663. public float m_extraDistanceDown = 0.1f;
  664. public Vector2 m_rayXBoxScale = new Vector2(0.9f, 0.6f);
  665. public Vector2 m_rayYBoxScale = new Vector2(0f, 0.8f);
  666. }
  667. private class IgnoreCollider
  668. {
  669. public Collider2D m_collider;
  670. public float m_duration;
  671. public float m_curTime;
  672. }
  673. public class PlatformControllerState
  674. {
  675. public bool HasCollisions
  676. {
  677. get
  678. {
  679. return this.IsDetectedFront || this.IsDetectedBack || this.IsDetectedHead || this.IsDetectedGround;
  680. }
  681. }
  682. public bool JustGotGrounded
  683. {
  684. get
  685. {
  686. return this.IsGrounded && !this.WasGroundedLastFrame;
  687. }
  688. }
  689. public void Update(bool front, bool back, bool head, bool ground)
  690. {
  691. this.WasGroundedLastFrame = this.IsGrounded;
  692. this.IsDetectedFront = front;
  693. this.IsDetectedBack = back;
  694. this.IsDetectedHead = head;
  695. this.IsDetectedGround = ground;
  696. this.IsGrounded = ground;
  697. }
  698. public override string ToString()
  699. {
  700. return string.Format("(controller: r:{0} l:{1} a:{2} b:{3} down-slope:{4} up-slope:{5} angle: {6}", new object[]
  701. {
  702. this.IsDetectedFront,
  703. this.IsDetectedBack,
  704. this.IsDetectedHead,
  705. this.IsDetectedGround,
  706. "无",
  707. "无",
  708. "无"
  709. });
  710. }
  711. public bool IsDetectedFront;
  712. public bool IsDetectedBack;
  713. public bool IsDetectedHead;
  714. public bool IsDetectedGround;
  715. public bool IsGrounded;
  716. public bool WasGroundedLastFrame;
  717. }
  718. }