Add travel

This commit is contained in:
Aslan 2026-01-21 15:42:18 -05:00
parent c9b4f62a59
commit eea6031423
4 changed files with 134 additions and 28 deletions

View file

@ -1,9 +1,10 @@
import { getUserNameById } from "../../helpers.js";
import { state } from "../../store/store.js";
import type { IPlayer } from "./structures/entities.js";
import type { IEntity, IPlayer } from "./structures/entities.js";
import { itemShortsword } from "./structures/items.js";
import { Location } from "./structures/locations.js";
import { Race, raceHuman, races, type IRace } from "./structures/races.js";
import type { ILevel } from "./types.js";
const createPlayer = (name: string): IPlayer => ({
name: name,
@ -49,4 +50,27 @@ const getRace = (id: Race): IRace => {
return race ? race : raceHuman;
};
export { createPlayer, getPlayerById, getPlayer, getRace };
const getLevel = (experience: number): ILevel => {
let tmpExperience = experience;
let experienceToNextLevel = 50;
let level = 0;
while (tmpExperience >= experienceToNextLevel) {
level++;
tmpExperience -= experienceToNextLevel;
experienceToNextLevel = experienceToNextLevel *= 1.25;
}
return {
level: level,
totalExperience: Math.floor(experience),
experienceInLevel: Math.floor(tmpExperience),
experienceToNextLevel: Math.floor(experienceToNextLevel),
};
};
const getSpeed = (entity: IEntity) => {
return 1 + Math.sqrt(entity.agility + entity.endurance) / 3;
};
export { createPlayer, getPlayerById, getPlayer, getRace, getLevel, getSpeed };

View file

@ -1,22 +0,0 @@
import type { ILevel } from "./types.js";
const getLevel = (experience: number): ILevel => {
let tmpExperience = experience;
let experienceToNextLevel = 50;
let level = 0;
while (tmpExperience >= experienceToNextLevel) {
level++;
tmpExperience -= experienceToNextLevel;
experienceToNextLevel = experienceToNextLevel *= 1.25;
}
return {
level: level,
totalExperience: Math.floor(experience),
experienceInLevel: Math.floor(tmpExperience),
experienceToNextLevel: Math.floor(experienceToNextLevel),
};
};
export { getLevel };

View file

@ -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,
};