4 @export var block_size: int = 20
5 @export var cell_catalogue: Array[PackedScene]
7 var _grid_filled: Array[bool] = []
8 var _player_position: Vector2i = Vector2i.ZERO
9 var _player_cell: Cell = null
10 var _grid_final_y_row: int = 0
11 var _grid_final_x_row: int = 0
13 var _num_pieces: int = 0
15 func _ready() -> void:
16 _grid_final_y_row = size.y / block_size
17 _grid_final_x_row = size.x / block_size
18 print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row))
20 for i in range(0, _grid_final_x_row):
21 for j in range(0, _grid_final_y_row):
22 _grid_filled.append(false)
24 _on_turn_timer_timeout()
27 func _input(event: InputEvent) -> void:
28 if event.is_action_pressed("player_left"):
29 _move_cell(Vector2i(-1, 0))
30 elif event.is_action_pressed("player_right"):
31 _move_cell(Vector2i(1, 0))
33 if event.is_action_pressed("player_up"):
37 if event.is_action("player_down"):
38 _move_cell(Vector2i(0, 1))
41 func _add_player_cell():
42 var scene: PackedScene = cell_catalogue.pick_random()
43 var cell: Cell = scene.instantiate()
44 cell.block_size = block_size
46 _player_position = Vector2i(5, 0)
47 cell.position = _player_position * block_size
52 print("Added piece: " + str(_num_pieces))
55 func _move_cell(v: Vector2i):
56 var new_player_position: Vector2i = _player_position + v
58 if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
59 # ignore input that moves beyond lateral boundaries
62 if _is_grid_position_taken(new_player_position):
63 # if movement is vertical and blocked, then we're done
68 # update player position
69 _player_position = new_player_position
71 _player_cell.position = _player_position * block_size
73 # if at the bottom of the grid, we're done
74 if (_player_position.y == _grid_final_y_row - 1):
78 print("Time for a new piece!")
79 # disconnect player controls from current piece
82 # fill in collision grid with piece cells
83 _set_grid_position(_player_position, true)
86 # now start a new round
87 _on_turn_timer_timeout()
90 func _on_turn_timer_timeout() -> void:
91 if _player_cell == null:
94 _move_cell(Vector2i(0, 1))
98 func _set_grid_position(v: Vector2i, b: bool):
99 _grid_filled[v.x + v.y * _grid_final_x_row] = b
102 func _is_grid_position_taken(v: Vector2i) -> bool:
103 return _grid_filled[v.x + v.y * _grid_final_x_row]
107 for i in range(0, _grid_final_y_row):
109 for j in range(0, _grid_final_x_row):
110 row.append(_grid_filled[j + i * _grid_final_x_row])