Skip to content

Commit a37cbb1

Browse files
committed
refactor: move levels.json data into classes
The codebase has been refactored so that each level contains its own title and description, removing the need for a separate levels.json file.
1 parent 43e4f7d commit a37cbb1

File tree

8 files changed

+100
-67
lines changed

8 files changed

+100
-67
lines changed

assets/levels.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

assets/levels.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/levels.png-a6e910382d32e95b5c3f382bb559bde4.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://assets/levels.png"
13+
dest_files=[ "res://.import/levels.png-a6e910382d32e95b5c3f382bb559bde4.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

levels/bogo_sort.gd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
extends ComparisonSort
22
class_name BogoSort
33

4+
const TITLE = "BOGOSORT"
5+
const ABOUT = """Generates random permutations until the array is
6+
sorted."""
7+
48
func _init(array).(array):
5-
pass
9+
pass
610

711
func check(action):
8-
return true
12+
return true
913

1014
func next():
11-
array = ArrayModel.new(array.size)
15+
array = ArrayModel.new(array.size)
1216

1317
func emphasized(i):
14-
return false
18+
return false

levels/bubble_sort.gd

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
extends ComparisonSort
22
class_name BubbleSort
33

4+
const TITLE = "BUBBLE SORT"
5+
const ABOUT = """Bubble sort iterates through the array and looks at
6+
each pair of elements, swapping them if they are out of order. When it
7+
has gone through the entire array without swapping a single pair, it has
8+
finished. Though simple to understand, bubble sort is hopelessly
9+
inefficient on all but the smallest of arrays."""
410
var swapped = false
511

612
func _init(array).(array):
7-
pass
13+
pass
814

915
func check(action):
10-
if array.get(index) > array.get(index + 1):
11-
return action == "swap"
12-
else:
13-
return action == "no_swap"
16+
if array.get(index) > array.get(index + 1):
17+
return action == "swap"
18+
else:
19+
return action == "no_swap"
1420

1521
func next():
16-
if array.get(index) > array.get(index + 1):
17-
array.swap(index, index + 1)
18-
swapped = true
19-
index += 1
20-
if index == array.size - 1:
21-
if not swapped:
22-
emit_signal("done")
23-
index = 0
24-
swapped = false
22+
if array.get(index) > array.get(index + 1):
23+
array.swap(index, index + 1)
24+
swapped = true
25+
index += 1
26+
if index == array.size - 1:
27+
if not swapped:
28+
emit_signal("done")
29+
index = 0
30+
swapped = false
2531

2632
func emphasized(i):
27-
return i == index or i == index + 1
33+
return i == index or i == index + 1

levels/comparison_sort.gd

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ var timer = Timer.new()
1212
var active = true
1313

1414
func _init(array):
15-
self.array = array
16-
timer.one_shot = true
17-
timer.connect("timeout", self, "_on_Timer_timeout")
18-
add_child(timer)
19-
self.connect("mistake", self, "_on_ComparisonSort_mistake")
15+
self.array = array
16+
timer.one_shot = true
17+
timer.connect("timeout", self, "_on_Timer_timeout")
18+
add_child(timer)
19+
self.connect("mistake", self, "_on_ComparisonSort_mistake")
2020

2121
func check(action):
22-
pass
22+
pass
2323

2424
func next():
25-
pass
25+
pass
2626

2727
func _on_ComparisonSort_mistake():
28-
active = false
29-
timer.start(1)
28+
active = false
29+
timer.start(1)
3030

3131
func _on_Timer_timeout():
32-
active = true
32+
active = true
3333

3434
func _input(event):
35-
if not active:
36-
return
37-
38-
for action in ACTIONS:
39-
if event.is_action_pressed(action):
40-
if check(action):
41-
next()
42-
else:
43-
emit_signal("mistake")
35+
if not active:
36+
return
37+
38+
for action in ACTIONS:
39+
if event.is_action_pressed(action):
40+
if check(action):
41+
next()
42+
else:
43+
emit_signal("mistake")

scripts/levels.gd

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
11
extends HBoxContainer
22

3-
var levels: Dictionary
4-
var level
3+
var levels = [
4+
BubbleSort,
5+
]
6+
var level: ComparisonSort
57

68
func _ready():
7-
# Load level data
8-
var descriptions = File.new()
9-
descriptions.open("res://assets/levels.json", File.READ)
10-
levels = parse_json(descriptions.get_as_text())
11-
# Dynamically add buttons
9+
# Dynamically load level data
1210
for level in levels:
1311
var button = Button.new()
14-
button.text = level
12+
button.text = level.TITLE
1513
button.align = Button.ALIGN_LEFT
1614
button.connect("focus_entered", self, "_on_Button_focus_changed")
17-
button.connect("pressed", self, "_on_Button_pressed", [level])
15+
button.connect("pressed", self, "_on_Button_pressed", [level.TITLE])
1816
$LevelsBorder/Levels.add_child(button)
1917
# Automatically focus on first button
2018
$LevelsBorder/Levels.get_child(0).grab_focus()
2119

2220
func _on_Button_focus_changed():
23-
var name = get_focus_owner().text
24-
$Preview/InfoBorder/Info/Description.text = levels[name]["about"]
25-
level = get_level(name).new(ArrayModel.new(10))
21+
level = get_level(get_focus_owner().text).new(ArrayModel.new(10))
2622
level.active = false
23+
$Preview/InfoBorder/Info/Description.text = level.ABOUT.replace("\n", " ")
2724
# Start over when simulation is finished
2825
level.connect("done", self, "_on_Button_focus_changed")
2926
# Replace old display with new
3027
for child in $Preview/Display.get_children():
3128
child.queue_free()
3229
$Preview/Display.add_child(ArrayView.new(level))
3330

34-
func _on_Button_pressed(level):
35-
scene.change_scene("res://scenes/play.tscn",
36-
{"name": level, "level": get_level(level)})
31+
func _on_Button_pressed(title):
32+
scene.change_scene("res://scenes/play.tscn", {"level": get_level(title)})
3733

38-
func get_level(level):
39-
match level:
40-
"BUBBLE SORT":
41-
return BubbleSort
34+
func get_level(title):
35+
for level in levels:
36+
if level.TITLE == title:
37+
return level
4238

4339
func _on_Timer_timeout():
4440
level.next()

scripts/play.gd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends VBoxContainer
33
var start_time = -1
44

55
func _ready():
6-
$HUDBorder/HUD/Level.text = scene.get_param("name")
6+
$HUDBorder/HUD/Level.text = scene.get_param("level").TITLE
77

88
func _process(delta):
99
if start_time >= 0:
@@ -14,8 +14,7 @@ func _on_Timer_timeout():
1414
# Delete ready text
1515
$DisplayBorder/Label.queue_free()
1616
# Load level
17-
var array = ArrayModel.new(10)
18-
var level = scene.get_param("level").new(array)
17+
var level = scene.get_param("level").new(ArrayModel.new(10))
1918
level.connect("done", self, "_on_Level_done")
2019
$DisplayBorder.add_child(ArrayView.new(level))
2120

views/array_view.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class_name ArrayView
44
const GREEN = Color(0.2, 1, 0.2)
55
const ORANGE = Color(1, 0.69, 0)
66

7-
var level
7+
var level: ComparisonSort
88
var rects = []
99

1010
func _init(level):

0 commit comments

Comments
 (0)