import { UserManager } from './UserManager' ; import { ActionMagnetMove } from './ActionMagnetMove' ; import { BoardManager } from './boardManager' ; import { ConstraintDrawing } from './ConstraintDrawing' ; /**
 * this class stores the movement of the magnets for then storing actions that are magnet movement
 */ export class MagnetMovementRecorder { /**
     * stores the points during the movement of a magnet
     */ static magnetIDToPoints = { } ; param id description static start ( id ) { MagnetMovementRecorder . magnetIDToPoints [ id ] = [ ] ; } param id param x param y description static move ( id , x , y ) { const magnet = document . getElementById ( id ) ; MagnetMovementUpdater . update ( magnet , x , y ) ; ConstraintDrawing . update ( ) ; if ( MagnetMovementRecorder . magnetIDToPoints [ id ] ) MagnetMovementRecorder . magnetIDToPoints [ id ] . push ( { x : x , y : y } ) ; } param id description static stop ( id ) { console . log ( `number of points in the movement of magnet: ${ MagnetMovementRecorder . magnetIDToPoints [ id ] . length }` ) ; if ( MagnetMovementRecorder . magnetIDToPoints [ id ] . length > 0 ) BoardManager . addAction ( new ActionMagnetMove ( UserManager . me . userID , id , MagnetMovementRecorder . magnetIDToPoints [ id ] ) ) ; delete MagnetMovementRecorder . magnetIDToPoints [ id ] ; } } export class MagnetMovementUpdater { static update ( magnet , x , y ) { const previousx = parseFloat ( magnet . style . left ) ; const previousy = parseFloat ( magnet . style . top ) ; if ( magnet . classList . contains ( "car" ) ) MagnetMovementUpdater . carUpdate ( magnet , x , y , previousx , previousy ) ; else MagnetMovementUpdater . simpleUpdate ( magnet , x , y ) ; } private static simpleUpdate ( magnet , x , y ) { magnet . style . top = y + "px" ; magnet . style . left = x + "px" ; } private static previousAngle = 0 ; private static carUpdate ( magnet , x , y , previousx , previousy ) { MagnetMovementUpdater . simpleUpdate ( magnet , x , y ) ; const angle = Math . atan2 ( y - previousy , x - previousx ) ; if ( Math . abs ( MagnetMovementUpdater . previousAngle - angle ) <= 0.1 ) magnet . style . rotate = angle + "rad" ; MagnetMovementUpdater . previousAngle = angle ; } }