2
2
3
3
import dev .zwazel .internal .game .misc .SimplifiedRGB ;
4
4
import dev .zwazel .internal .game .transform .Vec3 ;
5
- import lombok .NonNull ;
6
5
7
6
import java .util .ArrayList ;
8
7
import java .util .Arrays ;
@@ -25,35 +24,29 @@ public record MapDefinition(long width, long depth, SimplifiedRGB floorColor,
25
24
* @param worldPos the world position
26
25
* @return the closest tile to the world position, as a Vec3.
27
26
*/
28
- public Vec3 getClosestTileFromWorld (@ NonNull Vec3 worldPos ) {
27
+ public Vec3 getClosestTileFromWorld (Vec3 worldPos ) {
29
28
if (worldPos .getY () < 0 ) {
30
29
throw new IllegalArgumentException ("Y coordinate of the world position must be positive" );
31
30
}
32
31
33
- ArrayList < Vec3 > grid = gridToWorld () ;
32
+ double minDistance = Double . POSITIVE_INFINITY ;
34
33
Vec3 closest = null ;
35
- int closestIndex = -1 ;
36
34
37
- for (int i = 0 ; i < grid .size (); i ++) {
38
- Vec3 tile = grid .get (i );
35
+ for (Vec3 tile : gridToWorld ()) {
39
36
double distance = tile .distance (worldPos );
40
37
41
38
if (distance < TILE_SIZE / 2 ) {
42
- closestIndex = i ;
43
- break ;
39
+ return getTile ((int ) tile .getX (), (int ) tile .getZ ());
44
40
}
45
41
46
- if (closest == null || distance < closest .distance (worldPos )) {
42
+ if (distance < minDistance ) {
43
+ minDistance = distance ;
47
44
closest = tile ;
48
- closestIndex = i ;
49
45
}
50
46
}
51
47
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 ());
57
50
}
58
51
59
52
/**
@@ -63,9 +56,9 @@ public Vec3 getClosestTileFromWorld(@NonNull Vec3 worldPos) {
63
56
*/
64
57
public ArrayList <Vec3 > gridToWorld () {
65
58
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 ));
69
62
}
70
63
}
71
64
return grid ;
@@ -79,7 +72,7 @@ public ArrayList<Vec3> gridToWorld() {
79
72
* @return the world coordinate of the center of the tile
80
73
*/
81
74
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 );
83
76
}
84
77
85
78
/**
@@ -90,7 +83,7 @@ public Vec3 getWorldTileCenter(int x, int y) {
90
83
* @return the height of the tile
91
84
*/
92
85
public float getTileHeight (int x , int y ) {
93
- return tiles [x ][ y ];
86
+ return tiles [y ][ x ];
94
87
}
95
88
96
89
/**
@@ -120,7 +113,7 @@ public Vec3 getTile(int x, int y) {
120
113
throw new IllegalArgumentException ("Invalid grid coordinates: (" + x + ", " + y + ")" );
121
114
}
122
115
123
- return new Vec3 (x , tiles [ y ][ x ] , y );
116
+ return new Vec3 (x , getTileHeight ( x , y ) , y );
124
117
}
125
118
126
119
/**
0 commit comments