Skip to content

Commit 54422fa

Browse files
committed
feat: make level preview more user-friendly
The simulation speeds are now clamped and it no longer restarts the preview immediately after finishing.
1 parent 8971957 commit 54422fa

File tree

8 files changed

+27
-9
lines changed

8 files changed

+27
-9
lines changed

levels/bogo_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ func next(action):
1717
if array.is_sorted():
1818
emit_signal("done")
1919

20-
func get_effect(i):
20+
func _get_effect(i):
2121
return EFFECTS.NONE

levels/bubble_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func next(action):
4242
_end -= 1
4343
_swapped = false
4444

45-
func get_effect(i):
45+
func _get_effect(i):
4646
if i == _index or i == _index + 1:
4747
return EFFECTS.HIGHLIGHTED
4848
if i >= _end:

levels/comparison_sort.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func _init(array):
2727

2828
func _input(event):
2929
"""Pass input events for checking and take appropriate action."""
30-
if not active:
30+
if not active or array.is_sorted():
3131
return
3232
if event.is_pressed():
3333
return next(event.as_text())
@@ -36,6 +36,14 @@ func next(action):
3636
"""Check the action and advance state or emit signal as needed."""
3737
push_error("NotImplementedError")
3838

39+
func get_effect(i):
40+
if array.is_sorted():
41+
return EFFECTS.NONE
42+
return _get_effect(i)
43+
44+
func _get_effect(i):
45+
push_error("NotImplementedError")
46+
3947
func _on_ComparisonSort_mistake():
4048
"""Disable the controls for one second."""
4149
active = false

levels/insertion_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func next(action):
3838
return emit_signal("mistake")
3939
_grow()
4040

41-
func get_effect(i):
41+
func _get_effect(i):
4242
if i == _index or i == _index - 1:
4343
return EFFECTS.HIGHLIGHTED
4444
if i < _end:

levels/merge_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func next(action):
6262
_left = _get_begin()
6363
_right = _get_middle()
6464

65-
func get_effect(i):
65+
func _get_effect(i):
6666
var is_left = _left != _get_middle() and i == _left
6767
var is_right = _right != _get_end() and i == _right
6868
if is_left or is_right:

levels/selection_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func next(action):
4242
if _base == array.size - 1:
4343
emit_signal("done")
4444

45-
func get_effect(i):
45+
func _get_effect(i):
4646
if i == _min or i == _index:
4747
return EFFECTS.HIGHLIGHTED
4848
if i < _base:

models/array_model.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func at(i):
2121

2222
func is_sorted():
2323
"""Check if the array is in monotonically increasing order."""
24-
for i in range(size - 1):
24+
for i in range(get_size() - 1):
2525
if _array[i] > _array[i + 1]:
2626
return false
2727
return true

scripts/levels.gd

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const LEVELS = [
66
SelectionSort,
77
MergeSort,
88
]
9+
const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
10+
const MAX_WAIT = 4
911
const MIN_SIZE = 8
1012
const MAX_SIZE = 128
1113

@@ -32,6 +34,14 @@ func _ready():
3234
top_button.grab_focus()
3335

3436
func _on_Button_focus_entered(size=_level.array.size):
37+
# Pause a bit to show completely sorted array
38+
if _level.array.is_sorted():
39+
$Timer.stop()
40+
yield(get_tree().create_timer(1), "timeout")
41+
$Timer.start()
42+
# Prevent race condition caused by switching levels during pause
43+
if not _level.array.is_sorted():
44+
return
3545
_level = _get_level(get_focus_owner().text).new(ArrayModel.new(size))
3646
_level.active = false
3747
$Preview/InfoBorder/Info/About.text = _cleanup(_level.ABOUT)
@@ -45,9 +55,9 @@ func _on_Button_focus_entered(size=_level.array.size):
4555

4656
func _input(event):
4757
if event.is_action_pressed("faster"):
48-
$Timer.wait_time /= 2
58+
$Timer.wait_time = max(MIN_WAIT, $Timer.wait_time / 2)
4959
elif event.is_action_pressed("slower"):
50-
$Timer.wait_time *= 2
60+
$Timer.wait_time = min(MAX_WAIT, $Timer.wait_time * 2)
5161
elif event.is_action_pressed("bigger"):
5262
_on_Button_focus_entered(min(MAX_SIZE, _level.array.size * 2))
5363
elif event.is_action_pressed("smaller"):

0 commit comments

Comments
 (0)