]> Untitled Git - catris.git/blob - script/board.gd
5368ddd9fde2d16c4149cbd8eccbd4f9680381e7
[catris.git] / script / board.gd
1 extends Control
2 class_name Board
3
4 @export var block_size: int = 20
5 @export var cell_catalogue: Array[PackedScene]
6
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
12
13 var _num_pieces: int = 0
14
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))
19         
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)
23         
24         _on_turn_timer_timeout()
25
26
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))
32                 
33         if event.is_action_pressed("player_up"):
34                 # TODO: rotate
35                 pass
36
37         if event.is_action("player_down"):
38                 _move_cell(Vector2i(0, 1))
39
40
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
45         
46         _player_position = Vector2i(5, 0)
47         cell.position = _player_position * block_size
48         _player_cell = cell
49         
50         add_child(cell)
51         _num_pieces += 1
52         print("Added piece: " + str(_num_pieces))
53
54
55 func _move_cell(v: Vector2i):
56         var new_player_position: Vector2i = _player_position + v
57         
58         if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
59                 # ignore input that moves beyond lateral boundaries
60                 return
61         
62         if _is_grid_position_taken(new_player_position):
63                 # if movement is vertical and blocked, then we're done
64                 if v.y != 0:
65                         _end_round()
66                 return
67                 
68         # update player position
69         _player_position = new_player_position
70         if _player_cell:
71                 _player_cell.position = _player_position * block_size
72         
73         # if at the bottom of the grid, we're done
74         if (_player_position.y == _grid_final_y_row - 1):
75                 _end_round()
76
77 func _end_round():
78                 print("Time for a new piece!")
79                 # disconnect player controls from current piece
80                 _player_cell = null
81                 
82                 # fill in collision grid with piece cells
83                 _set_grid_position(_player_position, true)
84                 #_print_grid()
85                 
86                 # now start a new round
87                 _on_turn_timer_timeout()
88
89
90 func _on_turn_timer_timeout() -> void:
91         if _player_cell == null:
92                 _add_player_cell()
93         else:
94                 _move_cell(Vector2i(0, 1))
95         %turnTimer.start()
96
97
98 func _set_grid_position(v: Vector2i, b: bool):
99         _grid_filled[v.x + v.y * _grid_final_x_row] = b
100
101
102 func _is_grid_position_taken(v: Vector2i) -> bool:
103         return _grid_filled[v.x + v.y * _grid_final_x_row]
104
105
106 func _print_grid():
107         for i in range(0, _grid_final_y_row):
108                 var row = []
109                 for j in range(0, _grid_final_x_row):
110                         row.append(_grid_filled[j + i * _grid_final_x_row])
111                 print(str(row))