]> Untitled Git - catris.git/blobdiff - script/board.gd
Broken: doesn't rotate correctly on the right side
[catris.git] / script / board.gd
index 2e90425247b3dd9d6f75842c03f14467cbba98a5..bc056160704b329a67381e209e3e16c80105faa1 100644 (file)
@@ -41,11 +41,32 @@ func _input(event: InputEvent) -> void:
                
        if event.is_action_pressed("player_up"):
                if _player_piece:
-                       _player_piece.rotate_left()
-                       print(_player_piece.to_string())
+                       _player_piece.rotate_cells_left()
+                       
+                       # if player piece has cells off to the right or left of the board,
+                       # then shift it back onto the board
+                       var leftmost: int = 0
+                       var rightmost: int = 0
+                       for pos: Vector2i in _player_piece.get_cell_grid():
+                               pos += _player_position # remember, pos is relative!
+                               if pos.x < leftmost:
+                                       leftmost = pos.x
+                               elif pos.x > _grid_final_x_row - 1 and pos.x > rightmost:
+                                       rightmost = pos.x - _grid_final_x_row - 1
+                       
+       
+                       print("l,r: %d, %d" % [leftmost, rightmost])
+                       
+                       if leftmost < 0:
+                               _move(-Vector2i(leftmost, 0))
+                       elif rightmost > 0:
+                               _move(-Vector2i(rightmost, 0))
 
-       if event.is_action("player_down"):
-               _move(Vector2i(0, 1))
+       if event.is_action_pressed("player_down"):
+               # move piece to bottom 
+               var b := true
+               while b:
+                       b = _move(Vector2i(0, 1))
 
 
 func _add_player_piece():
@@ -64,7 +85,8 @@ func _add_player_piece():
        added_piece.emit()
 
 
-func _move(v: Vector2i):
+# returns TRUE if piece can move more, FALSE if end state 
+func _move(v: Vector2i) -> bool:
        assert(_player_piece != null, "Player piece must not be null")
        
        var new_player_position: Vector2i = _player_position + v
@@ -74,13 +96,13 @@ func _move(v: Vector2i):
                pos += new_player_position
                if pos.x < 0 or pos.x >= _grid_final_x_row:
                        # ignore input that moves beyond lateral boundaries
-                       return
+                       return false
                
                if _is_grid_position_taken(pos):
                        # if movement is vertical and blocked, then we're done
                        if v.y != 0:
                                end_round.emit()
-                       return
+                       return false
                        
        # update player position
        _player_position = new_player_position
@@ -92,16 +114,20 @@ func _move(v: Vector2i):
                pos += _player_position
                if (pos.y >= _grid_final_y_row - 1):
                        end_round.emit()
+                       return false
+       
+       return true
 
 
 func _on_end_round():
+               # disconnect player controls from current piece
+               var piece: Piece = _player_piece
+               _player_piece = null
+               
                # fill in collision grid with piece cells
-               for pos: Vector2i in _player_piece.get_cell_grid():
+               for pos: Vector2i in piece.get_cell_grid():
                        pos += _player_position
                        _set_grid_position(pos, true)
-
-               # disconnect player controls from current piece
-               _player_piece = null
                
                # now start a new round
                _on_turn_timer_timeout()