npm run format, improved a* and move event
This commit is contained in:
@ -1,145 +1,78 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../utilities/Types'
|
||||
import { TSocket, ExtendedCharacter } from '../../utilities/Types'
|
||||
import ZoneManager from '../../managers/ZoneManager'
|
||||
import prisma from '../../utilities/Prisma'
|
||||
import { AStar, type Node } from '../../utilities/Player/AStar'
|
||||
import { AStar } from '../../utilities/Player/AStar'
|
||||
import Rotation from '../../utilities/Player/Rotation'
|
||||
import { ExtendedCharacter as Character } from '../../utilities/Types'
|
||||
|
||||
interface SocketResponse {
|
||||
position_x: number
|
||||
position_y: number
|
||||
}
|
||||
|
||||
const characterMoveTokens = new Map<number, Symbol>()
|
||||
const moveTokens = new Map<number, symbol>()
|
||||
|
||||
export default function setupCharacterMove(socket: TSocket, io: Server) {
|
||||
socket.on('character:move', async (data: SocketResponse) => {
|
||||
try {
|
||||
console.log('character:move requested', data)
|
||||
socket.on('character:move', async ({ position_x, position_y }: { position_x: number; position_y: number }) => {
|
||||
const { character } = socket
|
||||
if (!character) return console.error('character:move error', 'Character not found')
|
||||
|
||||
if (!socket.character) {
|
||||
console.error('character:move error', 'Character not found')
|
||||
return
|
||||
}
|
||||
moveTokens.set(character.id, Symbol('moveToken'))
|
||||
const grid = await ZoneManager.getGrid(character.zoneId)
|
||||
if (!grid?.length) return console.error('character:move error', 'Grid not found or empty')
|
||||
|
||||
const oldMoveToken = characterMoveTokens.get(socket.character.id)
|
||||
if (oldMoveToken) {
|
||||
characterMoveTokens.delete(socket.character.id)
|
||||
}
|
||||
const start = { x: Math.floor(character.position_x), y: Math.floor(character.position_y) }
|
||||
const end = { x: Math.floor(position_x), y: Math.floor(position_y) }
|
||||
|
||||
const moveToken = Symbol('moveToken')
|
||||
characterMoveTokens.set(socket.character.id, moveToken)
|
||||
if (isObstacle(end, grid)) return socket.emit('character:moveError', 'Destination is an obstacle')
|
||||
|
||||
const grid = await ZoneManager.getGrid(socket.character.zoneId)
|
||||
const path = AStar.findPath(start, end, grid)
|
||||
if (!path.length) return socket.emit('character:moveError', 'No valid path found')
|
||||
|
||||
if (!grid || grid.length === 0) {
|
||||
console.error('character:move error', 'Grid not found or empty')
|
||||
return
|
||||
}
|
||||
|
||||
const start = { x: Math.floor(socket.character.position_x), y: Math.floor(socket.character.position_y) }
|
||||
const end = { x: Math.floor(data.position_x), y: Math.floor(data.position_y) }
|
||||
|
||||
console.log('Pathfinding from', start, 'to', end)
|
||||
console.log('Grid dimensions:', grid.length, 'x', grid[0].length)
|
||||
|
||||
// Check if the destination is an obstacle
|
||||
if (isObstacle(end, grid)) {
|
||||
console.log('character:move error', 'Destination is an obstacle')
|
||||
socket.emit('character:moveError', 'Destination is an obstacle')
|
||||
return
|
||||
}
|
||||
|
||||
const path = AStar.findPath(start, end, grid)
|
||||
|
||||
if (path.length > 0) {
|
||||
socket.character.isMoving = true
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
|
||||
moveAlongPath(socket, io, path, grid, moveToken).catch(console.error)
|
||||
} else {
|
||||
console.log('character:move error', 'No valid path found')
|
||||
socket.emit('character:moveError', 'No valid path found')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('character:move error', error)
|
||||
if (socket.character) {
|
||||
socket.character.isMoving = false
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
}
|
||||
}
|
||||
character.isMoving = true
|
||||
io.in(character.zoneId.toString()).emit('character:moved', character)
|
||||
moveAlongPath(socket, io, path, grid).catch(console.error)
|
||||
})
|
||||
}
|
||||
|
||||
async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: number[][], moveToken: Symbol) {
|
||||
if (!socket.character) return
|
||||
async function moveAlongPath(socket: TSocket, io: Server, path: Array<{ x: number; y: number }>, grid: number[][]) {
|
||||
const { character } = socket
|
||||
if (!character) return
|
||||
|
||||
const totalSteps = path.length
|
||||
const updateInterval = 50 // milliseconds between updates
|
||||
|
||||
for (let step = 0; step < totalSteps - 1; step++) {
|
||||
if (characterMoveTokens.get(socket.character.id) !== moveToken) {
|
||||
console.log('Movement cancelled for character', socket.character.id)
|
||||
return
|
||||
}
|
||||
const moveToken = moveTokens.get(character.id)
|
||||
const stepDuration = 250
|
||||
const updateInterval = 50
|
||||
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
const startTime = Date.now()
|
||||
const stepDuration = 250 // 250ms per tile
|
||||
|
||||
while (Date.now() - startTime < stepDuration) {
|
||||
if (characterMoveTokens.get(socket.character.id) !== moveToken) {
|
||||
console.log('Movement cancelled for character', socket.character.id)
|
||||
return
|
||||
}
|
||||
if (moveTokens.get(character.id) !== moveToken) return
|
||||
|
||||
const progress = (Date.now() - startTime) / stepDuration
|
||||
const currentPosition = interpolatePosition(path[step], path[step + 1], progress)
|
||||
const current = interpolatePosition(path[i], path[i + 1], progress)
|
||||
|
||||
if (isObstacle(currentPosition, grid)) {
|
||||
console.log('Obstacle encountered at', currentPosition)
|
||||
break
|
||||
}
|
||||
if (isObstacle(current, grid)) break
|
||||
|
||||
const rotation = Rotation.calculate(path[step].x, path[step].y, path[step + 1].x, path[step + 1].y)
|
||||
|
||||
await updateCharacterPosition(socket.character, currentPosition.x, currentPosition.y, rotation)
|
||||
|
||||
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, updateInterval))
|
||||
await updateCharacter(character, current, Rotation.calculate(path[i].x, path[i].y, path[i + 1].x, path[i + 1].y))
|
||||
io.in(character.zoneId.toString()).emit('character:moved', character)
|
||||
await new Promise((resolve) => setTimeout(resolve, updateInterval))
|
||||
}
|
||||
}
|
||||
|
||||
if (socket.character && characterMoveTokens.get(socket.character.id) === moveToken) {
|
||||
socket.character.isMoving = false
|
||||
characterMoveTokens.delete(socket.character.id)
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
if (moveTokens.get(character.id) === moveToken) {
|
||||
character.isMoving = false
|
||||
moveTokens.delete(character.id)
|
||||
io.in(character.zoneId.toString()).emit('character:moved', character)
|
||||
}
|
||||
}
|
||||
|
||||
function interpolatePosition(start: Node, end: Node, progress: number): Node {
|
||||
return {
|
||||
f: 0, g: 0, h: 0,
|
||||
x: start.x + (end.x - start.x) * progress,
|
||||
y: start.y + (end.y - start.y) * progress
|
||||
}
|
||||
}
|
||||
const interpolatePosition = (start: { x: number; y: number }, end: { x: number; y: number }, progress: number) => ({
|
||||
x: start.x + (end.x - start.x) * progress,
|
||||
y: start.y + (end.y - start.y) * progress
|
||||
})
|
||||
|
||||
function isObstacle(position: { x: number; y: number }, grid: number[][]): boolean {
|
||||
const x = Math.floor(position.x)
|
||||
const y = Math.floor(position.y)
|
||||
return grid[y] && grid[y][x] === 1
|
||||
}
|
||||
|
||||
async function updateCharacterPosition(character: Character, x: number, y: number, rotation: number) {
|
||||
character.position_x = x
|
||||
character.position_y = y
|
||||
character.rotation = rotation
|
||||
const isObstacle = ({ x, y }: { x: number; y: number }, grid: number[][]) => grid[Math.floor(y)]?.[Math.floor(x)] === 1
|
||||
|
||||
async function updateCharacter(character: ExtendedCharacter, { x, y }: { x: number; y: number }, rotation: number) {
|
||||
Object.assign(character, { position_x: x, position_y: y, rotation })
|
||||
ZoneManager.updateCharacterInZone(character.zoneId, character)
|
||||
await prisma.character.update({
|
||||
where: { id: character.id },
|
||||
data: { position_x: x, position_y: y, rotation }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ export default function (socket: TSocket, io: Server) {
|
||||
const zone = await ZoneRepository.getById(character.zoneId)
|
||||
if (!zone) return
|
||||
|
||||
|
||||
callback(true)
|
||||
|
||||
io.to(zone.id.toString()).emit('chat:message', {
|
||||
|
Reference in New Issue
Block a user