]> Untitled Git - catris.git/blobdiff - script/board.gd
Added proper shape collision
[catris.git] / script / board.gd
index f4057916203a76e3cc55e0b02873404c0d1a101e..f820e00660ee0a0189185bdfb11aba007f933906 100644 (file)
@@ -1,6 +1,11 @@
 extends Control
 class_name Board
 
+
+signal added_piece
+signal end_round
+
+
 @export var block_size: int = 20
 @export var piece_catalogue: Array[PackedScene] = []
 
@@ -10,7 +15,7 @@ var _player_piece: Piece = null
 var _grid_final_y_row: int = 0
 var _grid_final_x_row: int = 0
 
-var _num_pieces: int = 0
+var num_pieces: int = 0
 
 func _ready() -> void:
        assert(piece_catalogue.size() >= 1, "Expected at least one piece in catalogue")
@@ -46,44 +51,55 @@ func _add_player_piece():
        var piece: Piece = scene.instantiate()
        piece.block_size = block_size
        
+       # TODO: start piece at center of board
        _player_position = Vector2i(5, 0)
        piece.position = _player_position * block_size
        _player_piece = piece
        
+       # add piece to scene tree and emit signal
        add_child(piece)
-       _num_pieces += 1
-       print(str(piece))
+       num_pieces += 1
+       added_piece.emit()
 
 
 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()
 
@@ -102,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))