ANativeColliderBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.IO;
  3. using kt.Utility;
  4. using UnityEngine;
  5. namespace kt.Physics
  6. {
  7. public abstract class ANativeColliderBase : MonoBehaviour
  8. {
  9. public abstract NativeColliderStatus status { get; protected set; }
  10. public virtual Vector3 worldCenter
  11. {
  12. get
  13. {
  14. return base.transform.TransformPoint(this.status.center);
  15. }
  16. }
  17. protected virtual void Awake()
  18. {
  19. NativeColliderStatus status = this.status;
  20. status.onBeforeSerialize = (Action)Delegate.Combine(status.onBeforeSerialize, new Action(delegate()
  21. {
  22. if (this == null)
  23. {
  24. return;
  25. }
  26. this.status.name = base.name;
  27. this.status.parentName = ((!(base.transform.parent != null)) ? string.Empty : base.transform.parent.name);
  28. this.status.localPosition = base.transform.localPosition;
  29. this.status.localRotation = base.transform.localRotation;
  30. this.status.localScale = base.transform.localScale;
  31. }));
  32. NativeColliderStatus status2 = this.status;
  33. status2.onAfterDeserialize = (Action)Delegate.Combine(status2.onAfterDeserialize, new Action(this.SetTransformFromStatus));
  34. }
  35. public virtual bool Collide(ref Vector3 position, float radius, NativeColliderStatus.Bound bound)
  36. {
  37. NativeColliderStatus.Bound bound2 = this.status.bound;
  38. this.status.bound = bound;
  39. bool result = this.Collide(ref position, radius);
  40. this.status.bound = bound2;
  41. return result;
  42. }
  43. public virtual bool Collide(ref Vector3 position, float radius)
  44. {
  45. return false;
  46. }
  47. public bool Collide(ANativeColliderBase collider, out Vector3 normal)
  48. {
  49. normal = Vector3.zero;
  50. switch (collider.status.colliderType)
  51. {
  52. case NativeColliderStatus.ColliderType.Plane:
  53. return this.CollidePlane(collider as NativePlaneCollider, ref normal);
  54. case NativeColliderStatus.ColliderType.Capsule:
  55. return this.CollideCapsule(collider as NativeCapsuleCollider, ref normal);
  56. case NativeColliderStatus.ColliderType.Sphere:
  57. return this.CollideSphere(collider as NativeSphereCollider, ref normal);
  58. case NativeColliderStatus.ColliderType.MaidPropCol:
  59. return this.CollideMaidPropCol(collider as NativeMaidPropCollider, ref normal);
  60. default:
  61. return false;
  62. }
  63. }
  64. protected virtual bool CollidePlane(NativePlaneCollider plane, ref Vector3 normal)
  65. {
  66. bool flag = plane.Collide(this, out normal);
  67. if (flag)
  68. {
  69. normal = -normal;
  70. }
  71. return flag;
  72. }
  73. protected virtual bool CollideCapsule(NativeCapsuleCollider capsule, ref Vector3 normal)
  74. {
  75. return false;
  76. }
  77. protected virtual bool CollideSphere(NativeSphereCollider sphere, ref Vector3 normal)
  78. {
  79. Vector3 worldCenter = sphere.worldCenter;
  80. float radius = sphere.sphereStatus.radius;
  81. bool flag = this.Collide(ref worldCenter, radius, NativeColliderStatus.Bound.Outside);
  82. if (flag)
  83. {
  84. normal = worldCenter - sphere.worldCenter;
  85. }
  86. return flag;
  87. }
  88. protected virtual bool CollideMaidPropCol(NativeMaidPropCollider mp_col, ref Vector3 normal)
  89. {
  90. return this.CollideCapsule(mp_col, ref normal);
  91. }
  92. public float GetLossyScale()
  93. {
  94. return Mathf.Abs(base.transform.lossyScale.x);
  95. }
  96. public abstract void Save(StreamWriter writer);
  97. protected void Save<T>(StreamWriter writer) where T : NativeColliderStatus
  98. {
  99. T t = this.status as T;
  100. string value = JsonUtility.ToJson(t);
  101. writer.Write(value);
  102. }
  103. public abstract void Load(StreamReader reader, Transform parent_search_root);
  104. protected void Load<T>(StreamReader reader, Transform parent_search_root) where T : NativeColliderStatus
  105. {
  106. this.ParentSearchRoot = parent_search_root;
  107. string json = reader.ReadToEnd();
  108. this.status = JsonUtility.FromJson<T>(json);
  109. }
  110. public T GetStatus<T>() where T : NativeColliderStatus
  111. {
  112. return this.status as T;
  113. }
  114. public void SetStatus(NativeColliderStatus status, TBody body)
  115. {
  116. Transform transform = null;
  117. if (body)
  118. {
  119. transform = body.GetBone(status.parentName);
  120. if (transform)
  121. {
  122. transform = transform.parent;
  123. }
  124. }
  125. this.SetStatus(status, transform);
  126. }
  127. public virtual void SetStatus(NativeColliderStatus status, Transform parent_search_root = null)
  128. {
  129. if (status == null)
  130. {
  131. return;
  132. }
  133. this.status = status;
  134. this.ParentSearchRoot = parent_search_root;
  135. this.SetTransformFromStatus();
  136. }
  137. private void SetTransformFromStatus()
  138. {
  139. base.name = this.status.name;
  140. if (this.ParentSearchRoot && !string.IsNullOrEmpty(this.status.parentName))
  141. {
  142. Transform transform = this.ParentSearchRoot.SearchChildByName(this.status.parentName);
  143. if (base.transform.parent != transform)
  144. {
  145. DebugUtility.Assert.IsNotNull<Transform>(transform, "物理体親ボーンがありません。");
  146. base.transform.SetParent(transform, false);
  147. }
  148. }
  149. base.transform.localPosition = this.status.localPosition;
  150. base.transform.localRotation = this.status.localRotation;
  151. base.transform.localScale = this.status.localScale;
  152. }
  153. private Transform ParentSearchRoot;
  154. }
  155. }