Skip to content

Commit 76d8552

Browse files
committed
fixed get closest tile
1 parent e619439 commit 76d8552

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/main/java/dev/zwazel/internal/debug/MapVisualiser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@Data
1919
public class MapVisualiser extends JPanel {
2020
private final PublicGameWorld world;
21-
private final int CELL_SIZE = 50;
21+
private int CELL_SIZE = 50; // Will be scaled by the map size, to fit the window
2222
private DrawingMode drawingMode = DrawingMode.HEIGHT;
2323
private LinkedList<Node> path = new LinkedList<>();
2424
private Graph graph;
@@ -27,6 +27,13 @@ public void showMap() {
2727
int width = ((int) world.getGameConfig().mapDefinition().width() + 1) * CELL_SIZE;
2828
int height = ((int) world.getGameConfig().mapDefinition().depth() + 1) * CELL_SIZE;
2929

30+
// scale down cell_size until we don't go over the max window size (1000x1000)
31+
while (width > 1000 || height > 1000) {
32+
CELL_SIZE--;
33+
width = ((int) world.getGameConfig().mapDefinition().width() + 1) * CELL_SIZE;
34+
height = ((int) world.getGameConfig().mapDefinition().depth() + 1) * CELL_SIZE;
35+
}
36+
3037
JFrame frame = new JFrame("Map Visualiser");
3138
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3239
frame.setSize(width, height);

src/main/java/dev/zwazel/internal/game/map/MapDefinition.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dev.zwazel.internal.game.misc.SimplifiedRGB;
44
import dev.zwazel.internal.game.transform.Vec3;
5-
import lombok.NonNull;
65

76
import java.util.ArrayList;
87
import java.util.Arrays;
@@ -25,35 +24,29 @@ public record MapDefinition(long width, long depth, SimplifiedRGB floorColor,
2524
* @param worldPos the world position
2625
* @return the closest tile to the world position, as a Vec3.
2726
*/
28-
public Vec3 getClosestTileFromWorld(@NonNull Vec3 worldPos) {
27+
public Vec3 getClosestTileFromWorld(Vec3 worldPos) {
2928
if (worldPos.getY() < 0) {
3029
throw new IllegalArgumentException("Y coordinate of the world position must be positive");
3130
}
3231

33-
ArrayList<Vec3> grid = gridToWorld();
32+
double minDistance = Double.POSITIVE_INFINITY;
3433
Vec3 closest = null;
35-
int closestIndex = -1;
3634

37-
for (int i = 0; i < grid.size(); i++) {
38-
Vec3 tile = grid.get(i);
35+
for (Vec3 tile : gridToWorld()) {
3936
double distance = tile.distance(worldPos);
4037

4138
if (distance < TILE_SIZE / 2) {
42-
closestIndex = i;
43-
break;
39+
return getTile((int) tile.getX(), (int) tile.getZ());
4440
}
4541

46-
if (closest == null || distance < closest.distance(worldPos)) {
42+
if (distance < minDistance) {
43+
minDistance = distance;
4744
closest = tile;
48-
closestIndex = i;
4945
}
5046
}
5147

52-
// Translate the index to the grid coordinates
53-
int x = closestIndex / (int) width;
54-
int y = closestIndex % (int) width;
55-
56-
return getTile(x, y);
48+
assert closest != null;
49+
return getTile((int) closest.getX(), (int) closest.getZ());
5750
}
5851

5952
/**
@@ -63,9 +56,9 @@ public Vec3 getClosestTileFromWorld(@NonNull Vec3 worldPos) {
6356
*/
6457
public ArrayList<Vec3> gridToWorld() {
6558
ArrayList<Vec3> grid = new ArrayList<>();
66-
for (int x = 0; x < width; x++) {
67-
for (int z = 0; z < depth; z++) {
68-
grid.add(getWorldTileCenter(x, z));
59+
for (int y = 0; y < depth; y++) {
60+
for (int x = 0; x < width; x++) {
61+
grid.add(getWorldTileCenter(x, y));
6962
}
7063
}
7164
return grid;
@@ -79,7 +72,7 @@ public ArrayList<Vec3> gridToWorld() {
7972
* @return the world coordinate of the center of the tile
8073
*/
8174
public Vec3 getWorldTileCenter(int x, int y) {
82-
return new Vec3(x + TILE_SIZE / 2, tiles[y][x], y + TILE_SIZE / 2);
75+
return new Vec3(x + 0.5f, getTileHeight(x, y), y + 0.5f);
8376
}
8477

8578
/**
@@ -90,7 +83,7 @@ public Vec3 getWorldTileCenter(int x, int y) {
9083
* @return the height of the tile
9184
*/
9285
public float getTileHeight(int x, int y) {
93-
return tiles[x][y];
86+
return tiles[y][x];
9487
}
9588

9689
/**
@@ -120,7 +113,7 @@ public Vec3 getTile(int x, int y) {
120113
throw new IllegalArgumentException("Invalid grid coordinates: (" + x + ", " + y + ")");
121114
}
122115

123-
return new Vec3(x, tiles[y][x], y);
116+
return new Vec3(x, getTileHeight(x, y), y);
124117
}
125118

126119
/**

0 commit comments

Comments
 (0)