summaryrefslogtreecommitdiff
path: root/scenes/levels/puzzle
diff options
context:
space:
mode:
authorrom <romangraef@gmail.com>2021-04-26 20:22:05 +0200
committerrom <romangraef@gmail.com>2021-04-26 20:23:00 +0200
commit94a5897d3361029ad4a498c1fe3f6f7216f7ce21 (patch)
treeff2a1f3255b8368b9e8144368f2327a03cf7155b /scenes/levels/puzzle
parent9ababad5e2ae483b144b9d3c20ecfe1beffa9fb2 (diff)
downloadldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.tar.gz
ldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.tar.bz2
ldjam48-94a5897d3361029ad4a498c1fe3f6f7216f7ce21.zip
15 puzzle
Diffstat (limited to 'scenes/levels/puzzle')
-rw-r--r--scenes/levels/puzzle/puzzle.gd83
-rw-r--r--scenes/levels/puzzle/puzzle.tscn9
2 files changed, 92 insertions, 0 deletions
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="."]