# Collision

**Collision System Overview:**

The provided code defines an abstract class `CollisionBase` that serves as the foundation for a collision system in a Unity game. This system is designed to handle collision events (enter, stay, exit) involving a specified type of collider (`TCollider`). The implementation includes event handling, collision detection, and collider management.

1. **Generic CollisionBase Class:**
   * This is an abstract class (`CollisionBase<TCollider, TPhysicScene>`) that serves as the base for collision handling.
   * It extends `MonoBehaviour` and implements the `IEditableForEditor` interface.
   * It has three events (`OnCollisionEnter`, `OnCollisionStay`, `OnCollisionExit`) that can be subscribed to for reacting to collisions.
   * It includes properties and methods for managing collision layers, garbage collider size, center position, and physics scenes.
2. **Colliders and Garbage Collection:**
   * The class handles collisions using an array of colliders (`m_garbageColliders`) and a list (`_colliders`) to keep track of colliders involved in collisions.
   * The size of the garbage collider array is set by `GarbageColliderSize`.
   * The `HandleColliderCleaner` method is responsible for cleaning up null colliders from the list and resetting the garbage colliders.
3. **Physics Scene and Layer Management:**
   * The class has a physics scene (`TPhysicScene m_physicsScene`) that is updated using the `UpdatePhysicScene` method.
   * The collision layer is set using the `SetLayer` method, which takes a `LayerMask` parameter.
4. **Collision Handling:**
   * The main collision handling logic is in the `FixedUpdate` method, which is executed at a fixed time interval.
   * The `CalculateCollision` method is abstract and should be implemented by subclasses to define specific collision logic.
   * The class handles new collisions, continued collisions, and collisions exits using the `HandleNewCollisions`, `HandleContinuedCollisions`, and `HandleCollisionsExit` methods, respectively.
5. **Event Handling:**
   * The class provides methods (`HandleCollisionEnter`, `HandleCollisionStay`, `HandleCollisionExit`) to trigger the corresponding events when collisions occur.
6. **Editor Integration:**
   * The class implements an interface (`IEditableForEditor`) to support editor-specific functionality. The `Editable` property is likely used for toggling whether the collision system is editable in the Unity Editor.
7. **Initialization:**
   * The `Awake` method sets the initial capacity of colliders and garbage colliders using the `SetCollidersCapacity` method.
   * The `Start` method initializes the physics scene using `GetPhysicScene`.
8. **Error Handling:**
   * If the physics scene is not set during the `FixedUpdate`, an error message is logged to the console.

**How to Use:**

1. **Inheritance:**
   * Create a class that inherits from `CollisionBase`.
   * Implement the abstract methods (`CalculateCollision` and `GetPhysicScene`) to define specific collision behavior.
2. **Event Handling:**
   * Subscribe to the collision events (`OnCollisionEnter`, `OnCollisionStay`, `OnCollisionExit`) in the derived class to execute custom logic when collisions occur.
3. **Unity Editor Integration:**
   * Utilize the `Editable` property for debugging purposes, making the collision system visually editable in the Unity Editor.
4. **Layer Filtering:**
   * Set the `m_layer` property to filter collisions based on layers.

**Example Usage:**

```csharp
public class MyCollisionSystem : CollisionBase<Collider3D, PhysicsScene>
{
    // Implement abstract methods
    protected override void CalculateCollision()
    {
        // Custom collision calculation logic
    }

    protected override void GetPhysicScene()
    {
        // Custom physics scene retrieval logic
    }
}

public class MyCharacterController : MonoBehaviour
{
    [SerializeField] private MyCollisionSystem _collision;

    // Custom event handling
    private void Awake()
    {
        _collision.OnCollisionEnter += HandleCustomCollisionEnter;
    }
    
    private void HandleCustomCollisionEnter(Collider coll)
    {
        // Custom logic for handling collision enter
    }
}

```

**Benefits:**

* Provides a flexible foundation for implementing collision systems in Unity.
* Offers a modular structure for handling different types of colliders and physics scenes.
* Enables event-driven collision handling for easy customization.

**Considerations:**

* Ensure that the abstract methods are implemented in derived classes to define specific collision behavior.
* Customize the event handling methods (`HandleCollisionEnter`, `HandleCollisionStay`, `HandleCollisionExit`) for the desired behavior in the derived class.
* The system is designed for 3D physics; modifications may be necessary for 2D physics.
