From a626f4d0c215bd5f18ad3073ef9bda8e48ab8138 Mon Sep 17 00:00:00 2001 From: Clifton Palmer Date: Wed, 26 Mar 2025 18:12:23 +0200 Subject: [PATCH] Incomplete rotation handling --- scene/board.tscn | 2 +- scene/main.tscn | 17 +++++++++++++++++ script/board.gd | 12 +++++++----- script/piece.gd | 29 ++++++++++++++++++++++++++--- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/scene/board.tscn b/scene/board.tscn index ae4396a..f903938 100644 --- a/scene/board.tscn +++ b/scene/board.tscn @@ -9,7 +9,7 @@ [ext_resource type="PackedScene" uid="uid://36g8xep7m4ly" path="res://piece/T-piece.tscn" id="7_6upfg"] [node name="Board" type="Control"] -custom_minimum_size = Vector2(200, 400) +custom_minimum_size = Vector2(200, 500) layout_mode = 3 anchors_preset = 5 anchor_left = 0.5 diff --git a/scene/main.tscn b/scene/main.tscn index 392b873..8e8bdf2 100644 --- a/scene/main.tscn +++ b/scene/main.tscn @@ -61,6 +61,21 @@ theme_override_constants/margin_bottom = 50 layout_mode = 2 alignment = 1 +[node name="VSplitContainer3" type="HSplitContainer" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer"] +layout_mode = 2 +split_offset = 150 +dragging_enabled = false + +[node name="Label" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer3"] +layout_mode = 2 +text = "Score:" +horizontal_alignment = 2 + +[node name="label_score" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer3"] +unique_name_in_owner = true +layout_mode = 2 +text = "0" + [node name="VSplitContainer2" type="HSplitContainer" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer"] layout_mode = 2 split_offset = 150 @@ -69,6 +84,7 @@ dragging_enabled = false [node name="Label" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer2"] layout_mode = 2 text = "Time passed:" +horizontal_alignment = 2 [node name="label_time_passed" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer2"] unique_name_in_owner = true @@ -83,6 +99,7 @@ dragging_enabled = false [node name="Label" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer"] layout_mode = 2 text = "Number of pieces:" +horizontal_alignment = 2 [node name="label_num_pieces" type="Label" parent="MarginContainer/HBoxContainer/right/MarginContainer/VBoxContainer/VSplitContainer"] unique_name_in_owner = true diff --git a/script/board.gd b/script/board.gd index f820e00..6109ed3 100644 --- a/script/board.gd +++ b/script/board.gd @@ -39,8 +39,9 @@ func _input(event: InputEvent) -> void: _move(Vector2i(1, 0)) if event.is_action_pressed("player_up"): - # TODO: rotate - pass + if _player_piece: + _player_piece.rotate_left() + print(_player_piece.to_string()) if event.is_action("player_down"): _move(Vector2i(0, 1)) @@ -68,7 +69,7 @@ func _move(v: Vector2i): var new_player_position: Vector2i = _player_position + v # for each cell in the block, offset it by new position and check for collision - for pos: Vector2i in _player_piece.cell_grid: + for pos: Vector2i in _player_piece.get_cell_grid(): pos += new_player_position if pos.x < 0 or pos.x >= _grid_final_x_row: # ignore input that moves beyond lateral boundaries @@ -86,14 +87,15 @@ func _move(v: Vector2i): _player_piece.position = _player_position * block_size # if any cell's at the bottom of the grid, we're done - for pos: Vector2i in _player_piece.cell_grid: + for pos: Vector2i in _player_piece.get_cell_grid(): pos += _player_position if (pos.y >= _grid_final_y_row - 1): end_round.emit() + func _on_end_round(): # fill in collision grid with piece cells - for pos: Vector2i in _player_piece.cell_grid: + for pos: Vector2i in _player_piece.get_cell_grid(): pos += _player_position _set_grid_position(pos, true) diff --git a/script/piece.gd b/script/piece.gd index f2aa81c..33a9c7d 100644 --- a/script/piece.gd +++ b/script/piece.gd @@ -7,7 +7,11 @@ class_name Piece var cells: Array[Cell] = [] var block_size: int = 0 -var cell_grid: Array[Vector2i] = [] +var _cell_grid: Array[Vector2i] = [] + +var _rotation_angles: Array[float] = [0.0, 90.0, 180.0, 270.0] +var _rotation_index: int = 0 + func _ready() -> void: for c in self.get_children(): @@ -25,8 +29,27 @@ func _ready() -> void: for c in cells: var pos_x: int = floori(c.position.x / block_size) var pos_y: int = floori(c.position.y / block_size) - cell_grid.append(Vector2i(pos_x, pos_y)) + _cell_grid.append(Vector2i(pos_x, pos_y)) func _to_string() -> String: - return str(cell_grid) + return "%d: %s" % [rotation_degrees, str(get_cell_grid())] + + +func get_cell_grid() -> Array: + if rotation_degrees == 0.0: + return _cell_grid + elif rotation_degrees == 90.0: + return _cell_grid.map(func f(v): return Vector2i(v.y, v.x)) + elif rotation_degrees == 180.0: + return _cell_grid.map(func f(v): return Vector2i(-v.x, -v.y)) + elif rotation_degrees == 270.0: + return _cell_grid.map(func f(v): return Vector2i(-v.y, -v.x)) + else: + assert(false, "Invalid rotation: " + str(global_rotation_degrees)) + return [] + + +func rotate_left(): + _rotation_index += 1 + rotation_degrees = _rotation_angles[_rotation_index % _rotation_angles.size()] -- 2.47.2