Worked on pathfinding, character animation & rotation, few enhancements

This commit is contained in:
2024-07-29 09:01:54 +02:00
parent dbe25071c7
commit bbeac95512
5 changed files with 188 additions and 164 deletions

View File

@ -1,95 +1,96 @@
import { Server } from 'socket.io';
import { TSocket } from '../../utilities/Types';
import ZoneManager from '../../managers/ZoneManager';
import prisma from '../../utilities/Prisma';
import AStar, { type Node } from '../../utilities/Player/AStar';
import Rotation from '../../utilities/Player/Rotation';
import { Server } from 'socket.io'
import { TSocket } from '../../utilities/Types'
import ZoneManager from '../../managers/ZoneManager'
import prisma from '../../utilities/Prisma'
import AStar, { type Node } from '../../utilities/Player/AStar'
import Rotation from '../../utilities/Player/Rotation'
interface SocketResponse {
position_x: number;
position_y: number;
position_x: number
position_y: number
}
interface Character {
id: number;
position_x: number;
position_y: number;
rotation: number;
zoneId: number;
id: number
position_x: number
position_y: number
rotation: number
zoneId: number
}
export default function setupCharacterMove(socket: TSocket, io: Server) {
socket.on('character:move', async (data: SocketResponse) => {
try {
console.log('character:move requested', data);
console.log('character:move requested', data)
if (!socket.character) {
console.error('character:move error', 'Character not found');
return;
console.error('character:move error', 'Character not found')
return
}
const grid = await ZoneManager.getGrid(socket.character.zoneId);
const grid = await ZoneManager.getGrid(socket.character.zoneId)
if (grid.length === 0) {
console.error('character:move error', 'Grid not found');
return;
console.error('character:move error', 'Grid not found')
return
}
const start = { x: socket.character.position_x, y: socket.character.position_y };
const end = { x: data.position_x, y: data.position_y };
const start = { x: socket.character.position_x, y: socket.character.position_y }
const end = { x: data.position_x, y: data.position_y }
const path = AStar.findPath(start, end, grid);
const path = AStar.findPath(start, end, grid)
if (path.length > 0) {
await moveAlongPath(socket, io, path, grid);
await moveAlongPath(socket, io, path, grid)
} else {
console.log('character:move error', 'No valid path found');
console.log('character:move error', 'No valid path found')
}
} catch (error) {
console.error('character:move error', error);
console.error('character:move error', error)
}
});
})
}
async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: number[][]) {
if (!socket.character) return;
if (!socket.character) return
for (const position of path) {
for (let i = 0; i < path.length; i++) {
const position = path[i]
if (isObstacle(position, grid)) {
console.log('Obstacle encountered at', position);
break;
console.log('Obstacle encountered at', position)
break
}
const rotation = Rotation.calculate(
socket.character.position_x,
socket.character.position_y,
position.x,
position.y
);
// 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);
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);
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
console.log('Character moved to', position);
console.log('Character moved to', position)
// Add a small delay between moves to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 100));
await new Promise((resolve) => setTimeout(resolve, 100))
}
}
function isObstacle(position: Node, grid: number[][]): boolean {
return grid[position.y][position.x] === 1;
return grid[position.y][position.x] === 1
}
async function updateCharacterPosition(character: Character, x: number, y: number, rotation: number) {
character.position_x = x;
character.position_y = y;
character.rotation = rotation;
character.position_x = x
character.position_y = y
character.rotation = rotation
await prisma.character.update({
where: { id: character.id },
data: { position_x: x, position_y: y, rotation }
});
}
})
}