PlayerCollisionChecker.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class PlayerCollisionChecker : MonoBehaviour
  5. {
  6. public void RegisterHandler(IPhysicsEventHandler impl)
  7. {
  8. this.handler = impl;
  9. }
  10. private void Awake()
  11. {
  12. this.myTrans = base.transform;
  13. this.boxPoint1 = new Vector2(this.offset.x - this.size.x / 2f, this.offset.y + this.size.y / 2f);
  14. this.boxPoint2 = new Vector2(this.offset.x + this.size.x / 2f, this.offset.y - this.size.y / 2f);
  15. }
  16. private bool CanReceive(Collider2D collider)
  17. {
  18. foreach (string tag in this.receiveTags)
  19. {
  20. if (collider.gameObject.CompareTag(tag))
  21. {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. private void _FireExit(Collider2D[] colliders, int length)
  28. {
  29. DictionaryEntry[] array = new DictionaryEntry[this.currColliderTable.Count];
  30. this.currColliderTable.CopyTo(array, 0);
  31. foreach (DictionaryEntry dictionaryEntry in array)
  32. {
  33. Collider2D collider2D = (Collider2D)dictionaryEntry.Key;
  34. bool flag = false;
  35. for (int j = 0; j < length; j++)
  36. {
  37. if (colliders[j] == collider2D)
  38. {
  39. flag = true;
  40. if (collider2D.isTrigger)
  41. {
  42. this.handler._OnTriggerStay(collider2D);
  43. }
  44. else
  45. {
  46. this.handler._OnCollisionStay(collider2D);
  47. }
  48. }
  49. }
  50. if (!flag)
  51. {
  52. if (collider2D == null)
  53. {
  54. this.currColliderTable.Remove(collider2D);
  55. }
  56. else
  57. {
  58. if (collider2D.isTrigger)
  59. {
  60. this.handler._OnTriggerExit(collider2D);
  61. }
  62. else
  63. {
  64. this.handler._OnCollisionExit(collider2D);
  65. }
  66. this.currColliderTable.Remove(collider2D);
  67. }
  68. }
  69. }
  70. }
  71. private bool IsHitCollider(Vector2 dir, Collider2D collider, ref Vector2 normal)
  72. {
  73. Vector2 a = this.myTrans.position;
  74. if (this.shape == PlayerCollisionChecker.Shape.box)
  75. {
  76. RaycastHit2D[] array = Physics2D.BoxCastAll(a + this.offset, this.size, 0f, dir, 1f);
  77. foreach (RaycastHit2D raycastHit2D in array)
  78. {
  79. if (raycastHit2D.collider == collider)
  80. {
  81. normal = raycastHit2D.normal;
  82. return true;
  83. }
  84. }
  85. }
  86. else if (this.shape == PlayerCollisionChecker.Shape.circle)
  87. {
  88. RaycastHit2D[] array3 = Physics2D.CircleCastAll(a + this.offset, this.radius, dir, 1f);
  89. foreach (RaycastHit2D raycastHit2D2 in array3)
  90. {
  91. if (raycastHit2D2.collider == collider)
  92. {
  93. normal = raycastHit2D2.normal;
  94. return true;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100. private void _CheckCollison()
  101. {
  102. Vector2 a = this.myTrans.position;
  103. int num = 0;
  104. if (this.shape == PlayerCollisionChecker.Shape.circle)
  105. {
  106. num = Physics2D.OverlapCircleNonAlloc(a + this.offset, this.radius, this.colliders);
  107. }
  108. else if (this.shape == PlayerCollisionChecker.Shape.box)
  109. {
  110. num = Physics2D.OverlapAreaNonAlloc(a + this.boxPoint1, a + this.boxPoint2, this.colliders);
  111. }
  112. this._FireExit(this.colliders, num);
  113. for (int i = 0; i < num; i++)
  114. {
  115. Collider2D collider2D = this.colliders[i];
  116. if (this.currColliderTable[collider2D] == null && this.CanReceive(collider2D))
  117. {
  118. this.currColliderTable.Add(collider2D, collider2D);
  119. if (!collider2D.isTrigger)
  120. {
  121. Vector2 zero = Vector2.zero;
  122. if (!this.IsHitCollider(this.right, collider2D, ref zero))
  123. {
  124. if (!this.IsHitCollider(this.left, collider2D, ref zero))
  125. {
  126. if (!this.IsHitCollider(this.up, collider2D, ref zero))
  127. {
  128. if (this.IsHitCollider(this.down, collider2D, ref zero))
  129. {
  130. }
  131. }
  132. }
  133. }
  134. if (!this.handler._OnCollisionEnter(collider2D, zero))
  135. {
  136. this.currColliderTable.Remove(collider2D);
  137. }
  138. }
  139. else
  140. {
  141. this.handler._OnTriggerEnter(collider2D);
  142. }
  143. }
  144. }
  145. }
  146. private void Update()
  147. {
  148. if (this.handler == null)
  149. {
  150. return;
  151. }
  152. this._CheckCollison();
  153. }
  154. private void OnGUI()
  155. {
  156. if (this.debug)
  157. {
  158. Vector2 a = this.myTrans.position;
  159. a = Camera.main.WorldToViewportPoint(a + this.offset);
  160. a.y = 1f - a.y;
  161. float num = (float)Screen.width / 1920f;
  162. Vector2 center = new Vector2((float)Screen.width * a.x, (float)Screen.height * a.y);
  163. if (this.shape == PlayerCollisionChecker.Shape.box)
  164. {
  165. Vector2 vector = new Vector2(center.x - this.size.x * num / 2f, center.y + this.size.y * num / 2f);
  166. Vector2 vector2 = new Vector2(center.x + this.size.x * num / 2f, center.y + this.size.y * num / 2f);
  167. Vector2 vector3 = new Vector2(center.x + this.size.x * num / 2f, center.y - this.size.y * num / 2f);
  168. Vector2 vector4 = new Vector2(center.x - this.size.x * num / 2f, center.y - this.size.y * num / 2f);
  169. Drawing.DrawLine(vector, vector2, this.debugColor, 2f, false);
  170. Drawing.DrawLine(vector2, vector3, this.debugColor, 2f, false);
  171. Drawing.DrawLine(vector3, vector4, this.debugColor, 2f, false);
  172. Drawing.DrawLine(vector4, vector, this.debugColor, 2f, false);
  173. }
  174. else if (this.shape == PlayerCollisionChecker.Shape.circle)
  175. {
  176. Drawing.DrawCircle(center, (int)(this.radius * num), this.debugColor, 2f, 10);
  177. }
  178. }
  179. }
  180. public float radius;
  181. public Vector2 size;
  182. public Vector2 offset;
  183. public PlayerCollisionChecker.Shape shape;
  184. public string[] receiveTags;
  185. public bool debug;
  186. public Color debugColor = Color.blue;
  187. private Transform myTrans;
  188. private Vector2 boxPoint1;
  189. private Vector2 boxPoint2;
  190. private Collider2D[] colliders = new Collider2D[10];
  191. private Hashtable currColliderTable = new Hashtable();
  192. private IPhysicsEventHandler handler;
  193. private Vector2 right = Vector2.right;
  194. private Vector2 left = -Vector2.right;
  195. private Vector2 up = Vector2.up;
  196. private Vector2 down = -Vector2.up;
  197. public enum Shape
  198. {
  199. circle,
  200. box
  201. }
  202. }