| | | 1 | | //======================================================================= |
| | | 2 | | // LSCollider.Events.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using Gravitas.CollisionHandling; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.Colliders; |
| | | 12 | | |
| | | 13 | | internal readonly struct ColliderLifetimeToken |
| | | 14 | | { |
| | | 15 | | internal ColliderLifetimeToken(LSCollider collider) |
| | | 16 | | { |
| | 22480 | 17 | | Collider = collider; |
| | 22480 | 18 | | LifetimeVersion = collider.LifetimeVersion; |
| | 22480 | 19 | | } |
| | | 20 | | |
| | | 21 | | internal LSCollider Collider { get; } |
| | | 22 | | |
| | | 23 | | internal long LifetimeVersion { get; } |
| | | 24 | | |
| | 33126 | 25 | | internal bool IsActive => Collider.IsActive |
| | 33126 | 26 | | && !Collider.IsDeactivationInProgress |
| | 33126 | 27 | | && IsCurrentLifetime; |
| | | 28 | | |
| | 52544 | 29 | | internal bool IsCurrentLifetime => Collider.LifetimeVersion == LifetimeVersion; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public abstract partial class LSCollider |
| | | 33 | | { |
| | | 34 | | internal int CollisionPairCount => _pairState.CollisionPairCount; |
| | | 35 | | |
| | | 36 | | internal int CollisionPairHolderCount => _pairState.CollisionPairHolderCount; |
| | | 37 | | |
| | | 38 | | internal SwiftDictionary<int, CollisionPair>? CollisionPairs => _pairState.CollisionPairs; |
| | | 39 | | |
| | | 40 | | internal SwiftHashSet<int>? CollisionPairHolders => _pairState.CollisionPairHolders; |
| | | 41 | | |
| | | 42 | | /// <summary>Handles a 3D contact notification for the other body.</summary> |
| | | 43 | | public delegate void BodyCollisionFunc(SolidBody other); |
| | | 44 | | /// <summary>Raised while this collider is touching another collider that owns a 3D body.</summary> |
| | | 45 | | public event BodyCollisionFunc? OnContact; |
| | | 46 | | /// <summary>Raised on the first simulation frame this collider touches another 3D body.</summary> |
| | | 47 | | public event BodyCollisionFunc? OnContactEnter; |
| | | 48 | | /// <summary>Raised when this collider stops touching another 3D body.</summary> |
| | | 49 | | public event BodyCollisionFunc? OnContactExit; |
| | | 50 | | |
| | | 51 | | /// <summary>Handles a 3D trigger notification for the other collider.</summary> |
| | | 52 | | public delegate void TriggerCollisionFunc(LSCollider other); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Raised on the first simulation frame this collider participates in a valid trigger pair. |
| | | 56 | | /// </summary> |
| | | 57 | | public event TriggerCollisionFunc? OnTriggerEnter; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Raised each simulation frame this collider participates in an overlapped valid trigger pair. |
| | | 61 | | /// </summary> |
| | | 62 | | public event TriggerCollisionFunc? OnTriggerStay; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Raised when this collider stops participating in a valid trigger pair. |
| | | 66 | | /// </summary> |
| | | 67 | | public event TriggerCollisionFunc? OnTriggerExit; |
| | | 68 | | |
| | | 69 | | /// <summary>Handles a mixed contact or trigger notification for the other 2D collider.</summary> |
| | | 70 | | public delegate void MixedCollisionFunc(LSCollider2D other); |
| | | 71 | | /// <summary>Raised while this collider has a physical mixed contact with a 2D collider.</summary> |
| | | 72 | | public event MixedCollisionFunc? OnMixedContact; |
| | | 73 | | /// <summary>Raised when this collider begins a physical mixed contact with a 2D collider.</summary> |
| | | 74 | | public event MixedCollisionFunc? OnMixedContactEnter; |
| | | 75 | | /// <summary>Raised when this collider ends a physical mixed contact with a 2D collider.</summary> |
| | | 76 | | public event MixedCollisionFunc? OnMixedContactExit; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Raised on the first mixed 3D/2D simulation frame this collider participates in a valid trigger pair. |
| | | 80 | | /// </summary> |
| | | 81 | | public event MixedCollisionFunc? OnMixedTriggerEnter; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Raised each mixed 3D/2D simulation frame this collider participates in an overlapped valid trigger pair. |
| | | 85 | | /// </summary> |
| | | 86 | | public event MixedCollisionFunc? OnMixedTriggerStay; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Raised when this collider stops participating in a valid mixed 3D/2D trigger pair. |
| | | 90 | | /// </summary> |
| | | 91 | | public event MixedCollisionFunc? OnMixedTriggerExit; |
| | | 92 | | |
| | | 93 | | internal void NotifyContact(LSCollider other, bool isColliding, bool isChanged) => |
| | | 94 | | NotifyContact( |
| | | 95 | | other, |
| | | 96 | | other.Body, |
| | | 97 | | isColliding, |
| | | 98 | | isChanged, |
| | | 99 | | allowInactive: false, |
| | | 100 | | new ColliderLifetimeToken(this), |
| | | 101 | | new ColliderLifetimeToken(other), |
| | | 102 | | IsTrigger || other.IsTrigger, |
| | | 103 | | ColliderTriggerEventPolicy.ShouldRaise(this, other)); |
| | | 104 | | |
| | | 105 | | internal void NotifyContact( |
| | | 106 | | LSCollider other, |
| | | 107 | | SolidBody? otherBody, |
| | | 108 | | bool isColliding, |
| | | 109 | | bool isChanged, |
| | | 110 | | bool allowInactive, |
| | | 111 | | in ColliderLifetimeToken registration, |
| | | 112 | | in ColliderLifetimeToken otherRegistration, |
| | | 113 | | bool isTriggerPair, |
| | | 114 | | bool shouldRaiseTrigger) |
| | | 115 | | { |
| | | 116 | | if (isColliding |
| | | 117 | | ? !registration.IsActive || !otherRegistration.IsActive |
| | | 118 | | : allowInactive ? !registration.IsCurrentLifetime : !registration.IsActive) |
| | | 119 | | { |
| | | 120 | | return; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | if (isColliding) |
| | | 124 | | { |
| | | 125 | | if (isTriggerPair) |
| | | 126 | | { |
| | | 127 | | if (shouldRaiseTrigger) |
| | | 128 | | { |
| | | 129 | | if (isChanged) |
| | | 130 | | { |
| | | 131 | | OnTriggerEnter?.Invoke(other); |
| | | 132 | | if (!registration.IsActive || !otherRegistration.IsActive) |
| | | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | OnTriggerStay?.Invoke(other); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | return; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | if (isChanged && otherBody != null) |
| | | 143 | | { |
| | | 144 | | OnContactEnter?.Invoke(otherBody); |
| | | 145 | | if (!registration.IsActive || !otherRegistration.IsActive) |
| | | 146 | | return; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | if (otherBody != null) |
| | | 150 | | OnContact?.Invoke(otherBody); |
| | | 151 | | |
| | | 152 | | return; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | if (!isChanged) |
| | | 156 | | return; |
| | | 157 | | |
| | | 158 | | if (isTriggerPair) |
| | | 159 | | { |
| | | 160 | | if (shouldRaiseTrigger) |
| | | 161 | | OnTriggerExit?.Invoke(other); |
| | | 162 | | |
| | | 163 | | return; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | if (otherBody != null) |
| | | 167 | | OnContactExit?.Invoke(otherBody); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | internal void NotifyMixedContact(LSCollider2D other, bool isColliding, bool isChanged, bool isTriggerPair) => |
| | | 171 | | NotifyMixedContact( |
| | | 172 | | other, |
| | | 173 | | isColliding, |
| | | 174 | | isChanged, |
| | | 175 | | isTriggerPair, |
| | | 176 | | allowInactive: false, |
| | | 177 | | new ColliderLifetimeToken(this), |
| | | 178 | | new ColliderLifetimeToken2D(other), |
| | | 179 | | ColliderTriggerEventPolicy.ShouldRaise(this, other)); |
| | | 180 | | |
| | | 181 | | internal void NotifyMixedContact( |
| | | 182 | | LSCollider2D other, |
| | | 183 | | bool isColliding, |
| | | 184 | | bool isChanged, |
| | | 185 | | bool isTriggerPair, |
| | | 186 | | bool allowInactive, |
| | | 187 | | in ColliderLifetimeToken registration, |
| | | 188 | | in ColliderLifetimeToken2D otherRegistration, |
| | | 189 | | bool shouldRaiseTrigger) |
| | | 190 | | { |
| | | 191 | | if (isColliding |
| | | 192 | | ? !registration.IsActive || !otherRegistration.IsActive |
| | | 193 | | : allowInactive |
| | | 194 | | ? !registration.IsCurrentLifetime || !otherRegistration.IsCurrentLifetime |
| | | 195 | | : !registration.IsActive) |
| | | 196 | | { |
| | | 197 | | return; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | if (isColliding) |
| | | 201 | | { |
| | | 202 | | if (isTriggerPair) |
| | | 203 | | { |
| | | 204 | | if (shouldRaiseTrigger) |
| | | 205 | | { |
| | | 206 | | if (isChanged) |
| | | 207 | | { |
| | | 208 | | OnMixedTriggerEnter?.Invoke(other); |
| | | 209 | | if (!registration.IsActive || !otherRegistration.IsActive) |
| | | 210 | | return; |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | OnMixedTriggerStay?.Invoke(other); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | return; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | if (isChanged) |
| | | 220 | | { |
| | | 221 | | OnMixedContactEnter?.Invoke(other); |
| | | 222 | | if (!registration.IsActive || !otherRegistration.IsActive) |
| | | 223 | | return; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | OnMixedContact?.Invoke(other); |
| | | 227 | | return; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | if (!isChanged) |
| | | 231 | | return; |
| | | 232 | | |
| | | 233 | | if (isTriggerPair) |
| | | 234 | | { |
| | | 235 | | if (shouldRaiseTrigger) |
| | | 236 | | OnMixedTriggerExit?.Invoke(other); |
| | | 237 | | |
| | | 238 | | return; |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | OnMixedContactExit?.Invoke(other); |
| | | 242 | | } |
| | | 243 | | } |