using System; using System.IO; using kt.Utility; using UnityEngine; namespace kt.Physics { public abstract class ANativeColliderBase : MonoBehaviour { public abstract NativeColliderStatus status { get; protected set; } public virtual Vector3 worldCenter { get { return base.transform.TransformPoint(this.status.center); } } protected virtual void Awake() { NativeColliderStatus status = this.status; status.onBeforeSerialize = (Action)Delegate.Combine(status.onBeforeSerialize, new Action(delegate() { if (this == null) { return; } this.status.name = base.name; this.status.parentName = ((!(base.transform.parent != null)) ? string.Empty : base.transform.parent.name); this.status.localPosition = base.transform.localPosition; this.status.localRotation = base.transform.localRotation; this.status.localScale = base.transform.localScale; })); NativeColliderStatus status2 = this.status; status2.onAfterDeserialize = (Action)Delegate.Combine(status2.onAfterDeserialize, new Action(this.SetTransformFromStatus)); } public virtual bool Collide(ref Vector3 position, float radius, NativeColliderStatus.Bound bound) { NativeColliderStatus.Bound bound2 = this.status.bound; this.status.bound = bound; bool result = this.Collide(ref position, radius); this.status.bound = bound2; return result; } public virtual bool Collide(ref Vector3 position, float radius) { return false; } public bool Collide(ANativeColliderBase collider, out Vector3 normal) { normal = Vector3.zero; switch (collider.status.colliderType) { case NativeColliderStatus.ColliderType.Plane: return this.CollidePlane(collider as NativePlaneCollider, ref normal); case NativeColliderStatus.ColliderType.Capsule: return this.CollideCapsule(collider as NativeCapsuleCollider, ref normal); case NativeColliderStatus.ColliderType.Sphere: return this.CollideSphere(collider as NativeSphereCollider, ref normal); case NativeColliderStatus.ColliderType.MaidPropCol: return this.CollideMaidPropCol(collider as NativeMaidPropCollider, ref normal); default: return false; } } protected virtual bool CollidePlane(NativePlaneCollider plane, ref Vector3 normal) { bool flag = plane.Collide(this, out normal); if (flag) { normal = -normal; } return flag; } protected virtual bool CollideCapsule(NativeCapsuleCollider capsule, ref Vector3 normal) { return false; } protected virtual bool CollideSphere(NativeSphereCollider sphere, ref Vector3 normal) { Vector3 worldCenter = sphere.worldCenter; float radius = sphere.sphereStatus.radius; bool flag = this.Collide(ref worldCenter, radius, NativeColliderStatus.Bound.Outside); if (flag) { normal = worldCenter - sphere.worldCenter; } return flag; } protected virtual bool CollideMaidPropCol(NativeMaidPropCollider mp_col, ref Vector3 normal) { return this.CollideCapsule(mp_col, ref normal); } public float GetLossyScale() { return Mathf.Abs(base.transform.lossyScale.x); } public abstract void Save(StreamWriter writer); protected void Save(StreamWriter writer) where T : NativeColliderStatus { T t = this.status as T; string value = JsonUtility.ToJson(t); writer.Write(value); } public abstract void Load(StreamReader reader, Transform parent_search_root); protected void Load(StreamReader reader, Transform parent_search_root) where T : NativeColliderStatus { this.ParentSearchRoot = parent_search_root; string json = reader.ReadToEnd(); this.status = JsonUtility.FromJson(json); } public T GetStatus() where T : NativeColliderStatus { return this.status as T; } public void SetStatus(NativeColliderStatus status, TBody body) { Transform transform = null; if (body) { transform = body.GetBone(status.parentName); if (transform) { transform = transform.parent; } } this.SetStatus(status, transform); } public virtual void SetStatus(NativeColliderStatus status, Transform parent_search_root = null) { if (status == null) { return; } this.status = status; this.ParentSearchRoot = parent_search_root; this.SetTransformFromStatus(); } private void SetTransformFromStatus() { base.name = this.status.name; if (this.ParentSearchRoot && !string.IsNullOrEmpty(this.status.parentName)) { Transform transform = this.ParentSearchRoot.SearchChildByName(this.status.parentName); if (base.transform.parent != transform) { DebugUtility.Assert.IsNotNull(transform, "物理体親ボーンがありません。"); base.transform.SetParent(transform, false); } } base.transform.localPosition = this.status.localPosition; base.transform.localRotation = this.status.localRotation; base.transform.localScale = this.status.localScale; } private Transform ParentSearchRoot; } }