Skip to content

Commit 4efbd6e

Browse files
committed
feat: add feedback and end scene
The controls are now disabled for one second when the user makes a mistake and a blank end screen is triggered upon completion.
1 parent 1f1aca9 commit 4efbd6e

File tree

7 files changed

+73
-15
lines changed

7 files changed

+73
-15
lines changed

levels/bubble_sort.gd

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

4+
var swapped = false
5+
46
func _init(array).(array):
57
pass
68

7-
func next(action):
8-
if action == "swap":
9+
func check(action):
10+
if array.get(index) > array.get(index + 1):
11+
return action == "swap"
12+
else:
13+
return action == "no_swap"
14+
15+
func next():
16+
if array.get(index) > array.get(index + 1):
917
array.swap(index, index + 1)
18+
swapped = true
1019
index += 1
1120
if index == array.size - 1:
21+
if not swapped:
22+
emit_signal("done")
1223
index = 0
24+
swapped = false
1325

1426
func emphasized(i):
1527
return i == index or i == index + 1

levels/comparison_sort.gd

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
extends Node
22
class_name ComparisonSort
33

4+
signal done
5+
signal mistake
6+
7+
const ACTIONS = ["swap", "no_swap"]
8+
49
var array: ArrayModel
510
var index = 0
11+
var timer = Timer.new()
12+
var active = true
613

714
func _init(array):
815
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")
920

10-
func next(action):
21+
func check(action):
1122
pass
1223

13-
func emphasized(i):
24+
func next():
1425
pass
1526

27+
func _on_ComparisonSort_mistake():
28+
active = false
29+
timer.start(1)
30+
31+
func _on_Timer_timeout():
32+
active = true
33+
1634
func _input(event):
17-
if event.is_action_pressed("swap"):
18-
next("swap")
19-
elif event.is_action_pressed("no_swap"):
20-
next("no_swap")
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")

models/array_model.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ func _init(size):
1717
func get(i):
1818
return array[i]
1919

20+
func is_sorted():
21+
for i in range(size - 1):
22+
if array[i] > array[i + 1]:
23+
return false
24+
return true
25+
2026
func swap(i, j):
2127
var temp = array[i]
2228
array[i] = array[j]

project.godot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ _global_script_classes=[ {
2222
"base": "ComparisonSort",
2323
"class": "BubbleSort",
2424
"language": "GDScript",
25-
"path": "res://algorithms/bubble_sort.gd"
25+
"path": "res://levels/bubble_sort.gd"
2626
}, {
2727
"base": "Node",
2828
"class": "ComparisonSort",
2929
"language": "GDScript",
30-
"path": "res://algorithms/comparison_sort.gd"
30+
"path": "res://levels/comparison_sort.gd"
3131
} ]
3232
_global_script_class_icons={
3333
"ArrayModel": "",

scenes/end.tscn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[gd_scene format=2]
2+
3+
[node name="Viewport" type="MarginContainer"]
4+
anchor_right = 1.0
5+
anchor_bottom = 1.0
6+
__meta__ = {
7+
"_edit_use_anchors_": false
8+
}

scripts/play.gd

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ func _ready():
88
var label = Label.new()
99
label.text = "ready..."
1010
label.align = Label.ALIGN_CENTER
11-
label.set_anchors_preset(Control.PRESET_HCENTER_WIDE)
1211
$DisplayBorder.add_child(label)
1312

1413
func _process(delta):
@@ -22,8 +21,14 @@ func _on_Timer_timeout():
2221
$DisplayBorder.get_child(0).queue_free()
2322
# Load level
2423
var array = ArrayModel.new(10)
25-
var level
26-
match scene.get_param("level"):
27-
"BUBBLE SORT":
28-
level = BubbleSort.new(array)
24+
var level = getLevel(scene.get_param("level")).new(array)
25+
level.connect("done", self, "_on_Level_done")
2926
$DisplayBorder.add_child(ArrayView.new(level))
27+
28+
func getLevel(level):
29+
match level:
30+
"BUBBLE SORT":
31+
return BubbleSort
32+
33+
func _on_Level_done():
34+
scene.change_scene("res://scenes/end.tscn")

views/array_view.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var level
88
var rects = []
99

1010
func _init(level):
11+
level.connect("mistake", self, "_on_Level_mistake")
1112
add_child(level)
1213
self.level = level
1314
for i in range(level.array.size):
@@ -22,3 +23,6 @@ func _process(delta):
2223
rects[i].rect_scale.y = -1 # Override parent Control scale
2324
rects[i].color = ORANGE if level.emphasized(i) else GREEN
2425
rects[i].rect_size.y = rect_size.y * level.array.get(i) / level.array.size
26+
27+
func _on_Level_mistake():
28+
pass

0 commit comments

Comments
 (0)