diff options
author | pacien | 2018-01-14 16:36:04 +0100 |
---|---|---|
committer | pacien | 2018-01-14 16:36:04 +0100 |
commit | a494bba689e1a95dd17287287c6394cb7a0cb7ef (patch) | |
tree | 2bbf708ea7b6ed7c9ed4b89e32dd0c90c0f95cdd /src/main/java/fr | |
parent | 423f74f694476b95fdfce608de07e3c02feed08a (diff) | |
download | wallj-a494bba689e1a95dd17287287c6394cb7a0cb7ef.tar.gz |
Move neighbour tile list
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java/fr')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/PathFinder.java | 9 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/TileVec2.java | 15 |
2 files changed, 17 insertions, 7 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java index 9c509f3..8dbab2d 100644 --- a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java +++ b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java | |||
@@ -12,11 +12,6 @@ import java.util.stream.Collectors; | |||
12 | public class PathFinder { | 12 | public class PathFinder { |
13 | 13 | ||
14 | private static final int LEAP_COST = 1; | 14 | private static final int LEAP_COST = 1; |
15 | private static final List<TileVec2> NEIGHBOR_OFFSETS = Arrays.asList( | ||
16 | TileVec2.of(0, -1), | ||
17 | TileVec2.of(-1, 0), | ||
18 | TileVec2.of(0, 1), | ||
19 | TileVec2.of(1, 0)); | ||
20 | 15 | ||
21 | private static class Node<T> { | 16 | private static class Node<T> { |
22 | final T val; | 17 | final T val; |
@@ -99,8 +94,8 @@ public class PathFinder { | |||
99 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | 94 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
100 | 95 | ||
101 | for (Node<TileVec2> node : map.values()) | 96 | for (Node<TileVec2> node : map.values()) |
102 | NEIGHBOR_OFFSETS.stream() | 97 | node.val.neighbors().stream() |
103 | .map(offsetVector -> map.get(node.val.add(offsetVector))) | 98 | .map(map::get) |
104 | .filter(Objects::nonNull) | 99 | .filter(Objects::nonNull) |
105 | .forEach(neighbor -> node.neighbors.put(neighbor, LEAP_COST)); | 100 | .forEach(neighbor -> node.neighbors.put(neighbor, LEAP_COST)); |
106 | 101 | ||
diff --git a/src/main/java/fr/umlv/java/wallj/board/TileVec2.java b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java index 70beed9..82200a7 100644 --- a/src/main/java/fr/umlv/java/wallj/board/TileVec2.java +++ b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java | |||
@@ -2,7 +2,10 @@ package fr.umlv.java.wallj.board; | |||
2 | 2 | ||
3 | import org.jbox2d.common.Vec2; | 3 | import org.jbox2d.common.Vec2; |
4 | 4 | ||
5 | import java.util.Arrays; | ||
6 | import java.util.List; | ||
5 | import java.util.Objects; | 7 | import java.util.Objects; |
8 | import java.util.stream.Collectors; | ||
6 | 9 | ||
7 | /** | 10 | /** |
8 | * A typed immutable tile coordinate vector containing the coordinates of a Tile in a Board. | 11 | * A typed immutable tile coordinate vector containing the coordinates of a Tile in a Board. |
@@ -12,6 +15,11 @@ import java.util.Objects; | |||
12 | public final class TileVec2 { | 15 | public final class TileVec2 { |
13 | 16 | ||
14 | private static final int TILE_DIM = 20; | 17 | private static final int TILE_DIM = 20; |
18 | private static final List<TileVec2> NEIGHBOR_OFFSETS = Arrays.asList( | ||
19 | of(0, -1), | ||
20 | of(-1, 0), | ||
21 | of(0, 1), | ||
22 | of(1, 0)); | ||
15 | 23 | ||
16 | /** | 24 | /** |
17 | * @param col the column | 25 | * @param col the column |
@@ -62,6 +70,13 @@ public final class TileVec2 { | |||
62 | return TileVec2.of(col + v.col, row + v.row); | 70 | return TileVec2.of(col + v.col, row + v.row); |
63 | } | 71 | } |
64 | 72 | ||
73 | /** | ||
74 | * @return a list of the surrounding direct neighbours of this tile | ||
75 | */ | ||
76 | public List<TileVec2> neighbors() { | ||
77 | return NEIGHBOR_OFFSETS.stream().map(this::add).collect(Collectors.toList()); | ||
78 | } | ||
79 | |||
65 | @Override | 80 | @Override |
66 | public boolean equals(Object o) { | 81 | public boolean equals(Object o) { |
67 | if (this == o) return true; | 82 | if (this == o) return true; |