Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08640a1

Browse files
feat: add custom data types
1 parent b31a9b0 commit 08640a1

File tree

6 files changed

+154
-40
lines changed

6 files changed

+154
-40
lines changed

‎models/array_model.gd‎

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,62 @@ signal swapped(i, j)
66
signal sorted(i, j)
77

88
const DEFAULT_SIZE = 16
9+
enum DATA_TYPES {
10+
RANDOM_UNIQUE,
11+
TRUE_RANDOM,
12+
REVERSED,
13+
FEW_UNIQUE,
14+
ALL_THE_SAME,
15+
NEARLY_SORTED,
16+
ALREADY_SORTED,
17+
}
918

1019
var _array = []
1120
var size = 0 setget , get_size
1221
var biggest = null
1322

14-
func _init(size=DEFAULT_SIZE):
23+
func _init(size=DEFAULT_SIZE, data_type=DATA_TYPES.RANDOM_UNIQUE):
1524
"""Randomize the array."""
16-
for i in range(1, size + 1):
17-
_array.append(i)
18-
_array.shuffle()
19-
biggest = _array.max()
25+
match data_type:
26+
DATA_TYPES.RANDOM_UNIQUE:
27+
for i in range(1, size + 1):
28+
_array.append(i)
29+
_array.shuffle()
30+
DATA_TYPES.TRUE_RANDOM:
31+
for i in range(size):
32+
_array.append(randi() % size + 1)
33+
DATA_TYPES.REVERSED:
34+
for i in range(size, 0, -1):
35+
_array.append(i)
36+
DATA_TYPES.FEW_UNIQUE:
37+
var values = []
38+
for i in range(sqrt(size)):
39+
values.append(randi() % size + 1)
40+
for i in range(size):
41+
_array.append(values[randi() % values.size()])
42+
DATA_TYPES.ALL_THE_SAME:
43+
for i in range(size):
44+
_array.append(1)
45+
DATA_TYPES.NEARLY_SORTED:
46+
# We interpret nearly sorted as every element being K or
47+
# less places away from its sorted position, where K is a
48+
# small number relative to the size of the array.
49+
for i in range(1, size + 1):
50+
_array.append(i)
51+
_array.shuffle()
52+
_nearly_sort(0, size - 1, ceil(sqrt(size)))
53+
DATA_TYPES.ALREADY_SORTED:
54+
for i in range(1, size + 1):
55+
_array.append(i)
56+
biggest = _array.max() if data_type != DATA_TYPES.ALL_THE_SAME else 0
2057

2158
func at(i):
2259
"""Retrieve the value of the element at index i."""
2360
return _array[i]
2461

2562
func frac(i):
2663
"""Get the quotient of the element at index i and the biggest."""
27-
return float(_array[i]) / biggest
64+
return float(_array[i]) / biggestifbiggest!=0else0.5
2865

2966
func is_sorted():
3067
"""Check if the array is in monotonically increasing order."""
@@ -52,3 +89,16 @@ func sort(i, j):
5289

5390
func get_size():
5491
return _array.size()
92+
93+
func _nearly_sort(start, end, k):
94+
# If false, then no element in this subarray is more than K places
95+
# away from its sorted position, and we can exit
96+
if end - start > k:
97+
var pointer = start
98+
for i in range(start, end):
99+
if _array[i] < _array[end]:
100+
swap(i, pointer)
101+
pointer += 1
102+
swap(pointer, end)
103+
_nearly_sort(start, pointer - 1, k)
104+
_nearly_sort(pointer + 1, end, k)

‎project.godot‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,16 @@ smaller={
192192
"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":83,"unicode":0,"echo":false,"script":null)
193193
]
194194
}
195-
sound={
195+
toggle_sound={
196196
"deadzone": 0.5,
197197
"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":77,"unicode":0,"echo":false,"script":null)
198198
]
199199
}
200+
change_data={
201+
"deadzone": 0.5,
202+
"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":67,"unicode":0,"echo":false,"script":null)
203+
]
204+
}
200205

201206
[rendering]
202207

‎scenes/levels.tscn‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ __meta__ = {
4040
"_edit_use_anchors_": false
4141
}
4242

43-
[node name="Current" type="Label" parent="Levels/NamesContainer/Names"]
43+
[node name="Current" type="Button" parent="Levels/NamesContainer/Names"]
4444
margin_left = 565.0
4545
margin_right = 635.0
4646
margin_bottom = 19.0
47-
custom_colors/font_color = Color( 1, 0.690196, 0, 1 )
4847
text = "CURRENT"
49-
align = 1
5048

5149
[node name="Next" type="Label" parent="Levels/NamesContainer/Names"]
5250
margin_left = 643.0
@@ -91,6 +89,30 @@ margin_bottom = 431.0
9189
size_flags_vertical = 3
9290
script = ExtResource( 3 )
9391

92+
[node name="TypesContainer" type="MarginContainer" parent="Levels/Level/Right/Display"]
93+
visible = false
94+
margin_left = 218.0
95+
margin_top = 105.0
96+
margin_right = 398.0
97+
margin_bottom = 326.0
98+
size_flags_horizontal = 4
99+
size_flags_vertical = 4
100+
script = ExtResource( 3 )
101+
102+
[node name="Types" type="VBoxContainer" parent="Levels/Level/Right/Display/TypesContainer"]
103+
margin_left = 20.0
104+
margin_top = 20.0
105+
margin_right = 160.0
106+
margin_bottom = 201.0
107+
size_flags_horizontal = 4
108+
size_flags_vertical = 4
109+
110+
[node name="ArrayView" type="HBoxContainer" parent="Levels/Level/Right/Display"]
111+
margin_left = 20.0
112+
margin_top = 20.0
113+
margin_right = 596.0
114+
margin_bottom = 411.0
115+
94116
[node name="Info" type="HBoxContainer" parent="Levels/Level/Right"]
95117
margin_top = 439.0
96118
margin_right = 616.0
@@ -170,4 +192,5 @@ align = 2
170192
uppercase = true
171193

172194
[node name="Timer" type="Timer" parent="Levels"]
195+
[connection signal="pressed" from="Levels/NamesContainer/Names/Current" to="Levels" method="_on_Current_pressed"]
173196
[connection signal="timeout" from="Levels/Timer" to="Levels" method="_on_Timer_timeout"]

‎scenes/menu.tscn‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,57 @@ __meta__ = {
4343

4444
[node name="InstructionsContainer" type="MarginContainer" parent="MainMenu/Display"]
4545
visible = false
46-
margin_left = 509.0
47-
margin_top = 186.0
48-
margin_right = 689.0
49-
margin_bottom = 412.0
46+
margin_left = 484.0
47+
margin_top = 175.0
48+
margin_right = 714.0
49+
margin_bottom = 423.0
5050
size_flags_horizontal = 4
5151
size_flags_vertical = 4
5252
script = ExtResource( 3 )
5353

5454
[node name="Instructions" type="VBoxContainer" parent="MainMenu/Display/InstructionsContainer"]
5555
margin_left = 20.0
5656
margin_top = 20.0
57-
margin_right = 160.0
58-
margin_bottom = 206.0
57+
margin_right = 210.0
58+
margin_bottom = 228.0
5959
custom_constants/separation = 16
6060

6161
[node name="Controls" type="HBoxContainer" parent="MainMenu/Display/InstructionsContainer/Instructions"]
62-
margin_right = 140.0
63-
margin_bottom = 151.0
62+
margin_right = 190.0
63+
margin_bottom = 173.0
6464
custom_constants/separation = 20
6565

6666
[node name="Keys" type="Label" parent="MainMenu/Display/InstructionsContainer/Instructions/Controls"]
6767
margin_right = 50.0
68-
margin_bottom = 151.0
68+
margin_bottom = 173.0
6969
size_flags_horizontal = 4
7070
text = "W
7171
A
7272
S
7373
D
7474
M
75+
C
7576
esc
7677
space"
7778

7879
[node name="Actions" type="Label" parent="MainMenu/Display/InstructionsContainer/Instructions/Controls"]
7980
margin_left = 70.0
80-
margin_right = 140.0
81-
margin_bottom = 151.0
81+
margin_right = 190.0
82+
margin_bottom = 173.0
8283
text = "bigger
8384
slower
8485
smaller
8586
faster
86-
sound
87+
toggle sound
88+
change data
8789
back
8890
confirm"
8991
align = 2
9092

9193
[node name="Button" type="Button" parent="MainMenu/Display/InstructionsContainer/Instructions"]
92-
margin_top = 167.0
93-
margin_right = 140.0
94-
margin_bottom = 186.0
94+
margin_top = 189.0
95+
margin_right = 190.0
96+
margin_bottom = 208.0
9597
text = "OK"
9698

9799
[node name="Spacing" type="Control" parent="MainMenu"]

‎scripts/levels.gd‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,39 @@ const MAX_SIZE = 128
2121
var _index = LEVELS.find(GlobalScene.get_param("level"))
2222
var _level: ComparisonSort
2323
var _size = GlobalScene.get_param("size", ArrayModel.DEFAULT_SIZE)
24+
var _data_type = ArrayModel.DATA_TYPES.RANDOM_UNIQUE
2425

2526
func _ready():
27+
var types = $Level/Right/Display/TypesContainer/Types
28+
for type in ArrayModel.DATA_TYPES:
29+
var button = Button.new()
30+
button.text = type.replace("_", " ")
31+
button.connect("pressed", self, "_on_Button_pressed", [type])
32+
types.add_child(button)
33+
var top = types.get_child(0)
34+
var bottom = types.get_child(types.get_child_count() - 1)
35+
top.focus_neighbour_top = bottom.get_path()
36+
bottom.focus_neighbour_bottom = top.get_path()
37+
_reload()
38+
39+
func _reload():
40+
$NamesContainer/Names/Current.grab_focus()
2641
if _index == -1:
2742
_index = 0
28-
_level = LEVELS[_index].new(ArrayModel.new(_size))
43+
_level = LEVELS[_index].new(ArrayModel.new(_size, _data_type))
2944
_level.connect("done", self, "_on_ComparisonSort_done")
45+
_load_scores(_level)
46+
# Load level information
3047
$NamesContainer/Names/Current.text = _level.NAME
31-
for child in $Level/Right/Display.get_children():
32-
child.queue_free()
33-
$Level/Right/Display.add_child(ArrayView.new(_level))
34-
$Timer.start()
3548
$Level/Left/Code.text = _level.DESCRIPTION
3649
$Level/Right/Info/ControlsContainer/Controls.text = _level.CONTROLS
37-
_load_scores(_level)
50+
var view = $Level/Right/Display/ArrayView
51+
$Level/Right/Display.remove_child(view)
52+
view.queue_free()
53+
view = ArrayView.new(_level)
54+
view.name = "ArrayView"
55+
$Level/Right/Display.add_child(view)
56+
$Timer.start()
3857

3958
func _load_scores(level):
4059
var data = $Level/Right/Info/ScoresContainer/Scores/Data
@@ -52,7 +71,7 @@ func _switch_level(index):
5271
_index = 0
5372
else:
5473
_index = index
55-
_ready()
74+
_reload()
5675

5776
func _input(event):
5877
if event.is_action_pressed("ui_cancel"):
@@ -63,23 +82,38 @@ func _input(event):
6382
_switch_level(_index + 1)
6483
if event.is_action_pressed("bigger"):
6584
_size = min(_size * 2, MAX_SIZE)
66-
_ready()
85+
_reload()
6786
if event.is_action_pressed("smaller"):
6887
_size = max(_size / 2, MIN_SIZE)
69-
_ready()
88+
_reload()
7089
if event.is_action_pressed("faster"):
7190
$Timer.wait_time = max($Timer.wait_time / 2, MIN_WAIT)
7291
if event.is_action_pressed("slower"):
7392
$Timer.wait_time = min($Timer.wait_time * 2, MAX_WAIT)
74-
if event.is_action_pressed("ui_accept"):
75-
GlobalScene.change_scene("res://scenes/play.tscn",
76-
{"level": LEVELS[_index], "size": _size})
93+
if event.is_action_pressed("change_data"):
94+
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), true)
95+
$Level/Right/Display/ArrayView.hide()
96+
$Level/Right/Display/TypesContainer.show()
97+
$Timer.stop()
98+
$Level/Right/Display/TypesContainer/Types.get_child(0).grab_focus()
7799

78100
func _on_ComparisonSort_done():
79101
$Timer.stop()
80102
yield(get_tree().create_timer(1), "timeout")
81103
if _level.array.is_sorted():
82-
_ready()
104+
_reload()
83105

84106
func _on_Timer_timeout():
85107
_level.next(null)
108+
109+
func _on_Current_pressed():
110+
GlobalScene.change_scene("res://scenes/play.tscn",
111+
{"level": LEVELS[_index], "size": _size})
112+
113+
func _on_Button_pressed(data_type):
114+
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false)
115+
$Level/Right/Display/TypesContainer.hide()
116+
$Level/Right/Display/ArrayView.show()
117+
$Timer.start()
118+
_data_type = ArrayModel.DATA_TYPES[data_type]
119+
_reload()

‎views/array_sound.gd‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends Node
33

44
const SAMPLE_HZ = 44100
55
const MIN_HZ = 110
6-
const MAX_HZ = 880
6+
const MAX_HZ = 440
77

88
var frac: float
99
var player = AudioStreamPlayer.new()
@@ -32,7 +32,7 @@ func triangle(x):
3232
return 2 / PI * asin(sin(PI * x))
3333

3434
func _input(event):
35-
if event.is_action_pressed("sound"):
35+
if event.is_action_pressed("toggle_sound"):
3636
# Prevent event from propagating to ComparisonSort trigger
3737
get_tree().set_input_as_handled()
3838
var bus = AudioServer.get_bus_index("Master")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /