-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasicFunctions.js
More file actions
102 lines (97 loc) · 2.56 KB
/
Copy pathbasicFunctions.js
File metadata and controls
102 lines (97 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//@flow
import { Map, List } from 'immutable';
import reduce from 'lodash.reduce';
import compact from 'lodash.compact';
import type { Cell, Grid, FlowField, Position, UpdateFunction } from './types';
function getNeighbours(
position: Position,
outOfBounds: Function,
grid: Grid
): Array<Position> {
if (outOfBounds(position)) {
return [];
}
const [x, y]: Position = position;
// neighbours order
// 1 8 7
// 2 6
// 3 4 5
const allNeighbours = reduce(
{
TL: [x - 1, y + 1],
L: [x - 1, y],
BL: [x - 1, y - 1],
B: [x, y - 1],
BR: [x + 1, y - 1],
R: [x + 1, y],
TR: [x + 1, y + 1],
T: [x, y + 1]
},
(acc: Object, position: Position, key: string): Object => {
if (!outOfBounds(position)) {
acc[key] = position;
}
return acc;
},
{}
);
let neighboursToConsider = compact([
allNeighbours.L,
allNeighbours.R,
allNeighbours.B,
allNeighbours.T
]);
const isLeftGood =
allNeighbours.L && !grid.getIn(allNeighbours.L).get('obstacle');
const isRightGood =
allNeighbours.R && !grid.getIn(allNeighbours.R).get('obstacle');
const areSidesGood = isLeftGood || isRightGood;
if (
areSidesGood &&
allNeighbours.T &&
!grid.getIn(allNeighbours.T).get('obstacle')
) {
isLeftGood && neighboursToConsider.push(allNeighbours.TL);
isRightGood && neighboursToConsider.push(allNeighbours.TR);
}
if (
areSidesGood &&
allNeighbours.B &&
!grid.getIn(allNeighbours.B).get('obstacle')
) {
isLeftGood && neighboursToConsider.push(allNeighbours.BL);
isRightGood && neighboursToConsider.push(allNeighbours.BR);
}
return neighboursToConsider;
}
//function to get tile indices
//based on a floating number
function getCorrectedTileIndices(target: Position, step: number): Position {
const [x, y]: Position = target;
if (step < 0 || x < 0 || y < 0) {
throw 'Error, one of the arguments is not positive';
}
return [Math.floor(target[0] / step), Math.floor(target[1] / step)];
}
/*
** (o) | x | (o)
** x | o | x
** (o) | x | (o)
*/
function generateOutOfBoundsFunction(xRange: number, yRange: number): Function {
if (!xRange || xRange < 1 || !yRange || yRange < 1) {
throw 'Wrong inputs';
}
return function(position: Position): boolean {
if (!Array.isArray(position) || position.length !== 2) {
return true;
}
const [x, y] = position;
return x < 0 || y < 0 || x > xRange - 1 || y > yRange - 1;
};
}
export default {
getNeighbours,
getCorrectedTileIndices,
generateOutOfBoundsFunction
};