Skip to content

Commit 3111fdc

Browse files
committed
improved comments and fixed coordinate stuff, oops
1 parent 837488d commit 3111fdc

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public Vec3 getClosestTileFromWorld(@NonNull Vec3 worldPos) {
4545
}
4646

4747
// Translate the index to the grid coordinates
48-
int x = closestIndex % (int) width;
49-
int y = closestIndex / (int) width;
48+
int x = closestIndex / (int) width;
49+
int y = closestIndex % (int) width;
5050

51-
return new Vec3(x, y, 0);
51+
return getTile(x, y);
5252
}
5353

5454
/**
@@ -74,7 +74,7 @@ public ArrayList<Vec3> gridToWorld() {
7474
* @return the world coordinate of the center of the tile
7575
*/
7676
public Vec3 getWorldTileCenter(int x, int y) {
77-
return new Vec3(x + TILE_SIZE / 2, tiles[x][y], y + TILE_SIZE / 2);
77+
return new Vec3(x + TILE_SIZE / 2, tiles[y][x], y + TILE_SIZE / 2);
7878
}
7979

8080
/**
@@ -92,32 +92,44 @@ public float getTileHeight(int x, int y) {
9292
* Returns the height of the tile at the given grid coordinates.
9393
*
9494
* @param pos the grid coordinates of the tile, not world coordinates!
95+
* The y coordinate is the height of the tile.
96+
* The y coordinate is ignored in this method.
97+
* The z coordinate of the given Vec3 is the y coordinate of the tile in the 2D map grid.
98+
* The x coordinate of the given Vec3 is the x coordinate of the tile in the 2D map grid.
9599
* @return the height of the tile
96100
*/
97101
public float getTileHeight(Vec3 pos) {
98-
Vec3 getTile = getTile(pos);
99-
return getTileHeight((int) getTile.getX(), (int) getTile.getY());
102+
return getTileHeight((int) pos.getX(), (int) pos.getZ());
100103
}
101104

102105
/**
103106
* Returns the tile at the given grid coordinates as a Vec3.
104107
*
105108
* @param x the x coordinate of the tile in the map grid (0-indexed)
106109
* @param y the y coordinate of the tile in the map grid (0-indexed)
107-
* @return the tile as a Vec3 in grid coordinates. the z is always 0, as this is a 2D map.
110+
* @return the tile as a Vec3 in grid coordinates. the y is the height of the tile.
108111
*/
109112
public Vec3 getTile(int x, int y) {
110-
return new Vec3(x, y, 0);
113+
// Check if the coordinates are valid
114+
if (x < 0 || x >= width || y < 0 || y >= depth) {
115+
throw new IllegalArgumentException("Invalid grid coordinates: (" + x + ", " + y + ")");
116+
}
117+
118+
return new Vec3(x, tiles[y][x], y);
111119
}
112120

113121
/**
114122
* Returns the tile at the given grid coordinates as a Vec3.
115123
*
116124
* @param pos the grid coordinates of the tile, not world coordinates!
117-
* @return the tile as a Vec3 in grid coordinates. the z is always 0, as this is a 2D map.
125+
* The y coordinate is the height of the tile.
126+
* The y coordinate is ignored in this method.
127+
* The z coordinate of the given Vec3 is the y coordinate of the tile in the 2D map grid.
128+
* The x coordinate of the given Vec3 is the x coordinate of the tile in the 2D map grid.
129+
* @return the tile as a Vec3 in grid coordinates. the y is always 0, as this is a 2D map.
118130
*/
119131
public Vec3 getTile(Vec3 pos) {
120-
return getTile((int) pos.getX(), (int) pos.getY());
132+
return getTile((int) pos.getX(), (int) pos.getZ());
121133
}
122134

123135
@Override

0 commit comments

Comments
 (0)