import { Direction , DirectionMarker , Vec2 } from '@lib/model' ; import { Mine } from './mine.class' ; export class Cart { public direction : Direction ; public turnsAtIntersection = 0 ; constructor ( public position , directionMarker ) { this . direction = Direction . fromMarker ( directionMarker ) ; } public static compare ( a , b ) { return Vec2 . compareRowFirst ( a . position , b . position ) ; } public step ( mine ) { this . position = this . position . clone ( ) . addMut ( this . direction , { flipY : true } ) ; const rail = mine . rail . get ( this . position . toString ( ) ) ; if ( rail === '+' ) { switch ( this . turnsAtIntersection % 3 ) { case 0 : this . direction = this . direction . left ( ) ; break ; case 2 : this . direction = this . direction . right ( ) ; break ; } this . turnsAtIntersection ++ ; } else if ( rail ) { if ( this . direction . isVertical ( ) ) { if ( rail === '\\' ) { this . direction = this . direction . left ( ) ; } else if ( rail === '/' ) { this . direction = this . direction . right ( ) ; } } else if ( this . direction . isHorizonal ( ) ) { if ( rail === '\\' ) { this . direction = this . direction . right ( ) ; } else if ( rail === '/' ) { this . direction = this . direction . left ( ) ; } } } return this . isCrashed ( mine ) ? this . position : undefined ; } isCrashed ( mine ) { const crashing = mine . carts . filter ( ( cart ) => cart . position . toString ( ) === this . position . toString ( ) ) ; if ( crashing . length > 1 ) { mine . carts = mine . carts . filter ( ( cart ) => cart . position . toString ( ) !== this . position . toString ( ) ) ; } return crashing . length > 1 ; } }