Fix character movement speed, fix for diagonal movement and disabled it by default

This commit is contained in:
Dennis Postma 2024-09-07 18:08:10 +02:00
parent 2ecc65b14c
commit 194c5d23af
6 changed files with 40 additions and 22 deletions

View File

@ -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"

View File

@ -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<void> {
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<void> {
const { character } = this.socket
if (!character) {
console.error('character:move error', 'Character not found')
logger.error('character:move error', 'Character not found')
return
}

View File

@ -52,6 +52,6 @@ export class CharacterMoveService {
}
public async applyMovementDelay(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 240)) // 50ms delay between steps
await new Promise((resolve) => setTimeout(resolve, 210)) // 50ms delay between steps
}
}

View File

@ -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<string>()
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))

View File

@ -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

View File

@ -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')