1
0
forked from noxious/server

A* and move logic improvements

This commit is contained in:
2024-08-20 21:33:57 +02:00
parent 079d9408a8
commit acc9eaae9e
3 changed files with 29 additions and 15 deletions

View File

@ -40,21 +40,32 @@ async function moveAlongPath(socket: TSocket, io: Server, path: Array<{ x: numbe
for (let i = 0; i < path.length - 1; i++) {
const startTime = Date.now()
const start = path[i]
const end = path[i + 1]
while (Date.now() - startTime < stepDuration) {
if (moveTokens.get(character.id) !== moveToken) return
const progress = (Date.now() - startTime) / stepDuration
const current = interpolatePosition(path[i], path[i + 1], progress)
const current = interpolatePosition(start, end, progress)
if (isObstacle(current, grid)) break
if (isObstacle(current, grid)) {
// If obstacle encountered, stop at the last valid position
await updateCharacter(character, start, Rotation.calculate(start.x, start.y, end.x, end.y))
io.in(character.zoneId.toString()).emit('character:moved', character)
return
}
await updateCharacter(character, current, Rotation.calculate(path[i].x, path[i].y, path[i + 1].x, path[i + 1].y))
await updateCharacter(character, current, Rotation.calculate(start.x, start.y, end.x, end.y))
io.in(character.zoneId.toString()).emit('character:moved', character)
await new Promise((resolve) => setTimeout(resolve, updateInterval))
}
}
// Ensure the character reaches the exact final position
if (moveTokens.get(character.id) === moveToken) {
const finalPosition = path[path.length - 1]
await updateCharacter(character, finalPosition, character.rotation)
character.isMoving = false
moveTokens.delete(character.id)
io.in(character.zoneId.toString()).emit('character:moved', character)
@ -66,7 +77,14 @@ const interpolatePosition = (start: { x: number; y: number }, end: { x: number;
y: start.y + (end.y - start.y) * progress
})
const isObstacle = ({ x, y }: { x: number; y: number }, grid: number[][]) => grid[Math.floor(y)]?.[Math.floor(x)] === 1
const isObstacle = ({ x, y }: { x: number; y: number }, grid: number[][]) => {
const gridX = Math.floor(x)
const gridY = Math.floor(y)
return grid[gridY]?.[gridX] === 1 ||
grid[gridY]?.[Math.ceil(x)] === 1 ||
grid[Math.ceil(y)]?.[gridX] === 1 ||
grid[Math.ceil(y)]?.[Math.ceil(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 })