import { distance } from 'common/util' ; import Sprite , { Polygon , Circle } from './sprite' ; export default class Collide { constructor ( ) { if ( this instanceof Collide ) { return this ; } return new Collide ( ) ; } public isCandidateForCollide ( sprite , otherSprite ) { return sprite . visible && otherSprite . visible && sprite !== otherSprite ; } public didCollide ( sprite , otherSprite ) { if ( sprite . type === 'circle' && otherSprite . type === 'circle' ) { let x1 = ( sprite as Circle ) . x ; let y1 = ( sprite as Circle ) . y ; let r1 = ( sprite as Circle ) . radius ; let x2 = ( otherSprite as Circle ) . x ; let y2 = ( otherSprite as Circle ) . y ; let r2 = ( otherSprite as Circle ) . radius ; return distance ( x1 , y1 , x2 , y2 ) <= r1 + r2 ; } let axes1 = sprite . type === 'circle' ? ( sprite as Circle ) . getAxes ( otherSprite as Polygon ) : ( sprite as Polygon ) . getAxes ( ) ; let axes2 = otherSprite . type === 'circle' ? ( otherSprite as Circle ) . getAxes ( sprite as Polygon ) : ( otherSprite as Polygon ) . getAxes ( ) ; let axes = [ ... axes1 , ... axes2 ] ; for ( let axis of axes ) { let projections1 = sprite . getProjection ( axis ) ; let projections2 = otherSprite . getProjection ( axis ) ; if ( ! projections1 . overlaps ( projections2 ) ) { return false ; } } return true ; } }