|
| 1 | +/* |
| 2 | + Copyright (c) 2024 Xiamen Yaji Software Co., Ltd. |
| 3 | +
|
| 4 | + https://www.cocos.com/ |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights to |
| 9 | + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | + of the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +*/ |
| 24 | +import { PhysicsContact, b2ContactExtends } from './physics-contact'; |
| 25 | + |
| 26 | +const contactMap = new Map<b2ContactExtends, PhysicsContact>(); |
| 27 | +const pools: PhysicsContact[] = []; |
| 28 | + |
| 29 | +export class PhysicsContactManager { |
| 30 | + static get (b2contact: b2ContactExtends): PhysicsContact { |
| 31 | + let c = pools.pop(); |
| 32 | + if (!c) { |
| 33 | + c = new PhysicsContact(); |
| 34 | + } |
| 35 | + c.init(b2contact); |
| 36 | + contactMap.set(b2contact, c); |
| 37 | + return c; |
| 38 | + } |
| 39 | + |
| 40 | + static find (b2contact: b2ContactExtends): PhysicsContact | null { |
| 41 | + return contactMap.get(b2contact) ?? null; |
| 42 | + } |
| 43 | + |
| 44 | + static put (b2contact: b2ContactExtends): void { |
| 45 | + const c = contactMap.get(b2contact); |
| 46 | + if (!c) return; |
| 47 | + pools.push(c); |
| 48 | + c.reset(); |
| 49 | + contactMap.delete(b2contact); |
| 50 | + } |
| 51 | + |
| 52 | + static clear (): void { |
| 53 | + contactMap.clear(); |
| 54 | + pools.length = 0; |
| 55 | + } |
| 56 | +} |
0 commit comments