forked from noxious/server
Prevent walk through obstacles
This commit is contained in:
parent
5ce844c919
commit
910d8ad8f1
14
package-lock.json
generated
14
package-lock.json
generated
@ -622,12 +622,12 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "20.15.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.15.0.tgz",
|
||||
"integrity": "sha512-eQf4OkH6gA9v1W0iEpht/neozCsZKMTK+C4cU6/fv7wtJCCL8LEQ4hie2Ln8ZP/0YYM2xGj7//f8xyqItkJ6QA==",
|
||||
"version": "20.16.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.0.tgz",
|
||||
"integrity": "sha512-vDxceJcoZhIVh67S568bm1UGZO0DX0hpplJZxzeXMKwIPLn190ec5RRxQ69BKhX44SUGIxxgMdDY557lGLKprQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~6.13.0"
|
||||
"undici-types": "~6.19.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/qs": {
|
||||
@ -2335,9 +2335,9 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "6.13.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.13.0.tgz",
|
||||
"integrity": "sha512-xtFJHudx8S2DSoujjMd1WeWvn7KKWFRESZTMeL1RptAYERu29D6jphMjjY+vn96jvN3kVPDNxU/E13VTaXj6jg==",
|
||||
"version": "6.19.6",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.6.tgz",
|
||||
"integrity": "sha512-e/vggGopEfTKSvj4ihnOLTsqhrKRN3LeO6qSN/GxohhuRv8qH9bNQ4B8W7e/vFL+0XTnmHPB4/kegunZGA4Org==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/unpipe": {
|
||||
|
@ -44,6 +44,13 @@ export default function setupCharacterMove(socket: TSocket, io: Server) {
|
||||
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) {
|
||||
@ -71,7 +78,7 @@ async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: nu
|
||||
const totalSteps = path.length
|
||||
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) {
|
||||
console.log('Movement cancelled for character', socket.character.id)
|
||||
return
|
||||
@ -87,16 +94,14 @@ async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: nu
|
||||
}
|
||||
|
||||
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)) {
|
||||
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
|
||||
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)
|
||||
|
||||
@ -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 y = Math.floor(position.y)
|
||||
return grid[y] && grid[y][x] === 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user