diff --git a/.env.example b/.env.example index 478a495..2e5480b 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ PORT=4000 DATABASE_URL="mysql://root@localhost:3306/nq" JWT_SECRET="secret" +# Game configuration +ALLOW_DIAGONAL_MOVEMENT=false + # Default character create values DEFAULT_CHARACTER_ZONE="0" DEFAULT_CHARACTER_POS_X="0" diff --git a/src/events/zone/characterMoveEvent.ts b/src/events/zone/characterMoveEvent.ts index 012fe0c..aea192c 100644 --- a/src/events/zone/characterMoveEvent.ts +++ b/src/events/zone/characterMoveEvent.ts @@ -7,6 +7,7 @@ import { SocketEmitter } from '../../utilities/socketEmitter' import prisma from '../../utilities/prisma' import { ZoneEventTile, ZoneEventTileTeleport } from '@prisma/client' import Rotation from '../../utilities/character/rotation' +import logger from '../../utilities/logger' type ZoneEventTileWithTeleport = ZoneEventTile & { teleport: ZoneEventTileTeleport | null @@ -35,7 +36,7 @@ export default class CharacterMoveEvent { private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise { const { character } = this.socket if (!character) { - console.error('character:move error', 'Character not found') + logger.error('character:move error', 'Character not found') return } @@ -88,7 +89,7 @@ export default class CharacterMoveEvent { private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise { const { character } = this.socket if (!character) { - console.error('character:move error', 'Character not found') + logger.error('character:move error', 'Character not found') return } diff --git a/src/services/character/characterMoveService.ts b/src/services/character/characterMoveService.ts index f4b87e4..d4eeb6d 100644 --- a/src/services/character/characterMoveService.ts +++ b/src/services/character/characterMoveService.ts @@ -52,6 +52,6 @@ export class CharacterMoveService { } public async applyMovementDelay(): Promise { - await new Promise((resolve) => setTimeout(resolve, 240)) // 50ms delay between steps + await new Promise((resolve) => setTimeout(resolve, 210)) // 50ms delay between steps } } diff --git a/src/utilities/character/aStar.ts b/src/utilities/character/aStar.ts index 7a78ab2..104ea81 100644 --- a/src/utilities/character/aStar.ts +++ b/src/utilities/character/aStar.ts @@ -1,3 +1,5 @@ +import config from '../config' + type Position = { x: number; y: number } export type Node = Position & { parent?: Node; g: number; h: number; f: number } @@ -13,7 +15,7 @@ export class AStar { { x: 1, y: 1 } ] - static findPath(start: Position, end: Position, grid: number[][], allowDiagonal = false): Node[] { + static findPath(start: Position, end: Position, grid: number[][]): Node[] { const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }] const closedSet = new Set() const getKey = (p: Position) => `${p.x},${p.y}` @@ -25,7 +27,7 @@ export class AStar { openList.splice(openList.indexOf(current), 1) closedSet.add(getKey(current)) - const neighbors = this.DIRECTIONS.slice(0, allowDiagonal ? 8 : 4) + const neighbors = this.DIRECTIONS.slice(0, config.ALLOW_DIAGONAL_MOVEMENT ? 8 : 4) .map((dir) => ({ x: current.x + dir.x, y: current.y + dir.y })) .filter((pos) => this.isValidPosition(pos, grid, end)) diff --git a/src/utilities/character/rotation.ts b/src/utilities/character/rotation.ts index 9cce1f7..43c8f15 100644 --- a/src/utilities/character/rotation.ts +++ b/src/utilities/character/rotation.ts @@ -1,25 +1,35 @@ +import config from '../config' + class Rotation { static calculate(X1: number, Y1: number, X2: number, Y2: number): number { let rotation = 0 - if (X1 > X2 && Y1 > Y2) { - rotation = 7 - } else if (X1 < X2 && Y1 < Y2) { - rotation = 3 - } else if (X1 > X2 && Y1 < Y2) { - rotation = 5 - } else if (X1 < X2 && Y1 > Y2) { - rotation = 1 - } else if (X1 > X2) { - rotation = 6 - } else if (X1 < X2) { - rotation = 2 - } else if (Y1 < Y2) { - rotation = 4 - } else if (Y1 > Y2) { - rotation = 0 + + if (config.ALLOW_DIAGONAL_MOVEMENT) { + if (X1 > X2 && Y1 > Y2) { + rotation = 7 + } else if (X1 < X2 && Y1 < Y2) { + rotation = 3 + } else if (X1 > X2 && Y1 < Y2) { + rotation = 5 + } else if (X1 < X2 && Y1 > Y2) { + rotation = 1 + } } + + if (rotation === 0) { + if (X1 > X2) { + rotation = 6 + } else if (X1 < X2) { + rotation = 2 + } else if (Y1 < Y2) { + rotation = 4 + } else if (Y1 > Y2) { + rotation = 0 + } + } + return rotation } } -export default Rotation +export default Rotation \ No newline at end of file diff --git a/src/utilities/config.ts b/src/utilities/config.ts index b747735..fe1713d 100644 --- a/src/utilities/config.ts +++ b/src/utilities/config.ts @@ -8,6 +8,8 @@ class config { static PORT: number = process.env.PORT ? parseInt(process.env.PORT) : 6969 static JWT_SECRET: string = process.env.JWT_SECRET || 'secret' + static ALLOW_DIAGONAL_MOVEMENT: boolean = process.env.ALLOW_DIAGONAL_MOVEMENT === 'true' + static DEFAULT_CHARACTER_ZONE: number = parseInt(process.env.DEFAULT_CHARACTER_ZONE || '1') static DEFAULT_CHARACTER_X: number = parseInt(process.env.DEFAULT_CHARACTER_POS_X || '0') static DEFAULT_CHARACTER_Y: number = parseInt(process.env.DEFAULT_CHARACTER_POS_Y || '0')