]> Untitled Git - catris.git/blob - script/board.gd
Added time and piece count to main display
[catris.git] / script / board.gd
1 extends Control
2 class_name Board
3
4
5 signal added_piece
6
7
8 @export var block_size: int = 20
9 @export var piece_catalogue: Array[PackedScene] = []
10
11 var _grid_filled: Array[bool] = []
12 var _player_position: Vector2i = Vector2i.ZERO
13 var _player_piece: Piece = null
14 var _grid_final_y_row: int = 0
15 var _grid_final_x_row: int = 0
16
17 var num_pieces: int = 0
18
19 func _ready() -> void:
20         assert(piece_catalogue.size() >= 1, "Expected at least one piece in catalogue")
21         
22         # calculate grid
23         _grid_final_y_row = size.y / block_size
24         _grid_final_x_row = size.x / block_size
25         print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row))
26         
27         for i in range(0, _grid_final_x_row):
28                 for j in range(0, _grid_final_y_row):
29                         _grid_filled.append(false)
30         
31         _on_turn_timer_timeout()
32
33
34 func _input(event: InputEvent) -> void:
35         if event.is_action_pressed("player_left"):
36                 _move(Vector2i(-1, 0))
37         elif event.is_action_pressed("player_right"):
38                 _move(Vector2i(1, 0))
39                 
40         if event.is_action_pressed("player_up"):
41                 # TODO: rotate
42                 pass
43
44         if event.is_action("player_down"):
45                 _move(Vector2i(0, 1))
46
47
48 func _add_player_piece():
49         var scene: PackedScene = piece_catalogue.pick_random()
50         var piece: Piece = scene.instantiate()
51         piece.block_size = block_size
52         
53         # TODO: start piece at center of board
54         _player_position = Vector2i(5, 0)
55         piece.position = _player_position * block_size
56         _player_piece = piece
57         
58         # add piece to scene tree and emit signal
59         add_child(piece)
60         num_pieces += 1
61         added_piece.emit()
62         
63         print(str(piece))
64
65
66 func _move(v: Vector2i):
67         var new_player_position: Vector2i = _player_position + v
68         
69         if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
70                 # ignore input that moves beyond lateral boundaries
71                 return
72         
73         if _is_grid_position_taken(new_player_position):
74                 # if movement is vertical and blocked, then we're done
75                 if v.y != 0:
76                         _end_round()
77                 return
78                 
79         # update player position
80         _player_position = new_player_position
81         if _player_piece:
82                 _player_piece.position = _player_position * block_size
83         
84         # if at the bottom of the grid, we're done
85         if (_player_position.y == _grid_final_y_row - 1):
86                 _end_round()
87
88 func _end_round():
89                 # disconnect player controls from current piece
90                 _player_piece = null
91                 
92                 # fill in collision grid with piece cells
93                 _set_grid_position(_player_position, true)
94                 
95                 # now start a new round
96                 _on_turn_timer_timeout()
97
98
99 func _on_turn_timer_timeout() -> void:
100         if _player_piece == null:
101                 _add_player_piece()
102         else:
103                 _move(Vector2i(0, 1))
104         %turnTimer.start()
105
106
107 func _set_grid_position(v: Vector2i, b: bool):
108         _grid_filled[v.x + v.y * _grid_final_x_row] = b
109
110
111 func _is_grid_position_taken(v: Vector2i) -> bool:
112         return _grid_filled[v.x + v.y * _grid_final_x_row]
113
114
115 func _print_grid():
116         for i in range(0, _grid_final_y_row):
117                 var row = []
118                 for j in range(0, _grid_final_x_row):
119                         row.append(_grid_filled[j + i * _grid_final_x_row])
120                 print(str(row))