1
0
forked from noxious/server

Prevent walk through obstacles

This commit is contained in:
Dennis Postma 2024-08-19 18:00:06 +02:00
parent 5ce844c919
commit 910d8ad8f1
2 changed files with 18 additions and 13 deletions

14
package-lock.json generated
View File

@ -622,12 +622,12 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.15.0", "version": "20.16.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.15.0.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.0.tgz",
"integrity": "sha512-eQf4OkH6gA9v1W0iEpht/neozCsZKMTK+C4cU6/fv7wtJCCL8LEQ4hie2Ln8ZP/0YYM2xGj7//f8xyqItkJ6QA==", "integrity": "sha512-vDxceJcoZhIVh67S568bm1UGZO0DX0hpplJZxzeXMKwIPLn190ec5RRxQ69BKhX44SUGIxxgMdDY557lGLKprQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"undici-types": "~6.13.0" "undici-types": "~6.19.2"
} }
}, },
"node_modules/@types/qs": { "node_modules/@types/qs": {
@ -2335,9 +2335,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/undici-types": { "node_modules/undici-types": {
"version": "6.13.0", "version": "6.19.6",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.13.0.tgz", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.6.tgz",
"integrity": "sha512-xtFJHudx8S2DSoujjMd1WeWvn7KKWFRESZTMeL1RptAYERu29D6jphMjjY+vn96jvN3kVPDNxU/E13VTaXj6jg==", "integrity": "sha512-e/vggGopEfTKSvj4ihnOLTsqhrKRN3LeO6qSN/GxohhuRv8qH9bNQ4B8W7e/vFL+0XTnmHPB4/kegunZGA4Org==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/unpipe": { "node_modules/unpipe": {

View File

@ -44,6 +44,13 @@ export default function setupCharacterMove(socket: TSocket, io: Server) {
console.log('Pathfinding from', start, 'to', end) console.log('Pathfinding from', start, 'to', end)
console.log('Grid dimensions:', grid.length, 'x', grid[0].length) 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) const path = AStar.findPath(start, end, grid)
if (path.length > 0) { if (path.length > 0) {
@ -71,7 +78,7 @@ async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: nu
const totalSteps = path.length const totalSteps = path.length
const updateInterval = 50 // milliseconds between updates const updateInterval = 50 // milliseconds between updates
for (let step = 0; step < totalSteps; step++) { for (let step = 0; step < totalSteps - 1; step++) {
if (characterMoveTokens.get(socket.character.id) !== moveToken) { if (characterMoveTokens.get(socket.character.id) !== moveToken) {
console.log('Movement cancelled for character', socket.character.id) console.log('Movement cancelled for character', socket.character.id)
return return
@ -87,16 +94,14 @@ async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: nu
} }
const progress = (Date.now() - startTime) / stepDuration const progress = (Date.now() - startTime) / stepDuration
const currentPosition = interpolatePosition(path[step], path[step + 1] || path[step], progress) const currentPosition = interpolatePosition(path[step], path[step + 1], progress)
if (isObstacle(currentPosition, grid)) { if (isObstacle(currentPosition, grid)) {
console.log('Obstacle encountered at', currentPosition) console.log('Obstacle encountered at', currentPosition)
break break
} }
const rotation = step < totalSteps - 1 const rotation = Rotation.calculate(path[step].x, path[step].y, path[step + 1].x, path[step + 1].y)
? 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) await updateCharacterPosition(socket.character, currentPosition.x, currentPosition.y, rotation)
@ -122,7 +127,7 @@ function interpolatePosition(start: Node, end: Node, progress: number): Node {
} }
} }
function isObstacle(position: Node, grid: number[][]): boolean { function isObstacle(position: { x: number; y: number }, grid: number[][]): boolean {
const x = Math.floor(position.x) const x = Math.floor(position.x)
const y = Math.floor(position.y) const y = Math.floor(position.y)
return grid[y] && grid[y][x] === 1 return grid[y] && grid[y][x] === 1