]> Untitled Git - catris.git/commitdiff
Added proper shape collision
authorClifton Palmer <clifton.james.palmer@protonmail.com>
Wed, 26 Mar 2025 12:06:59 +0000 (14:06 +0200)
committerClifton Palmer <clifton.james.palmer@protonmail.com>
Wed, 26 Mar 2025 12:09:36 +0000 (14:09 +0200)
scene/board.tscn
script/board.gd
script/main.gd

index 732cbd093bff37587e196bfeb81a4b37aef7c687..ae4396ae901a9d1da9f286dab35c8cafae89975f 100644 (file)
@@ -1,9 +1,12 @@
-[gd_scene load_steps=5 format=3 uid="uid://4saff6435gux"]
+[gd_scene load_steps=8 format=3 uid="uid://4saff6435gux"]
 
 [ext_resource type="Script" uid="uid://bex8wrsgbx63r" path="res://script/board.gd" id="1_b3264"]
-[ext_resource type="PackedScene" uid="uid://dkiqoguo8ulrj" path="res://piece/base-piece.tscn" id="2_rl6p6"]
 [ext_resource type="PackedScene" uid="uid://baol0a7qqcn3x" path="res://piece/I-piece.tscn" id="3_cpyxj"]
 [ext_resource type="Texture2D" uid="uid://b6om08wjtci31" path="res://asset/grid.png" id="3_rl6p6"]
+[ext_resource type="PackedScene" uid="uid://dy0ckr6gerb0u" path="res://piece/L-piece.tscn" id="4_vojld"]
+[ext_resource type="PackedScene" uid="uid://d0810r1cqtcgi" path="res://piece/O-piece.tscn" id="5_tbg0t"]
+[ext_resource type="PackedScene" uid="uid://cippodirvtoil" path="res://piece/S-piece.tscn" id="6_ye24f"]
+[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)
@@ -13,7 +16,7 @@ anchor_left = 0.5
 anchor_right = 0.5
 grow_horizontal = 2
 script = ExtResource("1_b3264")
-piece_catalogue = Array[PackedScene]([ExtResource("2_rl6p6"), ExtResource("3_cpyxj")])
+piece_catalogue = Array[PackedScene]([ExtResource("3_cpyxj"), ExtResource("4_vojld"), ExtResource("5_tbg0t"), ExtResource("6_ye24f"), ExtResource("7_6upfg")])
 
 [node name="grid" type="TextureRect" parent="."]
 unique_name_in_owner = true
@@ -29,4 +32,5 @@ stretch_mode = 1
 [node name="turnTimer" type="Timer" parent="."]
 unique_name_in_owner = true
 
+[connection signal="end_round" from="." to="." method="_on_end_round"]
 [connection signal="timeout" from="turnTimer" to="." method="_on_turn_timer_timeout"]
index 9f70dfa0f39ca8e986559417b82b21f1ffd13307..f820e00660ee0a0189185bdfb11aba007f933906 100644 (file)
@@ -3,6 +3,7 @@ class_name Board
 
 
 signal added_piece
+signal end_round
 
 
 @export var block_size: int = 20
@@ -59,39 +60,46 @@ func _add_player_piece():
        add_child(piece)
        num_pieces += 1
        added_piece.emit()
-       
-       print(str(piece))
 
 
 func _move(v: Vector2i):
-       var new_player_position: Vector2i = _player_position + v
+       assert(_player_piece != null, "Player piece must not be null")
        
-       if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
-               # ignore input that moves beyond lateral boundaries
-               return
+       var new_player_position: Vector2i = _player_position + v
        
-       if _is_grid_position_taken(new_player_position):
-               # if movement is vertical and blocked, then we're done
-               if v.y != 0:
-                       _end_round()
-               return
+       # for each cell in the block, offset it by new position and check for collision
+       for pos: Vector2i in _player_piece.cell_grid:
+               pos += new_player_position
+               if pos.x < 0 or pos.x >= _grid_final_x_row:
+                       # ignore input that moves beyond lateral boundaries
+                       return
                
+               if _is_grid_position_taken(pos):
+                       # if movement is vertical and blocked, then we're done
+                       if v.y != 0:
+                               end_round.emit()
+                       return
+                       
        # update player position
        _player_position = new_player_position
        if _player_piece:
                _player_piece.position = _player_position * block_size
-       
-       # if at the bottom of the grid, we're done
-       if (_player_position.y == _grid_final_y_row - 1):
-               _end_round()
 
-func _end_round():
+       # if any cell's at the bottom of the grid, we're done
+       for pos: Vector2i in _player_piece.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:
+                       pos += _player_position
+                       _set_grid_position(pos, true)
+
                # disconnect player controls from current piece
                _player_piece = null
                
-               # fill in collision grid with piece cells
-               _set_grid_position(_player_position, true)
-               
                # now start a new round
                _on_turn_timer_timeout()
 
@@ -110,11 +118,3 @@ func _set_grid_position(v: Vector2i, b: bool):
 
 func _is_grid_position_taken(v: Vector2i) -> bool:
        return _grid_filled[v.x + v.y * _grid_final_x_row]
-
-
-func _print_grid():
-       for i in range(0, _grid_final_y_row):
-               var row = []
-               for j in range(0, _grid_final_x_row):
-                       row.append(_grid_filled[j + i * _grid_final_x_row])
-               print(str(row))
index 1821739fcf10ab5c95558b52a91d0b0fedcf2697..62806252cb08038fb4e93a355cf90d71747620c7 100644 (file)
@@ -38,7 +38,7 @@ func _input(event: InputEvent) -> void:
                        get_tree().quit()
 
 
-func _process(delta: float) -> void:
+func _process(_delta: float) -> void:
        if _started:
                _elapsed_time = Time.get_ticks_msec() - _start_time
                %label_time_passed.text = "%04d" % [_elapsed_time]