forked from noxious/server
New move mechanic
This commit is contained in:
parent
fd3e503966
commit
cd538bc4db
@ -5,6 +5,7 @@ import prisma from '../../utilities/Prisma'
|
||||
import { AStar, type Node } from '../../utilities/Player/AStar'
|
||||
import Rotation from '../../utilities/Player/Rotation'
|
||||
import { ExtendedCharacter as Character } from '../../utilities/Types'
|
||||
import { start } from 'node:repl'
|
||||
|
||||
interface SocketResponse {
|
||||
position_x: number
|
||||
@ -62,44 +63,58 @@ export default function setupCharacterMove(socket: TSocket, io: Server) {
|
||||
async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: number[][], moveToken: Symbol) {
|
||||
if (!socket.character) return
|
||||
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
// Check if this movement has been cancelled
|
||||
if (characterMoveTokens.get(socket.character.id) !== moveToken) {
|
||||
console.log('Movement cancelled for character', socket.character.id)
|
||||
return
|
||||
const totalSteps = path.length
|
||||
const updateInterval = 50 // milliseconds between updates
|
||||
const totalDuration = totalSteps * 250 // total duration of movement
|
||||
|
||||
for (let step = 0; step < totalSteps; step++) {
|
||||
const startTime = Date.now()
|
||||
|
||||
while (Date.now() - startTime < 250) { // 250ms per tile
|
||||
if (characterMoveTokens.get(socket.character.id) !== moveToken) {
|
||||
console.log('Movement cancelled for character', socket.character.id)
|
||||
return
|
||||
}
|
||||
|
||||
const progress = (Date.now() - startTime) / 250
|
||||
const currentPosition = interpolatePosition(path[step], path[step + 1] || path[step], progress)
|
||||
|
||||
if (isObstacle(currentPosition, grid)) {
|
||||
console.log('Obstacle encountered at', currentPosition)
|
||||
break
|
||||
}
|
||||
|
||||
const rotation = step < totalSteps - 1
|
||||
? Rotation.calculate(path[step].x, path[step].y, path[step + 1].x, path[step + 1].y)
|
||||
: socket.character.rotation
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
const position = path[i]
|
||||
if (isObstacle(position, grid)) {
|
||||
console.log('Obstacle encountered at', position)
|
||||
break
|
||||
}
|
||||
|
||||
// Calculate rotation based on the next position in the path
|
||||
let rotation = socket.character.rotation
|
||||
if (i < path.length - 1) {
|
||||
const nextPosition = path[i + 1]
|
||||
rotation = Rotation.calculate(position.x, position.y, nextPosition.x, nextPosition.y)
|
||||
}
|
||||
|
||||
await updateCharacterPosition(socket.character, position.x, position.y, rotation)
|
||||
|
||||
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
|
||||
// Add a small delay between moves to avoid overwhelming the server
|
||||
await new Promise((resolve) => setTimeout(resolve, 250))
|
||||
}
|
||||
|
||||
// Movement complete
|
||||
if (socket.character) {
|
||||
socket.character.isMoving = false
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.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
|
||||
}
|
||||
}
|
||||
|
||||
function isObstacle(position: Node, grid: number[][]): boolean {
|
||||
return grid[position.y][position.x] === 1
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user