9 @export var block_size: int = 20
10 @export var piece_catalogue: Array[PackedScene] = []
12 var _grid_filled: Array[bool] = []
13 var _player_position: Vector2i = Vector2i.ZERO
14 var _player_piece: Piece = null
15 var _grid_final_y_row: int = 0
16 var _grid_final_x_row: int = 0
18 var num_pieces: int = 0
19 const start_pos: Vector2i = Vector2i(5, 2)
21 func _ready() -> void:
22 assert(piece_catalogue.size() >= 1, "Expected at least one piece in catalogue")
25 _grid_final_y_row = size.y / block_size
26 _grid_final_x_row = size.x / block_size
27 print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row))
29 for i in range(0, _grid_final_x_row):
30 for j in range(0, _grid_final_y_row):
31 _grid_filled.append(false)
33 _on_turn_timer_timeout()
36 func _input(event: InputEvent) -> void:
37 if event.is_action_pressed("player_left"):
38 _move(Vector2i(-1, 0))
39 elif event.is_action_pressed("player_right"):
42 if event.is_action_pressed("player_up"):
44 _player_piece.rotate_left()
45 print(_player_piece.to_string())
47 if event.is_action_pressed("player_down"):
48 # move piece to bottom
51 b = _move(Vector2i(0, 1))
54 func _add_player_piece():
55 var scene: PackedScene = piece_catalogue.pick_random()
56 var piece: Piece = scene.instantiate()
57 piece.block_size = block_size
59 # TODO: start piece at center of board
60 _player_position = start_pos
61 piece.position = _player_position * block_size
64 # add piece to scene tree and emit signal
70 # returns TRUE if piece can move more, FALSE if end state
71 func _move(v: Vector2i) -> bool:
72 assert(_player_piece != null, "Player piece must not be null")
74 var new_player_position: Vector2i = _player_position + v
76 # for each cell in the block, offset it by new position and check for collision
77 for pos: Vector2i in _player_piece.get_cell_grid():
78 pos += new_player_position
79 if pos.x < 0 or pos.x >= _grid_final_x_row:
80 # ignore input that moves beyond lateral boundaries
83 if _is_grid_position_taken(pos):
84 # if movement is vertical and blocked, then we're done
89 # update player position
90 _player_position = new_player_position
92 _player_piece.position = _player_position * block_size
94 # if any cell's at the bottom of the grid, we're done
95 for pos: Vector2i in _player_piece.get_cell_grid():
96 pos += _player_position
97 if (pos.y >= _grid_final_y_row - 1):
104 func _on_end_round():
105 # disconnect player controls from current piece
106 var piece: Piece = _player_piece
109 # fill in collision grid with piece cells
110 for pos: Vector2i in piece.get_cell_grid():
111 pos += _player_position
112 _set_grid_position(pos, true)
114 # now start a new round
115 _on_turn_timer_timeout()
118 func _on_turn_timer_timeout() -> void:
119 if _player_piece == null:
122 _move(Vector2i(0, 1))
126 func _set_grid_position(v: Vector2i, b: bool):
127 _grid_filled[v.x + v.y * _grid_final_x_row] = b
130 func _is_grid_position_taken(v: Vector2i) -> bool:
131 return _grid_filled[v.x + v.y * _grid_final_x_row]