]> Untitled Git - catris.git/blob - script/board.gd
Added pieces. Needs proper collision handling
[catris.git] / script / board.gd
1 extends Control
2 class_name Board
3
4 @export var block_size: int = 20
5 @export var piece_catalogue: Array[PackedScene] = []
6
7 var _grid_filled: Array[bool] = []
8 var _player_position: Vector2i = Vector2i.ZERO
9 var _player_piece: Piece = 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         assert(piece_catalogue.size() >= 1, "Expected at least one piece in catalogue")
17         
18         # calculate grid
19         _grid_final_y_row = size.y / block_size
20         _grid_final_x_row = size.x / block_size
21         print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row))
22         
23         for i in range(0, _grid_final_x_row):
24                 for j in range(0, _grid_final_y_row):
25                         _grid_filled.append(false)
26         
27         _on_turn_timer_timeout()
28
29
30 func _input(event: InputEvent) -> void:
31         if event.is_action_pressed("player_left"):
32                 _move(Vector2i(-1, 0))
33         elif event.is_action_pressed("player_right"):
34                 _move(Vector2i(1, 0))
35                 
36         if event.is_action_pressed("player_up"):
37                 # TODO: rotate
38                 pass
39
40         if event.is_action("player_down"):
41                 _move(Vector2i(0, 1))
42
43
44 func _add_player_piece():
45         var scene: PackedScene = piece_catalogue.pick_random()
46         var piece: Piece = scene.instantiate()
47         piece.block_size = block_size
48         
49         _player_position = Vector2i(5, 0)
50         piece.position = _player_position * block_size
51         _player_piece = piece
52         
53         add_child(piece)
54         _num_pieces += 1
55
56
57 func _move(v: Vector2i):
58         var new_player_position: Vector2i = _player_position + v
59         
60         if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
61                 # ignore input that moves beyond lateral boundaries
62                 return
63         
64         if _is_grid_position_taken(new_player_position):
65                 # if movement is vertical and blocked, then we're done
66                 if v.y != 0:
67                         _end_round()
68                 return
69                 
70         # update player position
71         _player_position = new_player_position
72         if _player_piece:
73                 _player_piece.position = _player_position * block_size
74         
75         # if at the bottom of the grid, we're done
76         if (_player_position.y == _grid_final_y_row - 1):
77                 _end_round()
78
79 func _end_round():
80                 # disconnect player controls from current piece
81                 _player_piece = null
82                 
83                 # fill in collision grid with piece cells
84                 _set_grid_position(_player_position, true)
85                 
86                 # now start a new round
87                 _on_turn_timer_timeout()
88
89
90 func _on_turn_timer_timeout() -> void:
91         if _player_piece == null:
92                 _add_player_piece()
93         else:
94                 _move(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))