Skip to content

Commit

Permalink
src/ui/mainWindow.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
guysv committed Dec 7, 2024
1 parent 1005e46 commit 3893e86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
42 changes: 19 additions & 23 deletions src/services/vehicleDragger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const dragToolId = "rve-drag-vehicle";
*/
export function toggleVehicleDragger(isPressed: boolean,
storeVehicle: Store<[RideVehicle, number] | null>,
storeXYZ: Store<CoordsXYZ>,
storeX: Store<number>,
storeY: Store<number>,
storeZ: Store<number>,
storeTrackLocation: Store<CarTrackLocation | null>,
storeTrackProgress: Store<number>,
onCancel: () => void): void
Expand All @@ -35,14 +37,13 @@ export function toggleVehicleDragger(isPressed: boolean,
return;
}

const originalXYZ = storeXYZ.get();
const multiplayer = isMultiplayer();
const originalPosition =
{
revert: true,
x: originalXYZ.x,
y: originalXYZ.y,
z: originalXYZ.z,
x: storeX.get(),
y: storeY.get(),
z: storeZ.get(),
};
const originalTrackPosition = storeTrackLocation.get();
const originalTrackProgress = storeTrackProgress.get();
Expand All @@ -59,19 +60,14 @@ export function toggleVehicleDragger(isPressed: boolean,
// Limit updates to 8 fps to avoid bringing down multiplayer servers
return;
}
const [tilePosition, trackPosition] = getPositionFromTool(args, rideVehicle[0]._type(), rideVehicle[0]._id);
if (tilePosition && trackPosition && !equalCoordsXYZ(trackPosition, lastTrackPosition) ||
!trackPosition && tilePosition && !equalCoordsXYZ(tilePosition, lastPosition))
const position = getPositionFromTool(args, rideVehicle[0]._type(), rideVehicle[0]._id);
if (position && position.trackPosition && !equalCoordsXYZ(position.trackPosition, lastTrackPosition) ||
position && !equalCoordsXYZ(position.tilePosition, lastPosition))
{
const position = {
tilePosition,
trackPosition,
trackProgress: null,
};
updateCarPosition(rideVehicle, position, DragState.Dragging);
ui.tileSelection.tiles = [{ x: alignWithMap(tilePosition.x), y : alignWithMap(tilePosition.y) }];
lastPosition = tilePosition;
lastTrackPosition = trackPosition;
ui.tileSelection.tiles = [{ x: alignWithMap(position.tilePosition.x), y : alignWithMap(position.tilePosition.y) }];
lastPosition = position.tilePosition;
lastTrackPosition = position.trackPosition;
}
},
onDown: () =>
Expand Down Expand Up @@ -132,11 +128,11 @@ interface DragVehicleArgs
/**
* Get a possible position to drag the vehicle to.
*/
function getPositionFromTool(args: ToolEventArgs, vehicleType: RideObjectVehicle | null, currentId: number): [CoordsXYZ | null, CarTrackLocation | null]
function getPositionFromTool(args: ToolEventArgs, vehicleType: RideObjectVehicle | null, currentId: number): DragPosition | null
{
const { entityId, mapCoords, tileElementIndex } = args;
let x: number | undefined, y: number | undefined, z: number | undefined;
let trackLocation: CarTrackLocation | null = null;
let trackPosition: CarTrackLocation | null = null;

if (!isUndefined(entityId) && entityId !== currentId)
{
Expand Down Expand Up @@ -173,7 +169,7 @@ function getPositionFromTool(args: ToolEventArgs, vehicleType: RideObjectVehicle
if (type === "track") {
const iterator = map.getTrackIterator({x, y}, tileElementIndex);
if (!isNull(iterator)) {
trackLocation = {
trackPosition = {
x: iterator.position.x-16, // back to block center
y: iterator.position.y-16,
z: iterator.position.z,
Expand All @@ -189,9 +185,9 @@ function getPositionFromTool(args: ToolEventArgs, vehicleType: RideObjectVehicle
}
else
{
return [null, null];
return null;
}
return [{ x, y, z }, trackLocation];
return {tilePosition: { x, y, z }, trackPosition, trackProgress: null};
}

/**
Expand Down Expand Up @@ -227,8 +223,8 @@ function updateVehicleDrag(args: DragVehicleArgs): void
// internally, travelBy calls MoveTo so the car will render on the holding
// track
// HACK: seems to make the vehicle update render properly
car.travelBy(getDistanceFromProgress(car, 1));
car.travelBy(getDistanceFromProgress(car, -1));
car.travelBy(1);
car.travelBy(-1);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/ui/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export const mainWindow = window({
image: 5174, // SPR_PICKUP_BTN
isPressed: twoway(model._isDragging),
disabled: model._isPositionDisabled,
onChange: pressed => toggleVehicleDragger(pressed, model._selectedVehicle, model._xyz, model._trackLocation, model._trackProgress, () => model._isDragging.set(false))
onChange: pressed => toggleVehicleDragger(pressed, model._selectedVehicle, model._x, model._y, model._z,
model._trackLocation, model._trackProgress, () => model._isDragging.set(false))
}),
toggle({
width: buttonSize, height: buttonSize,
Expand Down Expand Up @@ -511,4 +512,4 @@ function positionSpinner(params: LabelledSpinnerParams & FlexiblePosition): Widg
params.tooltip = "The fantastic map location of your vehicle and where to find it. Only works when the vehicle is not moving.";
params._noDisabledMessage = true;
return labelSpinner(params);
}
}

0 comments on commit 3893e86

Please sign in to comment.