Skip to content

Commit 48fbaf1

Browse files
committed
feat: add basic gameplay without feedback
Bubble sort can now be controlled with the arrow keys but there is no feedback to tell the player they were wrong or an end condition when they win. Interestingly, this doesn't break bubble sort's gameplay at all; I think this is becaues bubble sort by design resets its state after the completion of every iteration of the array.
1 parent 931699a commit 48fbaf1

File tree

6 files changed

+112
-21
lines changed

6 files changed

+112
-21
lines changed

algorithms/bubble_sort.gd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extends ComparisonSort
2+
class_name BubbleSort
3+
4+
func _init(array).(array):
5+
pass
6+
7+
func next(action):
8+
if action == "swap":
9+
array.swap(index, index + 1)
10+
index += 1
11+
if index == array.size - 1:
12+
index = 0
13+
14+
func emphasized(i):
15+
return i == index or i == index + 1

algorithms/comparison_sort.gd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extends Node
2+
class_name ComparisonSort
3+
4+
var array: ArrayModel
5+
var index = 0
6+
7+
func _init(array):
8+
self.array = array
9+
10+
func next(action):
11+
pass
12+
13+
func emphasized(i):
14+
pass
15+
16+
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")

models/array_model.gd

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ var size
1010

1111
func _init(size):
1212
for i in range(1, size + 1):
13-
array.append(Element.new(i))
13+
array.append(i)
1414
array.shuffle()
1515
self.size = size
1616

17-
func get(index):
18-
return array[index]
17+
func get(i):
18+
return array[i]
1919

20-
class Element:
21-
var value
22-
var emphasized
23-
24-
func _init(value):
25-
self.value = value
26-
self.emphasized = false
20+
func swap(i, j):
21+
var temp = array[i]
22+
array[i] = array[j]
23+
array[j] = temp

project.godot

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@ _global_script_classes=[ {
1818
"class": "ArrayView",
1919
"language": "GDScript",
2020
"path": "res://views/array_view.gd"
21+
}, {
22+
"base": "ComparisonSort",
23+
"class": "BubbleSort",
24+
"language": "GDScript",
25+
"path": "res://algorithms/bubble_sort.gd"
26+
}, {
27+
"base": "Node",
28+
"class": "ComparisonSort",
29+
"language": "GDScript",
30+
"path": "res://algorithms/comparison_sort.gd"
2131
} ]
2232
_global_script_class_icons={
2333
"ArrayModel": "",
24-
"ArrayView": ""
34+
"ArrayView": "",
35+
"BubbleSort": "",
36+
"ComparisonSort": ""
2537
}
2638

2739
[application]
@@ -50,6 +62,51 @@ enabled=PoolStringArray( )
5062

5163
theme/use_hidpi=true
5264

65+
[input]
66+
67+
ui_accept={
68+
"deadzone": 0.5,
69+
"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":16777221,"unicode":0,"echo":false,"script":null)
70+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777222,"unicode":0,"echo":false,"script":null)
71+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
72+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
73+
]
74+
}
75+
ui_left={
76+
"deadzone": 0.5,
77+
"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)
78+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
79+
]
80+
}
81+
ui_right={
82+
"deadzone": 0.5,
83+
"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)
84+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
85+
]
86+
}
87+
ui_up={
88+
"deadzone": 0.5,
89+
"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":16777232,"unicode":0,"echo":false,"script":null)
90+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
91+
]
92+
}
93+
ui_down={
94+
"deadzone": 0.5,
95+
"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":16777234,"unicode":0,"echo":false,"script":null)
96+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
97+
]
98+
}
99+
swap={
100+
"deadzone": 0.5,
101+
"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)
102+
]
103+
}
104+
no_swap={
105+
"deadzone": 0.5,
106+
"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)
107+
]
108+
}
109+
53110
[rendering]
54111

55112
quality/driver/driver_name="GLES2"

scripts/play.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func _on_Timer_timeout():
2222
$DisplayBorder.get_child(0).queue_free()
2323
# Load level
2424
var array = ArrayModel.new(10)
25+
var level
2526
match scene.get_param("level"):
2627
"BUBBLE SORT":
27-
pass
28-
$DisplayBorder.add_child(ArrayView.new(array))
28+
level = BubbleSort.new(array)
29+
$DisplayBorder.add_child(ArrayView.new(level))

views/array_view.gd

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ class_name ArrayView
44
const GREEN = Color(0.2, 1, 0.2)
55
const ORANGE = Color(1, 0.69, 0)
66

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

10-
func _init(model):
11-
self.model = model
12-
for i in range(model.size):
10+
func _init(level):
11+
add_child(level)
12+
self.level = level
13+
for i in range(level.array.size):
1314
var rect = ColorRect.new()
1415
rect.size_flags_horizontal = Control.SIZE_EXPAND_FILL
1516
rect.size_flags_vertical = Control.SIZE_SHRINK_END
16-
rect.color = GREEN
1717
rects.append(rect)
1818
add_child(rect)
1919

20-
func _draw():
21-
for i in range(model.size):
20+
func _process(delta):
21+
for i in range(level.array.size):
2222
rects[i].rect_scale.y = -1 # Override parent Control scale
23-
rects[i].rect_size.y = rect_size.y * model.get(i).value / model.size
23+
rects[i].color = ORANGE if level.emphasized(i) else GREEN
24+
rects[i].rect_size.y = rect_size.y * level.array.get(i) / level.array.size

0 commit comments

Comments
 (0)