More TP work

This commit is contained in:
2024-08-26 21:22:03 +02:00
parent e8d100e063
commit 1aa3d1a208
5 changed files with 68 additions and 49 deletions

View File

@ -0,0 +1,17 @@
import { ExtendedCharacter } from './types'
import ZoneManager from '../managers/zoneManager'
export class MovementValidator {
public async isValidMove(character: ExtendedCharacter, position: { x: number; y: number }): Promise<boolean> {
const grid = await ZoneManager.getGrid(character.zoneId)
if (!grid?.length) return false
return !this.isObstacle(position, grid)
}
private isObstacle({ x, y }: { x: number; y: number }, grid: number[][]): boolean {
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
}
}