import { Client } from "./types" ; import { getPosition , getDist , getRotatiion , getAverageClient } from "./utils" ; export class ClientStore { public prevClients : Client [ ] = [ ] ; public startClients : Client [ ] = [ ] ; public movement = 0 ; public length = 0 ; constructor ( clients ) { this . startClients = clients ; this . prevClients = clients ; this . length = clients . length ; } public addClients ( clients = this . prevClients ) { const position = this . getPosition ( clients ) ; const { deltaX , deltaY } = position ; this . movement += Math . sqrt ( deltaX * deltaX + deltaY * deltaY ) ; this . prevClients = clients ; return position ; } public getAngle ( clients = this . prevClients ) { return getRotatiion ( clients ) ; } public getRotation ( clients = this . prevClients ) { return getRotatiion ( clients ) - getRotatiion ( this . startClients ) ; } public getPosition ( clients ? ) { return getPosition ( clients || this . prevClients , this . prevClients , this . startClients ) ; } public getPositions ( clients = this . prevClients ) { const prevClients = this . prevClients ; return this . startClients . map ( ( startClient , i ) => getPosition ( [ clients [ i ] ] , [ prevClients [ i ] ] , [ startClient ] ) ) ; } public getMovement ( clients ? ) { const movement = this . movement ; if ( ! clients ) { return movement ; } const currentClient = getAverageClient ( clients , this . length ) ; const prevClient = getAverageClient ( this . prevClients , this . length ) ; const deltaX = currentClient . clientX - prevClient . clientX ; const deltaY = currentClient . clientY - prevClient . clientY ; return Math . sqrt ( deltaX * deltaX + deltaY * deltaY ) + movement ; } public getDistance ( clients = this . prevClients ) { return getDist ( clients ) ; } public getScale ( clients = this . prevClients ) { return getDist ( clients ) / getDist ( this . startClients ) ; } public move ( deltaX , deltaY ) { this . startClients . forEach ( client => { client . clientX -= deltaX ; client . clientY -= deltaY ; } ) ; this . prevClients . forEach ( client => { client . clientX -= deltaX ; client . clientY -= deltaY ; } ) ; } }