diff options
author | rom <romangraef@gmail.com> | 2021-04-26 20:22:05 +0200 |
---|---|---|
committer | rom <romangraef@gmail.com> | 2021-04-26 20:23:00 +0200 |
commit | 94a5897d3361029ad4a498c1fe3f6f7216f7ce21 (patch) | |
tree | ff2a1f3255b8368b9e8144368f2327a03cf7155b | |
parent | 9ababad5e2ae483b144b9d3c20ecfe1beffa9fb2 (diff) | |
download | ldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.tar.gz ldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.tar.bz2 ldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.zip |
15 puzzle
-rw-r--r-- | components/button.tscn | 1 | ||||
-rw-r--r-- | scenes/levels/base_level.gd | 1 | ||||
-rw-r--r-- | scenes/levels/base_level.tscn | 1 | ||||
-rw-r--r-- | scenes/levels/puzzle/puzzle.gd | 83 | ||||
-rw-r--r-- | scenes/levels/puzzle/puzzle.tscn | 9 | ||||
-rw-r--r-- | scenes/menu.tscn | 1 |
6 files changed, 96 insertions, 0 deletions
diff --git a/components/button.tscn b/components/button.tscn index 883bda7..e3d784a 100644 --- a/components/button.tscn +++ b/components/button.tscn @@ -7,6 +7,7 @@ [node name="Button" type="Area2D"] script = ExtResource( 5 ) +text = "" [node name="Left" type="Sprite" parent="."] position = Vector2( -24, 0 ) diff --git a/scenes/levels/base_level.gd b/scenes/levels/base_level.gd index ca3e0eb..7a2c71e 100644 --- a/scenes/levels/base_level.gd +++ b/scenes/levels/base_level.gd @@ -11,6 +11,7 @@ const _levels = [ preload("res://scenes/levels/typeracer.tscn"), preload("res://scenes/levels/reactiontest.tscn"), preload("res://scenes/levels/pixel/PixelDefender.tscn"), + preload("res://scenes/levels/puzzle/puzzle.tscn"), ] var _current_level = 0 diff --git a/scenes/levels/base_level.tscn b/scenes/levels/base_level.tscn index 6681a7d..e2d57ba 100644 --- a/scenes/levels/base_level.tscn +++ b/scenes/levels/base_level.tscn @@ -41,6 +41,7 @@ position = Vector2( 571.642, -311.599 ) text = "Menu" [node name="PauseMenu" parent="Camera2D" instance=ExtResource( 5 )] + [connection signal="done" from="Camera2D" to="." method="_on_Camera2D_done"] [connection signal="on_click" from="Camera2D/MenuButton" to="." method="_on_MenuButton_on_click"] [connection signal="unpause" from="Camera2D/PauseMenu" to="." method="_on_PauseMenu_unpause"] diff --git a/scenes/levels/puzzle/puzzle.gd b/scenes/levels/puzzle/puzzle.gd new file mode 100644 index 0000000..219907f --- /dev/null +++ b/scenes/levels/puzzle/puzzle.gd @@ -0,0 +1,83 @@ +extends Level + + + +const _button_comp = preload("res://components/button.tscn") + +const dim = 4 + +var parts = [] +onready var order = range(dim*dim) + +func _ready(): + order.shuffle() + for i in range(dim*dim-1): + var button = _button_comp.instance() + var label = Label.new() + label.name = "CustomLabel" + label.text = str(i+1) + label.rect_size = Vector2(32,32) + label.rect_position = Vector2(-16,-16) + label.align = HALIGN_CENTER + label.valign = VALIGN_CENTER + button.fixed_width = 30 + button.add_child(label) + button.connect("on_click", self, "on_anybutton_click", [i+1]) + parts.append(button) + $Pieces.add_child(button) + _render_state() + +func _position_from_index(idx): + return Vector2(idx % dim, int(idx / dim)) + +func on_anybutton_click(piece_name): + var pos = _button_position(piece_name) + _do_push(pos, _hole_position()) + +func _render_state(): + for i in range(dim*dim): + if order[i] == 0: + continue + var piece = parts[order[i]-1] + piece.position = _position_from_index(i) * 80 + +func _check_state(): + for i in range(dim*dim): + if order[i] != i: + return + finish_level() + + +func _hole_position(): + return _button_position(0) + +func _button_position(piece_name): + for i in range(dim*dim): + if order[i] == piece_name: + return i + push_error("No "+str(piece_name)+" present") + assert(false) + +func _do_push(from, to): + if to == from: + return + var same_row = int(to / dim) == int(from/dim) + var same_col = int(to % dim) == int(from % dim) + var dir = sign(from - to) + if not (same_row or same_col): + print("Invalid move") + return + print("Performing push from ", from, " to ", to, " in direction ", dir) + var i = to + while i != from: + var next_pos + if same_row: + next_pos = i + 1 * dir + else: + next_pos = i + dim * dir + order[i] = order[next_pos] + i = next_pos + order[from] = 0 + _render_state() + _check_state() + diff --git a/scenes/levels/puzzle/puzzle.tscn b/scenes/levels/puzzle/puzzle.tscn new file mode 100644 index 0000000..ec10f81 --- /dev/null +++ b/scenes/levels/puzzle/puzzle.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scenes/levels/puzzle/puzzle.gd" type="Script" id=1] + +[node name="Node2D" type="Node2D"] +position = Vector2( 533.969, 239.439 ) +script = ExtResource( 1 ) + +[node name="Pieces" type="Node2D" parent="."] diff --git a/scenes/menu.tscn b/scenes/menu.tscn index ad2f2d4..b1ca973 100644 --- a/scenes/menu.tscn +++ b/scenes/menu.tscn @@ -224,6 +224,7 @@ anims/tosettings = SubResource( 4 ) [node name="success_sound" type="AudioStreamPlayer" parent="."] stream = ExtResource( 9 ) + [connection signal="on_click" from="Content/Back" to="." method="_on_Back_on_click"] [connection signal="on_click" from="Content/HardMode" to="." method="_on_HardMode_on_click"] [connection signal="on_click" from="Content/Fullscreen" to="." method="_on_Fullscreen_on_click"] |