Add travel
This commit is contained in:
parent
c9b4f62a59
commit
eea6031423
4 changed files with 134 additions and 28 deletions
|
|
@ -1,9 +1,12 @@
|
|||
import type { MatrixClient } from "matrix-js-sdk";
|
||||
import {
|
||||
Location,
|
||||
locationFarlands,
|
||||
locations,
|
||||
type ILocation,
|
||||
type Location,
|
||||
} from "./structures/locations.js";
|
||||
import type { IPlayer } from "./structures/entities.js";
|
||||
import { getSpeed } from "./entity.js";
|
||||
|
||||
const getLocation = (id: Location): ILocation => {
|
||||
const location = locations.find((location) => location.id === id);
|
||||
|
|
@ -11,6 +14,14 @@ const getLocation = (id: Location): ILocation => {
|
|||
return location ? location : locationFarlands;
|
||||
};
|
||||
|
||||
const getLocationByName = (name: string): ILocation => {
|
||||
const location = locations.find(
|
||||
(location) => location.name.toLowerCase() === name.toLowerCase(),
|
||||
);
|
||||
|
||||
return location ? location : locationFarlands;
|
||||
};
|
||||
|
||||
const getLocationDistance = (
|
||||
locationA: ILocation,
|
||||
locationB: ILocation,
|
||||
|
|
@ -23,4 +34,62 @@ const getLocationDistance = (
|
|||
return Math.sqrt(deltaPow);
|
||||
};
|
||||
|
||||
export { getLocation, getLocationDistance };
|
||||
const getParentLocation = (childLocation: Location): ILocation => {
|
||||
const parentLocation = locations.find((location) =>
|
||||
location.childLocations.includes(childLocation),
|
||||
);
|
||||
|
||||
return parentLocation ? parentLocation : locationFarlands;
|
||||
};
|
||||
|
||||
const hasLocation = (
|
||||
locationParent: Location,
|
||||
locationChild: Location,
|
||||
): boolean => {
|
||||
const childLocations = getLocation(locationParent).childLocations;
|
||||
|
||||
return childLocations.includes(locationChild);
|
||||
};
|
||||
|
||||
const startTravel = (
|
||||
player: IPlayer,
|
||||
location: Location,
|
||||
client: MatrixClient,
|
||||
roomId: string,
|
||||
) => {
|
||||
const currentLocation = getLocation(player.location);
|
||||
const newLocation = getLocation(location);
|
||||
|
||||
const distance = getLocationDistance(currentLocation, newLocation);
|
||||
|
||||
setTimeout(
|
||||
() => {
|
||||
finishTravel(player, newLocation, client, roomId);
|
||||
},
|
||||
(distance / getSpeed(player)) * 3600000,
|
||||
);
|
||||
};
|
||||
|
||||
const finishTravel = (
|
||||
player: IPlayer,
|
||||
location: ILocation,
|
||||
client: MatrixClient,
|
||||
roomId: string,
|
||||
) => {
|
||||
player.location = location.id;
|
||||
|
||||
client.sendTextMessage(
|
||||
roomId,
|
||||
`${player.name} has arrived to ${location.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
export {
|
||||
getLocation,
|
||||
getLocationByName,
|
||||
getLocationDistance,
|
||||
getParentLocation,
|
||||
hasLocation,
|
||||
startTravel,
|
||||
finishTravel,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue