Add travel
This commit is contained in:
parent
eea6031423
commit
1c0a2124f0
2 changed files with 28 additions and 7 deletions
|
|
@ -7,6 +7,7 @@ import {
|
||||||
getLocationByName,
|
getLocationByName,
|
||||||
getLocationDistance,
|
getLocationDistance,
|
||||||
getParentLocation,
|
getParentLocation,
|
||||||
|
getTravelTimeInHours,
|
||||||
hasLocation,
|
hasLocation,
|
||||||
startTravel,
|
startTravel,
|
||||||
} from "../../services/game/location.js";
|
} from "../../services/game/location.js";
|
||||||
|
|
@ -210,25 +211,36 @@ const onTravel = (text: string, roomId: string, sender: string) => {
|
||||||
const player = getPlayerById(sender);
|
const player = getPlayerById(sender);
|
||||||
|
|
||||||
const locationName = text.replace(`${gamePrefix} travel `, "").trim();
|
const locationName = text.replace(`${gamePrefix} travel `, "").trim();
|
||||||
const location = getLocationByName(locationName);
|
const travelLocation = getLocationByName(locationName);
|
||||||
if (!location) {
|
if (!travelLocation) {
|
||||||
client.sendTextMessage(roomId, "No such location exists");
|
client.sendTextMessage(roomId, "No such location exists");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentLocationHasLocation = hasLocation(
|
const currentLocationHasLocation = hasLocation(
|
||||||
player.location,
|
player.location,
|
||||||
location.id,
|
travelLocation.id,
|
||||||
);
|
);
|
||||||
const parentLocationHasLocation = hasLocation(
|
const parentLocationHasLocation = hasLocation(
|
||||||
getParentLocation(player.location).id,
|
getParentLocation(player.location).id,
|
||||||
location.id,
|
travelLocation.id,
|
||||||
);
|
);
|
||||||
if (!currentLocationHasLocation && !parentLocationHasLocation) {
|
if (!currentLocationHasLocation && !parentLocationHasLocation) {
|
||||||
client.sendTextMessage(roomId, "No such location found nearby");
|
client.sendTextMessage(roomId, "No such location found nearby");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const travelTime = getTravelTimeInHours(
|
||||||
|
player,
|
||||||
|
getLocation(player.location),
|
||||||
|
travelLocation,
|
||||||
|
);
|
||||||
|
|
||||||
|
client.sendTextMessage(
|
||||||
|
roomId,
|
||||||
|
`You're now travelling to ${travelLocation.name}. It will take you ${travelTime.toFixed(1)} hours`,
|
||||||
|
);
|
||||||
|
|
||||||
startTravel(player, location.id, client, roomId);
|
startTravel(player, location.id, client, roomId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,16 @@ const hasLocation = (
|
||||||
return childLocations.includes(locationChild);
|
return childLocations.includes(locationChild);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getTravelTimeInHours = (
|
||||||
|
player: IPlayer,
|
||||||
|
locationA: ILocation,
|
||||||
|
locationB: ILocation,
|
||||||
|
) => {
|
||||||
|
const distance = getLocationDistance(locationA, locationB);
|
||||||
|
|
||||||
|
return (distance / getSpeed(player)) * 3600000;
|
||||||
|
};
|
||||||
|
|
||||||
const startTravel = (
|
const startTravel = (
|
||||||
player: IPlayer,
|
player: IPlayer,
|
||||||
location: Location,
|
location: Location,
|
||||||
|
|
@ -60,13 +70,11 @@ const startTravel = (
|
||||||
const currentLocation = getLocation(player.location);
|
const currentLocation = getLocation(player.location);
|
||||||
const newLocation = getLocation(location);
|
const newLocation = getLocation(location);
|
||||||
|
|
||||||
const distance = getLocationDistance(currentLocation, newLocation);
|
|
||||||
|
|
||||||
setTimeout(
|
setTimeout(
|
||||||
() => {
|
() => {
|
||||||
finishTravel(player, newLocation, client, roomId);
|
finishTravel(player, newLocation, client, roomId);
|
||||||
},
|
},
|
||||||
(distance / getSpeed(player)) * 3600000,
|
getTravelTimeInHours(player, currentLocation, newLocation),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -90,6 +98,7 @@ export {
|
||||||
getLocationDistance,
|
getLocationDistance,
|
||||||
getParentLocation,
|
getParentLocation,
|
||||||
hasLocation,
|
hasLocation,
|
||||||
|
getTravelTimeInHours,
|
||||||
startTravel,
|
startTravel,
|
||||||
finishTravel,
|
finishTravel,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue