Skip to content

Commit e3e4609

Browse files
committed
refactor: move actions into individual classes
It didn't make any sense for sorts with different action semantics to share a global actions dictionary.
1 parent 8ebc0f7 commit e3e4609

File tree

6 files changed

+21
-34
lines changed

6 files changed

+21
-34
lines changed

levels/bubble_sort.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ If the two highlighted elements are out of order, hit LEFT ARROW to swap
1414
them. Otherwise, hit RIGHT ARROW to continue.
1515
"""
1616

17+
const ACTIONS = {
18+
"SWAP": "Left",
19+
"CONTINUE": "Right",
20+
}
1721
var _index = 0 # First of two elements being compared
1822
var _end = array.size # Beginning of sorted subarray
1923
var _swapped = false
@@ -27,7 +31,7 @@ func next(action):
2731
return emit_signal("mistake")
2832
array.swap(_index, _index + 1)
2933
_swapped = true
30-
elif action != null and action != ACTIONS.NO_SWAP:
34+
elif action != null and action != ACTIONS.CONTINUE:
3135
return emit_signal("mistake")
3236
_index += 1
3337
# Prevent player from having to spam tap through the end

levels/comparison_sort.gd

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ extends Node
44
signal done
55
signal mistake
66

7-
const ACTIONS = {
8-
"SWAP": "ui_left",
9-
"NO_SWAP": "ui_right",
10-
11-
"LEFT": "ui_left",
12-
"RIGHT": "ui_right",
13-
}
14-
157
const EFFECTS = {
168
"NONE": GlobalTheme.GREEN,
179
"HIGHLIGHTED": GlobalTheme.ORANGE,
@@ -37,9 +29,8 @@ func _input(event):
3729
"""Pass input events for checking and take appropriate action."""
3830
if not active:
3931
return
40-
for action in ACTIONS.values():
41-
if event.is_action_pressed(action):
42-
return next(action)
32+
if event.is_pressed():
33+
return next(event.as_text())
4334

4435
func next(action):
4536
"""Check the action and advance state or emit signal as needed."""

levels/insertion_sort.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ out of order. When this is no longer the case, hit RIGHT ARROW to
1515
advance.
1616
"""
1717

18+
const ACTIONS = {
19+
"SWAP": "Left",
20+
"CONTINUE": "Right",
21+
}
1822
var _end = 1 # Size of the sorted subarray
1923
var _index = 1 # Position of element currently being inserted
2024

@@ -30,7 +34,7 @@ func next(action):
3034
if _index == 0:
3135
_grow()
3236
else:
33-
if action != null and action != ACTIONS.NO_SWAP:
37+
if action != null and action != ACTIONS.CONTINUE:
3438
return emit_signal("mistake")
3539
_grow()
3640

levels/merge_sort.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ highlighted element is on. If you've reached the end of one side, press
1515
the other side's ARROW KEY.
1616
"""
1717

18+
const ACTIONS = {
19+
"LEFT": "Left",
20+
"RIGHT": "Right",
21+
}
1822
var _left = 0 # Index of left subarray pointer
1923
var _right = 1 # Index of right subarray pointer
2024
var _sub_size = 2 # Combined size of left and right subarrays

levels/selection_sort.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ smaller than the left highlighted element, then hit LEFT ARROW and
1515
repeat.
1616
"""
1717

18+
const ACTIONS = {
19+
"SWAP": "Left",
20+
"CONTINUE": "Right",
21+
}
1822
var _base = 0 # Size of sorted subarray
1923
var _min = 0 # Index of smallest known element
2024
var _index = 1 # Element currently being compared
@@ -27,7 +31,7 @@ func next(action):
2731
if action != null and action != ACTIONS.SWAP:
2832
return emit_signal("mistake")
2933
_min = _index
30-
elif action != null and action != ACTIONS.NO_SWAP:
34+
elif action != null and action != ACTIONS.CONTINUE:
3135
return emit_signal("mistake")
3236
_index += 1
3337
if _index == array.size:

project.godot

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,6 @@ ui_down={
125125
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
126126
]
127127
}
128-
SWAP={
129-
"deadzone": 0.5,
130-
"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)
131-
]
132-
}
133-
NO_SWAP={
134-
"deadzone": 0.5,
135-
"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)
136-
]
137-
}
138-
LEFT={
139-
"deadzone": 0.5,
140-
"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)
141-
]
142-
}
143-
RIGHT={
144-
"deadzone": 0.5,
145-
"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)
146-
]
147-
}
148128

149129
[rendering]
150130

0 commit comments

Comments
 (0)