/
adjacencyzone.js
67 lines (62 loc) · 2.36 KB
/
adjacencyzone.js
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
/**
* Created by nate on 1/7/17.
*/
/**
* This enum holds data regarding all eight possible directions that Spaces can have as AdjacencyZones
*/
var adjacencyDirections = {
none: new BoardCoordinate(0,0),
north: new BoardCoordinate(0,-1),
northEast: new BoardCoordinate(1,-1),
east: new BoardCoordinate(1,0),
southEast: new BoardCoordinate(1,1),
south: new BoardCoordinate(0,1),
southWest: new BoardCoordinate(-1,1),
west: new BoardCoordinate(-1,0),
northWest: new BoardCoordinate(-1,-1)
};
/**
* Creates an AdjacencyZone with the given direction
* @constructor
*/
function AdjacencyZone(givenDirection) {
if(adjacencyDirections[givenDirection] === undefined){
givenDirection = "none";
}
this.offsetBoardCoordinate = adjacencyDirections[givenDirection];
this.direction = givenDirection;
}
/**
* Gets the opposite AdjacencyZone based on the direction of this AdjacencyZone's BoardCoordinate
* @return Returns the opposite AdjacencyZone based on the direction of this AdjacencyZone's BoardCoordinate
*/
AdjacencyZone.prototype.getOppositeAdjacencyZone = function () {
var reverseBoardCoordinate = this.offsetBoardCoordinate.scalarMultiply(-1);
var oppositeAdjacencyZone = getAdjacencyZoneFromOffset(reverseBoardCoordinate);
return oppositeAdjacencyZone;
};
/**
* Gets the AdjacencyZone that matches the given BoardCoordinate in direciton
* @param offsetBoardCoordinate The BoardCoordinate to match to an AdjacencyZone
* @return Returns the AdjacencyZone that matches the given BoardCoordinate in direciton
*/
function getAdjacencyZoneFromOffset(offsetBoardCoordinate) {
for (var direction in adjacencyDirections){
if(adjacencyDirections[direction].equals(offsetBoardCoordinate)){
return new AdjacencyZone(direction);
}
}
}
/**
* Gets the list of all AdjacencyZones that have positive BoardCoordinate directions, not including AdjacencyZone.NONE
* @return Returns the list of all AdjacencyZones that have positive BoardCoordinate directions, not including AdjacencyZone.NONE
*/
var positiveAdjacencyZones = (function () {
var positiveAdjacencyZones = [];
for (var direction in adjacencyDirections){
if(adjacencyDirections[direction].isPositive()){
positiveAdjacencyZones.push(new AdjacencyZone(direction));
}
}
return positiveAdjacencyZones;
})();