Skip to content

Commit 92d7bbf

Browse files
authored
Fix isometric grid calculations using ints instead of floats (#1069)
1 parent e83d560 commit 92d7bbf

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/UI/Canvas/Grid.gd

+14-12
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,22 @@ func _draw_cartesian_grid(target_rect: Rect2i) -> void:
5353
func _draw_isometric_grid(target_rect: Rect2i) -> void:
5454
var grid_multiline_points := PackedVector2Array()
5555

56-
var cell_size := Global.isometric_grid_size
57-
var max_cell_count := target_rect.size / cell_size
58-
var origin_offset := Vector2(Global.grid_offset - target_rect.position).posmodv(cell_size)
56+
var cell_size: Vector2 = Global.isometric_grid_size
57+
var max_cell_count: Vector2 = Vector2(target_rect.size) / cell_size
58+
var origin_offset: Vector2 = Vector2(Global.grid_offset - target_rect.position).posmodv(
59+
cell_size
60+
)
5961

6062
# lines ↗↗↗ (from bottom-left to top-right)
61-
var per_cell_offset := cell_size * Vector2i(1, -1)
63+
var per_cell_offset: Vector2 = cell_size * Vector2(1, -1)
6264

6365
# lines ↗↗↗ starting from the rect's left side (top to bottom):
6466
var y: float = fposmod(
6567
origin_offset.y + cell_size.y * (0.5 + origin_offset.x / cell_size.x), cell_size.y
6668
)
6769
while y < target_rect.size.y:
68-
var start: Vector2 = target_rect.position + Vector2i(0, y)
69-
var cells_to_rect_bounds := minf(max_cell_count.x, y / cell_size.y)
70+
var start: Vector2 = Vector2(target_rect.position) + Vector2(0, y)
71+
var cells_to_rect_bounds: float = minf(max_cell_count.x, y / cell_size.y)
7072
var end := start + cells_to_rect_bounds * per_cell_offset
7173
grid_multiline_points.push_back(start)
7274
grid_multiline_points.push_back(end)
@@ -75,8 +77,8 @@ func _draw_isometric_grid(target_rect: Rect2i) -> void:
7577
# lines ↗↗↗ starting from the rect's bottom side (left to right):
7678
var x: float = (y - target_rect.size.y) / cell_size.y * cell_size.x
7779
while x < target_rect.size.x:
78-
var start: Vector2 = target_rect.position + Vector2i(x, target_rect.size.y)
79-
var cells_to_rect_bounds := minf(max_cell_count.y, max_cell_count.x - x / cell_size.x)
80+
var start: Vector2 = Vector2(target_rect.position) + Vector2(x, target_rect.size.y)
81+
var cells_to_rect_bounds: float = minf(max_cell_count.y, max_cell_count.x - x / cell_size.x)
8082
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
8183
grid_multiline_points.push_back(start)
8284
grid_multiline_points.push_back(end)
@@ -88,8 +90,8 @@ func _draw_isometric_grid(target_rect: Rect2i) -> void:
8890
# lines ↘↘↘ starting from the rect's left side (top to bottom):
8991
y = fposmod(origin_offset.y - cell_size.y * (0.5 + origin_offset.x / cell_size.x), cell_size.y)
9092
while y < target_rect.size.y:
91-
var start: Vector2 = target_rect.position + Vector2i(0, y)
92-
var cells_to_rect_bounds := minf(max_cell_count.x, max_cell_count.y - y / cell_size.y)
93+
var start: Vector2 = Vector2(target_rect.position) + Vector2(0, y)
94+
var cells_to_rect_bounds: float = minf(max_cell_count.x, max_cell_count.y - y / cell_size.y)
9395
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
9496
grid_multiline_points.push_back(start)
9597
grid_multiline_points.push_back(end)
@@ -98,8 +100,8 @@ func _draw_isometric_grid(target_rect: Rect2i) -> void:
98100
# lines ↘↘↘ starting from the rect's top side (left to right):
99101
x = fposmod(origin_offset.x - cell_size.x * (0.5 + origin_offset.y / cell_size.y), cell_size.x)
100102
while x < target_rect.size.x:
101-
var start: Vector2 = target_rect.position + Vector2i(x, 0)
102-
var cells_to_rect_bounds := minf(max_cell_count.y, max_cell_count.x - x / cell_size.x)
103+
var start: Vector2 = Vector2(target_rect.position) + Vector2(x, 0)
104+
var cells_to_rect_bounds: float = minf(max_cell_count.y, max_cell_count.x - x / cell_size.x)
103105
var end: Vector2 = start + cells_to_rect_bounds * per_cell_offset
104106
grid_multiline_points.push_back(start)
105107
grid_multiline_points.push_back(end)

0 commit comments

Comments
 (0)