forked from noxious/server
Better var. naming
This commit is contained in:
@ -38,7 +38,7 @@ export default class CharacterMove extends BaseEvent {
|
||||
await this.moveAlongPath(mapCharacter, path)
|
||||
}
|
||||
|
||||
private async moveAlongPath(mapCharacter: MapCharacter, path: Array<{ x: number; y: number }>): Promise<void> {
|
||||
private async moveAlongPath(mapCharacter: MapCharacter, path: Array<{ positionX: number; positionY: number }>): Promise<void> {
|
||||
const character = mapCharacter.getCharacter()
|
||||
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
@ -47,7 +47,7 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
|
||||
const [start, end] = [path[i], path[i + 1]]
|
||||
character.setRotation(CharacterService.calculateRotation(start.x, start.y, end.x, end.y))
|
||||
character.setRotation(CharacterService.calculateRotation(start.positionX, start.positionY, end.positionX, end.positionY))
|
||||
|
||||
const mapEventTileRepository = new MapEventTileRepository()
|
||||
const mapEventTile = await mapEventTileRepository.getEventTileByMapIdAndPosition(character.getMap().getId(), Math.floor(end.x), Math.floor(end.y))
|
||||
@ -59,7 +59,7 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
|
||||
// Update position first
|
||||
character.setPositionX(end.x).setPositionY(end.y)
|
||||
character.setPositionX(end.positionX).setPositionY(end.positionY)
|
||||
|
||||
// Then emit with the same properties
|
||||
this.io.in(character.map.id).emit('map:character:move', {
|
||||
|
@ -9,7 +9,7 @@ import TeleportService from '#services/teleportService'
|
||||
class MapCharacter {
|
||||
public readonly character: Character
|
||||
public isMoving: boolean = false
|
||||
public currentPath: Array<{ x: number; y: number }> | null = null
|
||||
public currentPath: Array<{ positionX: number; positionY: number }> | null = null
|
||||
|
||||
constructor(character: Character) {
|
||||
this.character = character
|
||||
|
@ -7,7 +7,7 @@ import SocketManager from '#managers/socketManager'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import MapRepository from '#repositories/mapRepository'
|
||||
|
||||
type Position = { x: number; y: number }
|
||||
type Position = { positionX: number; positionY: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
class CharacterService extends BaseService {
|
||||
@ -33,13 +33,13 @@ class CharacterService extends BaseService {
|
||||
}
|
||||
|
||||
const start: Position = {
|
||||
x: Math.floor(character.positionX),
|
||||
y: Math.floor(character.positionY)
|
||||
positionX: Math.floor(character.positionX),
|
||||
positionY: Math.floor(character.positionY)
|
||||
}
|
||||
|
||||
const end: Position = {
|
||||
x: Math.floor(targetX),
|
||||
y: Math.floor(targetY)
|
||||
positionX: Math.floor(targetX),
|
||||
positionY: Math.floor(targetY)
|
||||
}
|
||||
|
||||
return this.findPath(start, end, grid)
|
||||
@ -80,24 +80,24 @@ class CharacterService extends BaseService {
|
||||
private 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}`
|
||||
const getKey = (p: Position) => `${p.positionX},${p.positionY}`
|
||||
|
||||
while (openList.length > 0) {
|
||||
const current = openList.reduce((min, node) => (node.f < min.f ? node : min))
|
||||
if (current.x === end.x && current.y === end.y) return this.reconstructPath(current)
|
||||
if (current.positionX === end.positionX && current.positionY === end.positionY) return this.reconstructPath(current)
|
||||
|
||||
openList.splice(openList.indexOf(current), 1)
|
||||
closedSet.add(getKey(current))
|
||||
|
||||
const neighbors = this.DIRECTIONS.slice(0, config.ALLOW_DIAGONAL_MOVEMENT ? 8 : 4)
|
||||
.map((dir) => ({ x: current.x + dir.x, y: current.y + dir.y }))
|
||||
.map((dir) => ({ positionX: current.positionX + dir.x, positionY: current.positionY + dir.y }))
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end))
|
||||
|
||||
for (const neighbor of neighbors) {
|
||||
if (closedSet.has(getKey(neighbor))) continue
|
||||
|
||||
const g = current.g + this.getDistance(current, neighbor)
|
||||
const existing = openList.find((node) => node.x === neighbor.x && node.y === neighbor.y)
|
||||
const existing = openList.find((node) => node.positionX === neighbor.positionX && node.positionY === neighbor.positionY)
|
||||
|
||||
if (!existing || g < existing.g) {
|
||||
const h = this.getDistance(neighbor, end)
|
||||
@ -112,12 +112,12 @@ class CharacterService extends BaseService {
|
||||
}
|
||||
|
||||
private isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
|
||||
return pos.x >= 0 && pos.y >= 0 && pos.x < grid[0].length && pos.y < grid.length && (grid[pos.y][pos.x] === 0 || (pos.x === end.x && pos.y === end.y))
|
||||
return pos.positionX >= 0 && pos.positionY >= 0 && pos.positionX < grid[0].length && pos.positionY < grid.length && (grid[pos.positionY][pos.positionX] === 0 || (pos.positionX === end.positionX && pos.positionY === end.positionY))
|
||||
}
|
||||
|
||||
private getDistance(a: Position, b: Position): number {
|
||||
const dx = Math.abs(a.x - b.x),
|
||||
dy = Math.abs(a.y - b.y)
|
||||
const dx = Math.abs(a.positionX - b.positionX),
|
||||
dy = Math.abs(a.positionY - b.positionY)
|
||||
// Manhattan distance for straight paths, then Euclidean for diagonals
|
||||
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
|
||||
}
|
||||
|
Reference in New Issue
Block a user