extends Control class_name Board @export var block_size: int = 20 @export var cell_catalogue: Array[PackedScene] var _grid_filled: Array[bool] = [] var _player_position: Vector2i = Vector2i.ZERO var _player_cell: Cell = null var _grid_final_y_row: int = 0 var _grid_final_x_row: int = 0 var _num_pieces: int = 0 func _ready() -> void: _grid_final_y_row = size.y / block_size _grid_final_x_row = size.x / block_size print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row)) for i in range(0, _grid_final_x_row): for j in range(0, _grid_final_y_row): _grid_filled.append(false) _on_turn_timer_timeout() func _input(event: InputEvent) -> void: if event.is_action_pressed("player_left"): _move_cell(Vector2i(-1, 0)) elif event.is_action_pressed("player_right"): _move_cell(Vector2i(1, 0)) if event.is_action_pressed("player_up"): # TODO: rotate pass if event.is_action("player_down"): _move_cell(Vector2i(0, 1)) func _add_player_cell(): var scene: PackedScene = cell_catalogue.pick_random() var cell: Cell = scene.instantiate() cell.block_size = block_size _player_position = Vector2i(5, 0) cell.position = _player_position * block_size _player_cell = cell add_child(cell) _num_pieces += 1 print("Added piece: " + str(_num_pieces)) func _move_cell(v: Vector2i): var new_player_position: Vector2i = _player_position + v if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row: # ignore input that moves beyond lateral boundaries return 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 # update player position _player_position = new_player_position if _player_cell: _player_cell.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(): print("Time for a new piece!") # disconnect player controls from current piece _player_cell = null # fill in collision grid with piece cells _set_grid_position(_player_position, true) #_print_grid() # now start a new round _on_turn_timer_timeout() func _on_turn_timer_timeout() -> void: if _player_cell == null: _add_player_cell() else: _move_cell(Vector2i(0, 1)) %turnTimer.start() func _set_grid_position(v: Vector2i, b: bool): _grid_filled[v.x + v.y * _grid_final_x_row] = b 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))