]> Untitled Git - catris.git/commitdiff
Incomplete rotation handling
authorClifton Palmer <clifton.james.palmer@protonmail.com>
Wed, 26 Mar 2025 16:12:23 +0000 (18:12 +0200)
committerClifton Palmer <clifton.james.palmer@protonmail.com>
Thu, 27 Mar 2025 12:24:34 +0000 (14:24 +0200)
scene/board.tscn
scene/main.tscn
script/board.gd
script/piece.gd

index ae4396ae901a9d1da9f286dab35c8cafae89975f..f903938413cf1d3220420b2d5fd46b5837b71ccf 100644 (file)
@@ -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
index 392b8733ff4a9f3fa19327883100f75c481386e1..8e8bdf262e5342c05dbe8bcf633e0ca0bec75859 100644 (file)
@@ -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
index f820e00660ee0a0189185bdfb11aba007f933906..6109ed342269e6a87ccf8bcbf39ce953d1174234 100644 (file)
@@ -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)
 
index f2aa81c86cce545a99730c7408c3c871b60f1fa7..33a9c7daf92b97e1e3d8ef07ea33f3b56bba9c4d 100644 (file)
@@ -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()]