Skip to content

Commit c49e170

Browse files
Basssiiieguysv
andcommitted
Fix dragger tool not reverting car correctly on cancel
Co-authored-by: guysv <sviryguy@gmail.com>
1 parent 6479ceb commit c49e170

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/services/spacingEditor.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RideTrain } from "../objects/rideTrain";
22
import * as Log from "../utilities/logger";
3-
import { getTileByCoords } from "../utilities/map";
3+
import { getIndexForTrackElementAt } from "../utilities/map";
44
import { abs } from "../utilities/math";
55
import { isNull, isUndefined } from "../utilities/type";
66
import { getSubpositionTranslationDistance, getTrackSegmentDistances, TrackDistances } from "./subpositionHelper";
@@ -200,7 +200,7 @@ function calculateSpacingToPrecedingVehicle(car: Car, carInFront: Car): number |
200200
/**
201201
* Get a track iterator for the specified track location.
202202
*/
203-
function getTrackIteratorAtLocation(trackLocation: CoordsXYZD): TrackIterator | null
203+
function getTrackIteratorAtLocation(trackLocation: CarTrackLocation): TrackIterator | null
204204
{
205205
const currentTrackIndex = getIndexForTrackElementAt(trackLocation);
206206
if (isNull(currentTrackIndex))
@@ -218,27 +218,6 @@ function getTrackIteratorAtLocation(trackLocation: CoordsXYZD): TrackIterator |
218218
return iterator;
219219
}
220220

221-
/**
222-
* Finds the index of a matching track element on the specified tile.
223-
*/
224-
function getIndexForTrackElementAt(coords: CoordsXYZD): number | null
225-
{
226-
const tile = getTileByCoords(coords.x, coords.y);
227-
const allElements = tile.elements, len = allElements.length;
228-
229-
for (let i = 0; i < len; i++)
230-
{
231-
const element = tile.elements[i];
232-
if (element.type === "track"
233-
&& element.baseZ === coords.z
234-
&& element.direction === coords.direction)
235-
{
236-
return i;
237-
}
238-
}
239-
return null;
240-
}
241-
242221
/**
243222
* Returns true if the two locations are exactly equal, or false if not.
244223
*/

src/services/vehicleDragger.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Store } from "openrct2-flexui";
22
import { isMultiplayer } from "../environment";
33
import { getCarById, RideVehicle } from "../objects/rideVehicle";
44
import * as Log from "../utilities/logger";
5-
import { alignWithMap, getTileElement, toTileUnit } from "../utilities/map";
5+
import { alignWithMap, getIndexForTrackElementAt, getTileElement, toTileUnit } from "../utilities/map";
66
import { cancelCurrentTool, cancelTools } from "../utilities/tools";
77
import { isNumber, isUndefined } from "../utilities/type";
88
import { 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;

src/utilities/map.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,25 @@ export function getTileElement(x: number, y: number, elementIdx: number): TileEl
3434
const tile = getTileByCoords(x, y);
3535
return tile.getElement(elementIdx);
3636
}
37+
38+
/**
39+
* Finds the index of a matching track element on the specified tile.
40+
*/
41+
export function getIndexForTrackElementAt(location: CarTrackLocation): number | null
42+
{
43+
const tile = getTileByCoords(location.x, location.y);
44+
const allElements = tile.elements, len = allElements.length;
45+
46+
for (let i = 0; i < len; i++)
47+
{
48+
const element = tile.elements[i];
49+
if (element.type === "track"
50+
&& element.baseZ === location.z
51+
&& element.direction === location.direction
52+
&& element.trackType === location.trackType)
53+
{
54+
return i;
55+
}
56+
}
57+
return null;
58+
}

0 commit comments

Comments
 (0)