@@ -2,7 +2,7 @@ import { Store } from "openrct2-flexui";
22import { isMultiplayer } from "../environment" ;
33import { getCarById , RideVehicle } from "../objects/rideVehicle" ;
44import * as Log from "../utilities/logger" ;
5- import { alignWithMap , getTileElement , toTileUnit } from "../utilities/map" ;
5+ import { alignWithMap , getIndexForTrackElementAt , getTileElement , toTileUnit } from "../utilities/map" ;
66import { cancelCurrentTool , cancelTools } from "../utilities/tools" ;
77import { isNumber , isUndefined } from "../utilities/type" ;
88import { register } from "./actions" ;
@@ -21,8 +21,7 @@ export const dragToolId = "rve-drag-vehicle";
2121/**
2222 * Enable or disable a tool to drag the vehicle to a new location.
2323 */
24- export function toggleVehicleDragger ( isPressed : boolean , storeVehicle : Store < [ RideVehicle , number ] | null > , storeX : Store < number > , storeY : Store < number > , storeZ : Store < number > ,
25- storeTrackLocation : Store < CarTrackLocation | null > , storeTrackProgress : Store < number > , onCancel : ( ) => void ) : void
24+ export function toggleVehicleDragger ( isPressed : boolean , storeVehicle : Store < [ RideVehicle , number ] | null > , storeX : Store < number > , storeY : Store < number > , storeZ : Store < number > , storeTrackLocation : Store < CarTrackLocation | null > , storeTrackProgress : Store < number > , onCancel : ( ) => void ) : void
2625{
2726 const rideVehicle = storeVehicle . get ( ) ;
2827 if ( ! isPressed || ! rideVehicle )
@@ -75,6 +74,13 @@ export function toggleVehicleDragger(isPressed: boolean, storeVehicle: Store<[Ri
7574 if ( originalPosition . revert )
7675 {
7776 Log . debug ( "[VehicleDragger] Finish tool & revert position to" , originalPosition , rideVehicle [ 0 ] . _car ( ) ) ;
77+
78+ const track = originalPosition . track ;
79+ if ( track )
80+ {
81+ ( < DragPosition > originalPosition ) . trackElementIndex = getIndexForTrackElementAt ( track ) ;
82+ }
83+
7884 updateCarPosition ( rideVehicle , originalPosition , DragState . Cancel ) ;
7985 }
8086 ui . tileSelection . tiles = [ ] ;
@@ -100,6 +106,9 @@ interface DragPosition extends CoordsXYZ
100106{
101107 trackElementIndex ?: number | null ;
102108 progress ?: number | null ;
109+
110+ /** The track to move on to. Only available when cancelling the tool. */
111+ track ?: CarTrackLocation | null ;
103112}
104113
105114/**
@@ -201,19 +210,31 @@ function updateVehicleDrag(args: DragVehicleArgs): void
201210{
202211 const id = args . target ;
203212 const car = getCarById ( args . target ) ;
213+
204214 if ( ! car )
205215 {
206216 return ;
207217 }
208218
209219 const position = args . position ;
210220 const progress = position . progress ;
221+ let skipCarXYZ : boolean | undefined ;
222+
211223 if ( isNumber ( position . trackElementIndex ) && isNumber ( progress ) )
212224 {
213- car . moveToTrack ( toTileUnit ( position . x ) , toTileUnit ( position . y ) , position . trackElementIndex ) ;
225+ const track : CoordsXY = position . track || position ;
226+
227+ car . moveToTrack ( toTileUnit ( track . x ) , toTileUnit ( track . y ) , position . trackElementIndex ) ;
214228 car . travelBy ( getDistanceFromProgress ( car , progress - car . trackProgress ) ) ;
229+
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+ }
215235 }
216- else
236+
237+ if ( ! skipCarXYZ )
217238 {
218239 car . x = position . x ;
219240 car . y = position . y ;
0 commit comments