@@ -16,7 +16,7 @@ class Track {
1616 this . setLayout ( track )
1717 }
1818
19- _isCollision ( x , y ) { return ( this . carts . filter ( ( c ) => c . x === x && c . y === y && c . ghost !== true ) . length > 1 ) }
19+ _isCollision ( x , y ) { return ( this . carts . filter ( ( c ) => c . x === x && c . y === y ) . length > 1 ) }
2020 _isIntersection ( s ) { return s === '+' }
2121 _isTurn ( s ) { return this . trackTurns . indexOf ( s ) >= 0 }
2222
@@ -84,14 +84,16 @@ class Track {
8484 */
8585 advance ( ) {
8686 this . frame ++
87- this . carts . sort ( dynamicSortMultiple ( 'y' , 'x' ) ) . forEach ( ( c ) => {
88- try {
89- this . moveCart ( c )
90- } catch ( err ) {
91- console . error ( `Problem moving cart in frame ${ this . frame } ` )
92- console . error ( err )
93- }
94- } )
87+ while ( this . carts . filter ( ( c ) => c . moved === this . frame ) . length < this . carts . length ) {
88+ this . carts . filter ( ( c ) => c . moved !== this . frame ) . sort ( dynamicSortMultiple ( 'y' , 'x' ) ) . forEach ( ( c ) => {
89+ try {
90+ this . moveCart ( c )
91+ } catch ( err ) {
92+ console . error ( `Problem moving cart in frame ${ this . frame } ` )
93+ console . error ( err )
94+ }
95+ } )
96+ }
9597 }
9698
9799 /**
@@ -101,7 +103,7 @@ class Track {
101103 let output = ''
102104 const layout = JSON . parse ( JSON . stringify ( this . layout ) ) // Deep copy
103105 // Include the carts
104- this . carts . filter ( ( c ) => c . ghost !== true ) . forEach ( ( cart ) => {
106+ this . carts . forEach ( ( cart ) => {
105107 // If another cart is at the spot, draw a collision instead
106108 if ( this . cartDirections . indexOf ( layout [ cart . y ] [ cart . x ] ) >= 0 ) {
107109 layout [ cart . y ] [ cart . x ] = 'X'
@@ -160,6 +162,7 @@ class Track {
160162 const l = ( d % 3 === 0 ) ? - 1 : 1 // (+/-) distance of travel on the axis
161163 // move the cart
162164 cart [ a ] = cart [ a ] + l
165+ cart . moved = this . frame
163166 const s = this . getSegment ( cart . x , cart . y ) // Segment of track the cart is now on
164167
165168 // Make sure cart hasn't run off the rails
@@ -169,16 +172,18 @@ class Track {
169172 // Check for collision
170173 if ( this . _isCollision ( cart . x , cart . y ) ) {
171174 this . collision = { x : cart . x , y : cart . y }
175+ console . log ( `Collision in frame ${ this . frame } . removeCrashedCarts is ${ this . options . removeCrashedCarts } ` )
172176
173- if ( ! this . options . removeCrashedCarts ) {
177+ // Handle crashed carts
178+ if ( this . options . removeCrashedCarts ) {
179+ this . carts . filter ( ( c ) => c . x === cart . x && c . y === cart . y ) . forEach ( ( c ) => {
180+ this . carts . splice ( this . carts . indexOf ( c ) , 1 )
181+ } )
182+ } else {
174183 throw new Error ( `collision at ${ cart . x } , ${ cart . y } ` ) // Stop everything
175184 }
176- this . carts . filter ( ( c ) => c . x === cart . x && c . y === cart . y ) . forEach ( ( c ) => {
177- c . ghost = true // Ghost carts are dead and no longer collide
178- // we leave them in the array so that it doesn't mess up the loops
179- // necessary to finish out each cycle tick
180- } )
181185 }
186+
182187 // rotate the cart when entering a turn
183188 if ( this . _isTurn ( s ) ) {
184189 cart . direction = this . _rotate ( s , a , d )
0 commit comments