forked from noxious/server
Worked on character animations
This commit is contained in:
@ -13,19 +13,13 @@ export interface Node extends Position {
|
||||
/**
|
||||
* A* pathfinding algorithm.
|
||||
*/
|
||||
class AStar {
|
||||
export class AStar {
|
||||
private static readonly ORTHOGONAL_DIRECTIONS: Position[] = [
|
||||
{ x: 0, y: -1 }, // up
|
||||
{ x: 0, y: 1 }, // down
|
||||
{ x: -1, y: 0 }, // left
|
||||
{ x: 1, y: 0 } // right
|
||||
{ x: 0, y: -1 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 1, y: 0 }
|
||||
]
|
||||
|
||||
private static readonly DIAGONAL_DIRECTIONS: Position[] = [
|
||||
{ x: -1, y: -1 }, // up-left
|
||||
{ x: -1, y: 1 }, // down-left
|
||||
{ x: 1, y: -1 }, // up-right
|
||||
{ x: 1, y: 1 } // down-right
|
||||
{ x: -1, y: -1 }, { x: -1, y: 1 }, { x: 1, y: -1 }, { x: 1, y: 1 }
|
||||
]
|
||||
|
||||
/**
|
||||
@ -74,40 +68,27 @@ class AStar {
|
||||
return [] // No path found
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the node with the lowest F score from a list.
|
||||
*/
|
||||
private static getLowestFScoreNode(nodes: Node[]): Node {
|
||||
return nodes.reduce((min, node) => (node.f < min.f ? node : min))
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given node is the end node.
|
||||
*/
|
||||
private static isEndNode(node: Node, end: Position): boolean {
|
||||
return node.x === end.x && node.y === end.y
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a node from the open list.
|
||||
*/
|
||||
private static removeNodeFromOpenList(openList: Node[], node: Node): void {
|
||||
const index = openList.findIndex((n) => n.x === node.x && n.y === node.y)
|
||||
if (index !== -1) openList.splice(index, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a node to a string representation.
|
||||
*/
|
||||
private static nodeToString(node: Position): string {
|
||||
return `${node.x},${node.y}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets valid neighbors of the given node.
|
||||
*/
|
||||
private static getValidNeighbors(node: Node, grid: number[][], end: Position, allowDiagonal: boolean): Node[] {
|
||||
const directions = allowDiagonal ? [...this.ORTHOGONAL_DIRECTIONS, ...this.DIAGONAL_DIRECTIONS] : this.ORTHOGONAL_DIRECTIONS
|
||||
const directions = allowDiagonal
|
||||
? [...this.ORTHOGONAL_DIRECTIONS, ...this.DIAGONAL_DIRECTIONS]
|
||||
: this.ORTHOGONAL_DIRECTIONS
|
||||
|
||||
return directions
|
||||
.map((dir) => ({
|
||||
@ -117,50 +98,29 @@ class AStar {
|
||||
h: 0,
|
||||
f: 0
|
||||
}))
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end)) as Node[]
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end))
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given position is valid.
|
||||
*/
|
||||
private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
|
||||
const { x, y } = pos
|
||||
return x >= 0 && y >= 0 && x < grid.length && y < grid[0].length && (grid[y][x] === 0 || (x === end.x && y === end.y))
|
||||
return x >= 0 && y >= 0 && x < grid.length && y < grid[0].length &&
|
||||
(grid[y][x] === 0 || (x === end.x && y === end.y))
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given node is in the open list.
|
||||
*/
|
||||
private static isInOpenList(openList: Node[], node: Position): boolean {
|
||||
return openList.some((n) => n.x === node.x && n.y === node.y)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance between two positions.
|
||||
*/
|
||||
private static getDistance(a: Position, b: Position): number {
|
||||
const dx = Math.abs(a.x - b.x)
|
||||
const dy = Math.abs(a.y - b.y)
|
||||
|
||||
if (a.x === b.x || a.y === b.y) {
|
||||
// Orthogonal movement (horizontal/vertical)
|
||||
return Math.sqrt(dx * dx + dy * dy)
|
||||
} else {
|
||||
// Diagonal movement with cost sqrt(2)
|
||||
return Math.sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
return Math.sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
|
||||
/**
|
||||
* Heuristic function estimating the distance from a node to the goal.
|
||||
*/
|
||||
private static heuristic(node: Position, goal: Position): number {
|
||||
return this.getDistance(node, goal)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstructs the path from the end node.
|
||||
*/
|
||||
private static reconstructPath(endNode: Node): Node[] {
|
||||
const path: Node[] = []
|
||||
let currentNode: Node | undefined = endNode
|
||||
@ -172,6 +132,4 @@ class AStar {
|
||||
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
export default AStar
|
||||
}
|
@ -3,7 +3,7 @@ import { Character, User } from '@prisma/client'
|
||||
|
||||
export type TSocket = Socket & {
|
||||
user?: User
|
||||
character?: Character
|
||||
character?: ExtendedCharacter
|
||||
handshake?: {
|
||||
query?: {
|
||||
token?: any
|
||||
@ -16,13 +16,10 @@ export type TSocket = Socket & {
|
||||
}
|
||||
}
|
||||
|
||||
export type TCharacter = Socket & {
|
||||
user?: User
|
||||
character?: Character
|
||||
export type ExtendedCharacter = Character & {
|
||||
isMoving?: boolean
|
||||
}
|
||||
|
||||
export type TZoneCharacter = Character & {}
|
||||
|
||||
export type TAsset = {
|
||||
key: string
|
||||
url: string
|
||||
@ -30,3 +27,8 @@ export type TAsset = {
|
||||
frameWidth?: number
|
||||
frameHeight?: number
|
||||
}
|
||||
|
||||
// export type TCharacter = Socket & {
|
||||
// user?: User
|
||||
// character?: Character
|
||||
// }
|
Reference in New Issue
Block a user