@@ -41,6 +41,7 @@ export function toggleVehicleDragger(isPressed: boolean, storeVehicle: Store<[Ri
4141 progress : storeTrackProgress . get ( )
4242 } ;
4343 let lastPosition : DragPosition = originalPosition ;
44+ let resetTrack : CarTrackLocation | null | undefined ;
4445
4546 ui . activateTool ( {
4647 id : dragToolId ,
@@ -56,6 +57,20 @@ export function toggleVehicleDragger(isPressed: boolean, storeVehicle: Store<[Ri
5657 const position = getPositionFromTool ( args , vehicle ) ;
5758 if ( position && ( position . x !== lastPosition . x || position . y !== lastPosition . y || position . z !== lastPosition . z ) )
5859 {
60+ if ( isNumber ( position . trackElementIndex ) )
61+ {
62+ // Remember where to reset track location to when moving off track again.
63+ resetTrack ||= originalPosition . track ;
64+ }
65+ else if ( resetTrack )
66+ {
67+ // Reset back to original track piece.
68+ position . track = resetTrack ;
69+ position . trackElementIndex = getIndexForTrackElementAt ( resetTrack ) ;
70+ position . progress = originalPosition . progress ;
71+ resetTrack = null ;
72+ }
73+
5974 Log . debug ( "Selected position:" , JSON . stringify ( position ) ) ;
6075 updateCarPosition ( rideVehicle , position , DragState . Dragging ) ;
6176 ui . tileSelection . tiles = [ { x : alignWithMap ( position . x ) , y : alignWithMap ( position . y ) } ] ;
@@ -222,16 +237,14 @@ function updateVehicleDrag(args: DragVehicleArgs): void
222237
223238 if ( isNumber ( position . trackElementIndex ) && isNumber ( progress ) )
224239 {
225- const track : CoordsXY = position . track || position ;
240+ const track = position . track ;
241+ const target : CoordsXY = track || position ;
226242
227- car . moveToTrack ( toTileUnit ( track . x ) , toTileUnit ( track . y ) , position . trackElementIndex ) ;
243+ car . moveToTrack ( toTileUnit ( target . x ) , toTileUnit ( target . y ) , position . trackElementIndex ) ;
228244 car . travelBy ( getDistanceFromProgress ( car , progress - car . trackProgress ) ) ;
229245
230- // If cancelling, also update the car XYZ, as it may be different from the track.
231- if ( args . state !== DragState . Cancel )
232- {
233- skipCarXYZ = true ;
234- }
246+ // If track variable available, then apply XYZ as preferred vehicle position.
247+ skipCarXYZ = ! track ;
235248 }
236249
237250 if ( ! skipCarXYZ )
0 commit comments