]> Untitled Git - catris.git/blob - script/board.gd
f4057916203a76e3cc55e0b02873404c0d1a101e
[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         print(str(piece))
56
57
58 func _move(v: Vector2i):
59         var new_player_position: Vector2i = _player_position + v
60         
61         if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row:
62                 # ignore input that moves beyond lateral boundaries
63                 return
64         
65         if _is_grid_position_taken(new_player_position):
66                 # if movement is vertical and blocked, then we're done
67                 if v.y != 0:
68                         _end_round()
69                 return
70                 
71         # update player position
72         _player_position = new_player_position
73         if _player_piece:
74                 _player_piece.position = _player_position * block_size
75         
76         # if at the bottom of the grid, we're done
77         if (_player_position.y == _grid_final_y_row - 1):
78                 _end_round()
79
80 func _end_round():
81                 # disconnect player controls from current piece
82                 _player_piece = null
83                 
84                 # fill in collision grid with piece cells
85                 _set_grid_position(_player_position, true)
86                 
87                 # now start a new round
88                 _on_turn_timer_timeout()
89
90
91 func _on_turn_timer_timeout() -> void:
92         if _player_piece == null:
93                 _add_player_piece()
94         else:
95                 _move(Vector2i(0, 1))
96         %turnTimer.start()
97
98
99 func _set_grid_position(v: Vector2i, b: bool):
100         _grid_filled[v.x + v.y * _grid_final_x_row] = b
101
102
103 func _is_grid_position_taken(v: Vector2i) -> bool:
104         return _grid_filled[v.x + v.y * _grid_final_x_row]
105
106
107 func _print_grid():
108         for i in range(0, _grid_final_y_row):
109                 var row = []
110                 for j in range(0, _grid_final_x_row):
111                         row.append(_grid_filled[j + i * _grid_final_x_row])
112                 print(str(row))