diff options
Diffstat (limited to 'src/map.ts')
| -rw-r--r-- | src/map.ts | 43 |
1 files changed, 39 insertions, 4 deletions
@@ -1,8 +1,22 @@ -import Ball from "./Ball"; +/** + * @module Map +*/ + +import { Ball } from "./Ball"; import { DELETE_FROM_BALLS, MAP_HEIGHT, MAP_WIDTH } from "./consts"; import { Graph } from "./pathfinder/Graph"; +import { clearTd } from "./ui"; -export function findToRemove(balls: Ball[]): Ball[] { +/** @internal */ +export interface BallsArray<T> extends Array<T> { + clear: () => void; +} +/** + * Finds ball that should be deleted + * @param balls - array of {@linkcode Ball} + * @return Balls to remove +*/ +export function findToRemove(balls: Ball[]): BallsArray<Ball> { const out = new Set<Ball>() for (const ball of balls) { const xNeighbours = new Set<Ball>(); @@ -60,10 +74,20 @@ export function findToRemove(balls: Ball[]): Ball[] { if (s.size >= DELETE_FROM_BALLS) s.forEach(b => out.add(b)) } } - return [...out]; + + const rout = [...out] as BallsArray<Ball>; + rout.clear = function() { + this.forEach(b => clearTd(b.td)) + } + return rout; } +/** Array of array of balls */ export type Map = (Ball | null)[][]; +/** + * Function that generates map (Array of Arrays) of balls from arrays of balls. Empty spots are null + * @param balls - array of {@linkcode Ball} +*/ export function genMap(balls: Array<Ball>): Map { const map = <Map>[]; for (let i = 0; i < MAP_HEIGHT; i++) { @@ -78,7 +102,17 @@ export function genMap(balls: Array<Ball>): Map { return map; } -type Coord = { x: number; y: number }; +/** + * Object of coordinates +*/ +export type Coord = { x: number; y: number }; + +/** + * Function that converts map to {@link Graph} + * @param map - map to be converted + * @param start - start of search + * @param goal - finsish of search +*/ export function genGraph(map: Map, start: Coord, goal: Coord): Graph { const graph: Graph = []; map.forEach((row, i) => { @@ -117,6 +151,7 @@ export function genGraph(map: Map, start: Coord, goal: Coord): Graph { return graph; } +/** Generates UUID4 */ function uuidv4() { // @ts-ignore return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => |
