Skip to content

Commit 5cb9c76

Browse files
committed
refactor: move effect logic into one function
This makes it much easier to support multiple effects.
1 parent 98b4a90 commit 5cb9c76

File tree

9 files changed

+65
-26
lines changed

9 files changed

+65
-26
lines changed

levels/bogo_sort.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ func next():
2020
if array.is_sorted():
2121
emit_signal("done")
2222

23-
func emphasized(i):
24-
return false
23+
func get_effect(i):
24+
return EFFECTS.NONE

levels/bubble_sort.gd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ finished. Though simple to understand, bubble sort is hopelessly
1010
inefficient on all but the smallest of arrays.
1111
"""
1212
const CONTROLS = """
13-
If the two highlighted elements are out of order, hit LEFT ARROW to SWAP
13+
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1414
them. Otherwise, hit RIGHT ARROW to continue.
1515
"""
1616

@@ -37,5 +37,7 @@ func next():
3737
_index = 0
3838
_swapped = false
3939

40-
func emphasized(i):
41-
return i == _index or i == _index + 1
40+
func get_effect(i):
41+
if i == _index or i == _index + 1:
42+
return EFFECTS.HIGHLIGHTED
43+
return EFFECTS.NONE

levels/comparison_sort.gd

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ extends Node
44
signal done
55
signal mistake
66

7-
enum ACTIONS {SWAP, NO_SWAP}
7+
enum ACTIONS {
8+
SWAP,
9+
NO_SWAP,
10+
11+
LEFT,
12+
RIGHT,
13+
}
14+
15+
const EFFECTS = {
16+
"NONE": GlobalTheme.GREEN,
17+
"HIGHLIGHTED": GlobalTheme.ORANGE,
18+
"DIMMED": GlobalTheme.DARK_GREEN,
19+
"WUT": 0,
20+
}
821

922
const DISABLE_TIME = 1.0
1023

@@ -25,12 +38,13 @@ func _input(event):
2538
"""Pass input events for checking and take appropriate action."""
2639
if not active:
2740
return
41+
2842
for action in ACTIONS:
2943
if event.is_action_pressed(action):
3044
if check(ACTIONS[action]):
31-
next()
32-
else:
33-
emit_signal("mistake")
45+
return next()
46+
if event.is_pressed():
47+
emit_signal("mistake")
3448

3549
func check(action):
3650
"""Determine if the given action enum value is correct."""
@@ -40,10 +54,6 @@ func next():
4054
"""Advance the state by one step and signal done if completed."""
4155
push_error("NotImplementedError")
4256

43-
func emphasized(i):
44-
"""Return whether the given index should be highlighted."""
45-
push_error("NotImplementedError")
46-
4757
func _on_ComparisonSort_mistake():
4858
"""Disable the controls for one second."""
4959
active = false

levels/insertion_sort.gd

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ one of the faster quadratic algorithms. It is often used to sort smaller
1010
subarrays in hybrid sorting algorithms.
1111
"""
1212
const CONTROLS = """
13-
Hit LEFT ARROW to SWAP the two highlighted elements as long as they are
13+
Hit LEFT ARROW to swap the two highlighted elements as long as they are
1414
out of order. When this is no longer the case, hit RIGHT ARROW to
1515
advance.
1616
"""
1717

18-
var _end = 1
19-
var _index = _end
18+
var _end = 1 # Size of the sorted subarray
19+
var _index = 1 # Position of element currently being inserted
2020

2121
func _init(array).(array):
2222
pass
@@ -36,11 +36,13 @@ func next():
3636
else:
3737
_grow()
3838

39+
func get_effect(i):
40+
if i == _index or i == _index - 1:
41+
return EFFECTS.HIGHLIGHTED
42+
return EFFECTS.NONE
43+
3944
func _grow():
4045
_end += 1
4146
if _end == array.size:
4247
emit_signal("done")
4348
_index = _end
44-
45-
func emphasized(i):
46-
return i == _index or i == _index - 1

levels/selection_sort.gd

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ smaller than the left highlighted element, then hit LEFT ARROW to swap
1515
the new smallest into place and keep going.
1616
"""
1717

18-
var _base = 0
19-
var _index = _base + 1
18+
var _base = 0 # Size of sorted subarray
19+
var _index = 1 # Index of tentative new smallest
2020

2121
func _init(array).(array):
2222
pass
@@ -37,5 +37,7 @@ func next():
3737
if _base == array.size - 1:
3838
emit_signal("done")
3939

40-
func emphasized(i):
41-
return i == _index or i == _base
40+
func get_effect(i):
41+
if i == _index or i == _base:
42+
return EFFECTS.HIGHLIGHTED
43+
return EFFECTS.NONE

project.godot

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ _global_script_classes=[ {
4040
"path": "res://levels/insertion_sort.gd"
4141
}, {
4242
"base": "ComparisonSort",
43+
"class": "MergeSort",
44+
"language": "GDScript",
45+
"path": "res://levels/merge_sort.gd"
46+
}, {
47+
"base": "ComparisonSort",
4348
"class": "SelectionSort",
4449
"language": "GDScript",
4550
"path": "res://levels/selection_sort.gd"
@@ -51,6 +56,7 @@ _global_script_class_icons={
5156
"BubbleSort": "",
5257
"ComparisonSort": "",
5358
"InsertionSort": "",
59+
"MergeSort": "",
5460
"SelectionSort": ""
5561
}
5662

@@ -127,6 +133,16 @@ NO_SWAP={
127133
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
128134
]
129135
}
136+
LEFT={
137+
"deadzone": 0.5,
138+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
139+
]
140+
}
141+
RIGHT={
142+
"deadzone": 0.5,
143+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
144+
]
145+
}
130146

131147
[rendering]
132148

scripts/levels.gd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var levels = [
44
BubbleSort,
55
InsertionSort,
66
SelectionSort,
7+
MergeSort,
78
]
89
var level: ComparisonSort
910

@@ -19,9 +20,14 @@ func _ready():
1920
# Autofocus last played level
2021
if GlobalScene.get_param("level") == level:
2122
button.grab_focus()
22-
# If no last played level, autofocus first
23+
var top_button = $LevelsBorder/Levels.get_children()[0]
24+
var bottom_button = $LevelsBorder/Levels.get_children()[-1]
25+
# Allow looping from ends of list
26+
top_button.focus_neighbour_top = bottom_button.get_path()
27+
bottom_button.focus_neighbour_bottom = top_button.get_path()
28+
# If no last played level, autofocus first level
2329
if GlobalScene.get_param("level") == null:
24-
$LevelsBorder/Levels.get_child(0).grab_focus()
30+
top_button.grab_focus()
2531

2632
func _on_Button_focus_changed():
2733
"""Initialize the preview section."""

scripts/theme.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Global constants relating to the GUI.
55
extends Node
66

77
const GREEN = Color(0.2, 1, 0.2)
8+
const DARK_GREEN = Color(0.2, 1, 0.2, 0.5)
89
const ORANGE = Color(1, 0.69, 0)
910
const RED = Color(1, 0, 0)

views/array_view.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func _process(delta):
2424
"""Update heights of rectangles based on array values."""
2525
for i in range(level.array.size):
2626
rects[i].rect_scale.y = -1 # XXX: Override parent Control scale
27-
rects[i].color = GlobalTheme.ORANGE if level.emphasized(i) else GlobalTheme.GREEN
27+
rects[i].color = level.get_effect(i)
2828
rects[i].rect_size.y = rect_size.y * level.array.get(i) / level.array.size
2929

3030
func _on_Level_mistake():

0 commit comments

Comments
 (0)