1
0
forked from noxious/server
This commit is contained in:
2024-08-22 20:00:37 +02:00
parent ff7664bae0
commit c4b50ec811
16 changed files with 97 additions and 80 deletions

View File

@ -30,7 +30,7 @@ export default function (socket: TSocket, io: Server) {
const character: Character = await prisma.character.create({
data: {
name: data.name,
userId: user_id,
userId: user_id
// characterTypeId: 1 // @TODO set to chosen character type
}
})

View File

@ -8,7 +8,7 @@ import Rotation from '../../utilities/player/rotation'
const moveTokens = new Map<number, symbol>()
export default function setupCharacterMove(socket: TSocket, io: Server) {
socket.on('character:move', async ({ position_x, position_y }: { position_x: number; position_y: number }) => {
socket.on('character:move', async ({ positionX, positionY }: { positionX: number; positionY: number }) => {
const { character } = socket
if (!character) return console.error('character:move error', 'Character not found')
@ -16,8 +16,8 @@ export default function setupCharacterMove(socket: TSocket, io: Server) {
const grid = await ZoneManager.getGrid(character.zoneId)
if (!grid?.length) return console.error('character:move error', 'Grid not found or empty')
const start = { x: Math.floor(character.position_x), y: Math.floor(character.position_y) }
const end = { x: Math.floor(position_x), y: Math.floor(position_y) }
const start = { x: Math.floor(character.positionX), y: Math.floor(character.positionY) }
const end = { x: Math.floor(positionX), y: Math.floor(positionY) }
if (isObstacle(end, grid)) return socket.emit('character:moveError', 'Destination is an obstacle')
@ -80,17 +80,14 @@ const interpolatePosition = (start: { x: number; y: number }, end: { x: number;
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
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 })
Object.assign(character, { positionX: x, positionY: y, rotation })
ZoneManager.updateCharacterInZone(character.zoneId, character)
await prisma.character.update({
where: { id: character.id },
data: { position_x: x, position_y: y, rotation }
data: { positionX: x, positionY: y, rotation }
})
}