Skip to content

Commit a7c064a

Browse files
committed
fix: implement variable separation to fill display
Block sizes remain constant but separation can be one more pixel so the ArrayView fills its entire parent without looking blurry. This is just a quick fix; a thorough implementation later should vary block sizes while keeping separation constant so that large simulations with zero separation between blocks look acceptable. This would involve smarter and more complex positioning code.
1 parent 12d5a23 commit a7c064a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

scripts/levels.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const LEVELS = [
77
MergeSort,
88
]
99
const MIN_SIZE = 8
10-
const MAX_SIZE = 256
10+
const MAX_SIZE = 128
1111

1212
var _level = LEVELS[0].new(ArrayModel.new())
1313

views/array_view.gd

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const SWAP_DURATION = 0.1
1010
var _tween = Tween.new()
1111
var _level: ComparisonSort
1212
var _rects = []
13+
var _positions = []
1314
var _unit_width: int
1415
var _unit_height: int
1516
onready var _separation = 128 / _level.array.size
@@ -23,16 +24,26 @@ func _ready():
2324
yield(get_tree(), "idle_frame")
2425
_unit_width = rect_size.x / _level.array.size
2526
_unit_height = rect_size.y / _level.array.size
27+
# Keep track of accumulated pixel error from integer division
28+
var error = float(rect_size.x) / _level.array.size - _unit_width
29+
var accumulated = 0
30+
var x = 0
2631
_level.connect("mistake", get_parent(), "flash")
2732
for i in range(_level.array.size):
2833
var rect = Polygon2D.new()
34+
if accumulated >= 1:
35+
x += 1
36+
accumulated -= 1
2937
rect.polygon = [
3038
Vector2(0, 0),
3139
Vector2(0, rect_size.y),
3240
Vector2(_unit_width - _separation, rect_size.y),
3341
Vector2(_unit_width - _separation, 0),
3442
]
35-
rect.position = Vector2(i * _unit_width, rect_size.y)
43+
accumulated += error
44+
rect.position = Vector2(x, rect_size.y)
45+
_positions.append(x)
46+
x += _unit_width
3647
_rects.append(rect)
3748
add_child(rect)
3849
_level.array.connect("swapped", self, "_on_ArrayModel_swapped")
@@ -45,9 +56,9 @@ func _process(delta):
4556
func _on_ArrayModel_swapped(i, j):
4657
var time = SWAP_DURATION * (1 + float(j - i) / _level.array.size)
4758
_tween.interpolate_property(
48-
_rects[i], "position:x", null, j * _unit_width, time)
59+
_rects[i], "position:x", null, _positions[j], time)
4960
_tween.interpolate_property(
50-
_rects[j], "position:x", null, i * _unit_width, time)
61+
_rects[j], "position:x", null, _positions[i], time)
5162
var temp = _rects[i]
5263
_rects[i] = _rects[j]
5364
_rects[j] = temp

0 commit comments

Comments
 (0)