Add entity info
This commit is contained in:
parent
0958a8c272
commit
58df618956
4 changed files with 250 additions and 39 deletions
|
|
@ -1,6 +1,15 @@
|
|||
import { getUserNameById } from "../../helpers.js";
|
||||
import { state } from "../../store/store.js";
|
||||
import type { IEntity, IPlayer } from "./structures/entities.js";
|
||||
import {
|
||||
npcBecky,
|
||||
npcs,
|
||||
type IEntity,
|
||||
type INPC,
|
||||
type INPCData,
|
||||
type IPlayer,
|
||||
type NPC,
|
||||
type TFullNPC,
|
||||
} 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";
|
||||
|
|
@ -28,11 +37,36 @@ const createPlayer = (name: string): IPlayer => ({
|
|||
lockpicking: 0,
|
||||
});
|
||||
|
||||
const getPlayerById = (userId: string): IPlayer => {
|
||||
return getPlayer(getUserNameById(userId));
|
||||
const createNpcData = (npc: INPC): INPCData => ({
|
||||
id: npc.id,
|
||||
health: getMaxHealth(npc),
|
||||
dead: false,
|
||||
});
|
||||
|
||||
const existsPlayer = (name: string): boolean => {
|
||||
return (
|
||||
state.game.players.find(
|
||||
(player) => player.name.toLowerCase() === name.toLowerCase(),
|
||||
) !== undefined
|
||||
);
|
||||
};
|
||||
|
||||
const getPlayer = (name: string): IPlayer => {
|
||||
const existsNpc = (name: string): boolean => {
|
||||
return (
|
||||
npcs.find((npc) => npc.name.toLowerCase() === name.toLowerCase()) !==
|
||||
undefined
|
||||
);
|
||||
};
|
||||
|
||||
const existsEntity = (name: string): boolean => {
|
||||
return existsPlayer(name) || existsNpc(name);
|
||||
};
|
||||
|
||||
const getPlayer = (id: string): IPlayer => {
|
||||
return getPlayer(getUserNameById(id));
|
||||
};
|
||||
|
||||
const getPlayerByName = (name: string): IPlayer => {
|
||||
const player = state.game.players.find((player) => player.name === name);
|
||||
if (player) {
|
||||
return player;
|
||||
|
|
@ -44,6 +78,55 @@ const getPlayer = (name: string): IPlayer => {
|
|||
return newPlayer;
|
||||
};
|
||||
|
||||
const getEntityByName = (name: string): IPlayer | TFullNPC => {
|
||||
if (existsPlayer(name)) {
|
||||
return getPlayer(name);
|
||||
}
|
||||
|
||||
const npc = getNpcByName(name);
|
||||
|
||||
return { ...npc, ...getNpcData(npc.id) };
|
||||
};
|
||||
|
||||
const getNpc = (id: NPC): INPC => {
|
||||
const npc = npcs.find((npc) => npc.id === id);
|
||||
|
||||
return npc ? npc : npcBecky;
|
||||
};
|
||||
|
||||
const getNpcByName = (name: string): INPC => {
|
||||
const npc = npcs.find((npc) => npc.id === name);
|
||||
|
||||
return npc ? npc : npcBecky;
|
||||
};
|
||||
|
||||
const getNpcData = (id: NPC): INPCData => {
|
||||
const npcData = state.game.npcs.find((npcData) => npcData.id === id);
|
||||
if (npcData) {
|
||||
return npcData;
|
||||
}
|
||||
|
||||
const newNpcData = createNpcData(getNpc(id));
|
||||
state.game.npcs.push(newNpcData);
|
||||
|
||||
return newNpcData;
|
||||
};
|
||||
|
||||
const getNpcsAtLocation = (id: Location): INPC[] => {
|
||||
return npcs.filter((npc) => npc.location === id);
|
||||
};
|
||||
|
||||
const getPlayersAtLocation = (id: Location): IPlayer[] => {
|
||||
return state.game.players.filter((player) => player.location === id);
|
||||
};
|
||||
|
||||
const getEntitiesAtLocation = (id: Location): IEntity[] => {
|
||||
const npcsAtLocation = getNpcsAtLocation(id);
|
||||
const playersAtLocation = getPlayersAtLocation(id);
|
||||
|
||||
return [...npcsAtLocation, ...playersAtLocation];
|
||||
};
|
||||
|
||||
const getRace = (id: Race): IRace => {
|
||||
const race = races.find((race) => race.id === id);
|
||||
|
||||
|
|
@ -69,8 +152,41 @@ const getLevel = (experience: number): ILevel => {
|
|||
};
|
||||
};
|
||||
|
||||
const getSpeed = (entity: IEntity) => {
|
||||
return 1 + Math.sqrt(entity.agility + entity.endurance) / 3;
|
||||
const getMaxHealth = (entity: IEntity): number => {
|
||||
return entity.vitality * 10;
|
||||
};
|
||||
|
||||
export { createPlayer, getPlayerById, getPlayer, getRace, getLevel, getSpeed };
|
||||
const getHealthPercentage = (entity: IPlayer | INPC): number => {
|
||||
if ("health" in entity) {
|
||||
return entity.health / getMaxHealth(entity);
|
||||
}
|
||||
|
||||
const npcData = getNpcData(entity.id);
|
||||
return npcData.health / getMaxHealth(entity);
|
||||
};
|
||||
|
||||
const getSpeed = (entity: IEntity) => {
|
||||
return 60 + 3 + Math.sqrt(entity.agility + entity.endurance) / 2;
|
||||
};
|
||||
|
||||
export {
|
||||
createPlayer,
|
||||
createNpcData,
|
||||
existsPlayer,
|
||||
existsNpc,
|
||||
existsEntity,
|
||||
getPlayer,
|
||||
getPlayerByName,
|
||||
getEntityByName,
|
||||
getNpc,
|
||||
getNpcByName,
|
||||
getNpcData,
|
||||
getNpcsAtLocation,
|
||||
getPlayersAtLocation,
|
||||
getEntitiesAtLocation,
|
||||
getRace,
|
||||
getLevel,
|
||||
getMaxHealth,
|
||||
getHealthPercentage,
|
||||
getSpeed,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {
|
|||
locations,
|
||||
type ILocation,
|
||||
} from "./structures/locations.js";
|
||||
import type { IPlayer } from "./structures/entities.js";
|
||||
import type { IEntity, IPlayer } from "./structures/entities.js";
|
||||
import { getSpeed } from "./entity.js";
|
||||
|
||||
const existsLocation = (name: string): boolean => {
|
||||
|
|
@ -34,8 +34,24 @@ const getLocationDistance = (
|
|||
locationA: ILocation,
|
||||
locationB: ILocation,
|
||||
): number => {
|
||||
const deltaX = Math.abs(locationA.X - locationB.X);
|
||||
const deltaY = Math.abs(locationA.Y - locationB.Y);
|
||||
const parentOfA = getParentLocation(locationA.id);
|
||||
const parentOfB = getParentLocation(locationB.id);
|
||||
|
||||
let locAX = locationA.X;
|
||||
let locAY = locationA.Y;
|
||||
let locBX = locationB.X;
|
||||
let locBY = locationB.Y;
|
||||
|
||||
if (locationA.id === parentOfB.id) {
|
||||
locBX += locAX;
|
||||
locBY += locAY;
|
||||
} else if (locationB.id === parentOfA.id) {
|
||||
locAX += locBX;
|
||||
locAY += locBY;
|
||||
}
|
||||
|
||||
const deltaX = Math.abs(locAX - locBX);
|
||||
const deltaY = Math.abs(locAY - locBY);
|
||||
|
||||
const deltaPow = Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
|
||||
|
||||
|
|
@ -59,6 +75,13 @@ const hasLocation = (
|
|||
return childLocations.includes(locationChild);
|
||||
};
|
||||
|
||||
const entitiesShareLocation = (entityA: IEntity, entityB: IEntity): boolean => {
|
||||
const locationA = getLocation(entityA.location);
|
||||
const locationB = getLocation(entityB.location);
|
||||
|
||||
return locationA.id === locationB.id;
|
||||
};
|
||||
|
||||
const getTravelTimeInHours = (
|
||||
player: IPlayer,
|
||||
locationA: ILocation,
|
||||
|
|
@ -115,6 +138,7 @@ export {
|
|||
getLocationDistance,
|
||||
getParentLocation,
|
||||
hasLocation,
|
||||
entitiesShareLocation,
|
||||
getTravelTimeInHours,
|
||||
getTravelTimeInMilliseconds,
|
||||
startTravel,
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ export interface INPCData {
|
|||
dead: boolean;
|
||||
}
|
||||
|
||||
export type TFullNPC = INPC & INPCData;
|
||||
|
||||
export enum NPC {
|
||||
BECKY = "BECKY",
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue