using UnityEngine; namespace Lightbug.Utilities { /// /// This component is an encapsulation of the Collider and Collider2D components, containing the most commonly used /// properties and methods from these components. /// public abstract class ColliderComponent : MonoBehaviour { /// /// The size of the collider. /// public abstract Vector3 Size { get; set; } /// /// The distance between the center of the collider and the position of the object. /// public abstract Vector3 Offset { get; set; } /// /// The collider bounding volume. /// public abstract Vector3 BoundsSize { get; } public Vector3 Center => transform.position + transform.TransformVectorUnscaled(Offset); public static ColliderComponent CreateInstance(GameObject gameObject, bool includeChildren = true) { Collider2D collider2D = includeChildren ? gameObject.GetComponentInChildren() : gameObject.GetComponent(); Collider collider3D = includeChildren ? gameObject.GetComponentInChildren() : gameObject.GetComponent(); if (collider2D != null) { // Box collider ------------------------------------------------------------ BoxCollider2D boxCollider2D = null; try { boxCollider2D = (BoxCollider2D)collider2D; } catch (System.Exception) { } if (boxCollider2D != null) return gameObject.AddComponent(); // Circle collider ------------------------------------------------------------ CircleCollider2D circleCollider2D = null; try { circleCollider2D = (CircleCollider2D)collider2D; } catch (System.Exception) { } if (circleCollider2D != null) return gameObject.AddComponent(); // Capsule collider ------------------------------------------------------------ CapsuleCollider2D capsuleCollider2D = null; try { capsuleCollider2D = (CapsuleCollider2D)collider2D; } catch (System.Exception) { } if (capsuleCollider2D != null) return gameObject.AddComponent(); } else if (collider3D != null) { // Box collider ------------------------------------------------------------ BoxCollider boxCollider3D = null; try { boxCollider3D = (BoxCollider)collider3D; } catch (System.Exception) { } if (boxCollider3D != null) return gameObject.AddComponent(); // Circle collider ------------------------------------------------------------ SphereCollider sphereCollider3D = null; try { sphereCollider3D = (SphereCollider)collider3D; } catch (System.Exception) { } if (sphereCollider3D != null) return gameObject.AddComponent(); // Capsule collider ------------------------------------------------------------ CapsuleCollider capsuleCollider3D = null; try { capsuleCollider3D = (CapsuleCollider)collider3D; } catch (System.Exception) { } if (capsuleCollider3D != null) return gameObject.AddComponent(); } return null; } public delegate void PenetrationDelegate(ref Vector3 bodyPosition, ref Quaternion bodyRotation, Transform otherColliderTransform, Vector3 penetrationDirection, float penetrationDistance); /// /// Calcules the amount of penetration between this body and nearby neighbors. Alternatively, an action (delegate) /// can be passed in, so the resulting position/rotation can be modified if needed. /// /// The position reference. /// The rotation reference. /// This delegate will be called after the penetration value is calculated. /// True if there was any valid overlap. public abstract bool ComputePenetration(ref Vector3 position, ref Quaternion rotation, PenetrationDelegate Action); /// /// Performs an overlap check using the body shape. The filter used in this case corresponds to the internal one from the PhysicsBody2D/3D class. /// If a custom filter is required, 2D/3D implementations must be used instead. /// public abstract int OverlapBody(Vector3 position, Quaternion rotation); protected abstract void OnEnable(); protected abstract void OnDisable(); protected virtual void Awake() { hideFlags = HideFlags.None; } } }