import { Direction } from '../direction/direction.class' ; import { ToString } from '../to-string.interface' ; import { Vec2 } from '../vector/vec2.class' ; import { GridGraph } from './grid-graph.class' ; import { GridNode } from './grid-node.class' ; import { Weighter } from './heuristic.type' ; import { PortalGridNode } from './portal-grid-node.class' ; export class PortalGridGraph < T extends ToString , N extends GridNode < T > = PortalGridNode < T > > extends GridGraph < T , N > { public constructor ( ) { super ( ) ; } public static fromTorus < T = string , N extends GridNode < T > = PortalGridNode < T > > ( matrix , options ) { const weigther = options . weighter ?? ( ( a , b ) => ( a . value !== b . value ? Infinity : 0 ) ) ; const connectionDirections = options ?. connectionDirections ?? Direction . cardinalDirections ; const graph = new PortalGridGraph < T , N > ( ) ; for ( let y = 0 ; y < matrix . length ; y ++ ) { const row = matrix [ y ] ; for ( let x = 0 ; x < row . length ; x ++ ) { const v = row [ x ] ; const p = new Vec2 ( x , y ) ; if ( ! options . filter || options . filter ( p ) ) { const node = new PortalGridNode < T > ( p , options . portalOf ( p ) , v ) ; node . attachNeightbours ( graph as any , connectionDirections , weigther ) ; } } } return graph ; } }