import { useLayoutEffect , useRef } from 'react' ; import { useScene } from './useScene' ; import { Scene } from 'phaser' ; import { findGameObjectsByName } from '../utils' ; export type ColliderObjectType = Phaser . GameObjects . GameObject | string ; /**
 * Creates a collider between objects or arrays of objects. If provided values are strings, it will
 * search for all objects by that name in the scene.
 */ export function useArcadeCollider < T1 extends ColliderObjectType , T2 extends ColliderObjectType > ( obj1 , obj2 , args ) { const { onCollide , onProcess , overlapOnly } = args ; const scene = useScene ( ) ; const collider = useRef < Phaser . Physics . Arcade . Collider > ( null ) ; useLayoutEffect ( ( ) => { collider . current = scene . physics . add . collider ( createObjectsArray ( scene , obj1 ) , createObjectsArray ( scene , obj2 ) , onCollide , onProcess ) ; collider . current . overlapOnly = overlapOnly ; return ( ) => { collider . current . destroy ( ) ; } ; } , [ ] ) ; useLayoutEffect ( ( ) => { if ( collider . current ) { collider . current . object1 = createObjectsArray ( scene , obj1 ) ; collider . current . object2 = createObjectsArray ( scene , obj2 ) ; } } , [ obj1 , obj2 ] ) ; useLayoutEffect ( ( ) => { if ( collider . current ) { collider . current . collideCallback = onCollide ; collider . current . processCallback = onProcess ; collider . current . overlapOnly = overlapOnly ; } } , [ onCollide , onProcess , overlapOnly ] ) ; } function createObjectsArray ( scene , objects ) { return toArray ( objects ) . reduce ( ( total , object ) => { if ( typeof object === 'string' ) { return [ ... total , ... findGameObjectsByName ( scene , object ) ] ; } return [ ... total , object ] ; } , [ ] ) as Phaser . GameObjects . GameObject [ ] ; } function toArray < T > ( obj ) { return Array . isArray ( obj ) ? obj : [ obj ] ; }