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