aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.ts
diff options
context:
space:
mode:
authorMaksymilian Jopek <maks@jopek.eu>2022-04-21 13:44:38 +0200
committerMaksymilian Jopek <maks@jopek.eu>2022-04-21 15:55:51 +0200
commitca7f97e765024b3ba279f2c4485e28add7b7e1a7 (patch)
treef696510846b2654151ba99aebafcf5bb5445e254 /src/map.ts
parent6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925 (diff)
downloadboules-ca7f97e765024b3ba279f2c4485e28add7b7e1a7.tar.gz
boules-ca7f97e765024b3ba279f2c4485e28add7b7e1a7.tar.zst
boules-ca7f97e765024b3ba279f2c4485e28add7b7e1a7.zip
v1.0.0
Diffstat (limited to 'src/map.ts')
-rw-r--r--src/map.ts43
1 files changed, 39 insertions, 4 deletions
diff --git a/src/map.ts b/src/map.ts
index 9fa85ef..72c6778 100644
--- a/src/map.ts
+++ b/src/map.ts
@@ -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 =>