Skip to content

Commit 931699a

Browse files
committed
feat: add array model and array view component
Wrote a wrapper ArrayModel class in models/, which is where future data structures should go. It is used by the ArrayView class in views/, which is where future displays should go.
1 parent 0156277 commit 931699a

File tree

6 files changed

+74
-14
lines changed

6 files changed

+74
-14
lines changed

assets/theme.theme

1 Byte
Binary file not shown.

models/array_model.gd

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
A plain old one-dimensional random access array.
3+
"""
4+
5+
extends Reference
6+
class_name ArrayModel
7+
8+
var array = []
9+
var size
10+
11+
func _init(size):
12+
for i in range(1, size + 1):
13+
array.append(Element.new(i))
14+
array.shuffle()
15+
self.size = size
16+
17+
func get(index):
18+
return array[index]
19+
20+
class Element:
21+
var value
22+
var emphasized
23+
24+
func _init(value):
25+
self.value = value
26+
self.emphasized = false

project.godot

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88

99
config_version=4
1010

11-
_global_script_classes=[ ]
11+
_global_script_classes=[ {
12+
"base": "Reference",
13+
"class": "ArrayModel",
14+
"language": "GDScript",
15+
"path": "res://models/array_model.gd"
16+
}, {
17+
"base": "HBoxContainer",
18+
"class": "ArrayView",
19+
"language": "GDScript",
20+
"path": "res://views/array_view.gd"
21+
} ]
1222
_global_script_class_icons={
13-
23+
"ArrayModel": "",
24+
"ArrayView": ""
1425
}
1526

1627
[application]

scenes/play.tscn

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ margin_bottom = 1020.0
5555
size_flags_vertical = 3
5656
script = ExtResource( 3 )
5757

58-
[node name="Display" type="Control" parent="PlayingScreen/DisplayBorder"]
59-
margin_left = 20.0
60-
margin_top = 20.0
61-
margin_right = 1840.0
62-
margin_bottom = 914.0
63-
6458
[node name="Timer" type="Timer" parent="."]
6559
one_shot = true
6660
autostart = true

scripts/play.gd

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

55
func _ready():
6+
$HUDBorder/HUD/Level.text = scene.get_param("level")
7+
# Show ready text
68
var label = Label.new()
79
label.text = "ready..."
810
label.align = Label.ALIGN_CENTER
911
label.set_anchors_preset(Control.PRESET_HCENTER_WIDE)
10-
$DisplayBorder/Display.add_child(label)
11-
$HUDBorder/HUD/Level.text = scene.get_param("level")
12-
match scene.get_param("level"):
13-
"BUBBLE SORT":
14-
pass
12+
$DisplayBorder.add_child(label)
1513

1614
func _process(delta):
15+
# Show elapsed time in milliseconds
1716
var elapsed_time = stepify((OS.get_ticks_msec() - start_time) / 1000.0, 0.001)
1817
$HUDBorder/HUD/Score.text = "0.000" if start_time == -1 else "%.3f" % elapsed_time
1918

2019
func _on_Timer_timeout():
21-
$DisplayBorder/Display.get_child(0).queue_free()
2220
start_time = OS.get_ticks_msec()
21+
# Delete ready text
22+
$DisplayBorder.get_child(0).queue_free()
23+
# Load level
24+
var array = ArrayModel.new(10)
25+
match scene.get_param("level"):
26+
"BUBBLE SORT":
27+
pass
28+
$DisplayBorder.add_child(ArrayView.new(array))

views/array_view.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extends HBoxContainer
2+
class_name ArrayView
3+
4+
const GREEN = Color(0.2, 1, 0.2)
5+
const ORANGE = Color(1, 0.69, 0)
6+
7+
var model: ArrayModel
8+
var rects = []
9+
10+
func _init(model):
11+
self.model = model
12+
for i in range(model.size):
13+
var rect = ColorRect.new()
14+
rect.size_flags_horizontal = Control.SIZE_EXPAND_FILL
15+
rect.size_flags_vertical = Control.SIZE_SHRINK_END
16+
rect.color = GREEN
17+
rects.append(rect)
18+
add_child(rect)
19+
20+
func _draw():
21+
for i in range(model.size):
22+
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

0 commit comments

Comments
 (0)