From 2f553c6c1c71810a4ff47a966c2bf39893233373 Mon Sep 17 00:00:00 2001 From: Clifton Palmer Date: Sat, 22 Mar 2025 15:55:35 +0200 Subject: [PATCH] Basic tetris behavior with 1 cell piece --- asset/cell.png | Bin 0 -> 182 bytes asset/cell.png.import | 34 +++++++++++++ asset/grid.png | Bin 0 -> 151 bytes asset/grid.png.import | 34 +++++++++++++ piece/cell.tscn | 14 ++++++ project.godot | 29 +++++++++++ scene/board.tscn | 31 ++++++++++++ scene/main.tscn | 50 +++++++++++++++++++ script/board.gd | 111 ++++++++++++++++++++++++++++++++++++++++++ script/board.gd.uid | 1 + script/cell.gd | 18 +++++++ script/cell.gd.uid | 1 + script/main.gd | 32 ++++++++++++ script/main.gd.uid | 1 + 14 files changed, 356 insertions(+) create mode 100644 asset/cell.png create mode 100644 asset/cell.png.import create mode 100644 asset/grid.png create mode 100644 asset/grid.png.import create mode 100644 piece/cell.tscn create mode 100644 scene/board.tscn create mode 100644 scene/main.tscn create mode 100644 script/board.gd create mode 100644 script/board.gd.uid create mode 100644 script/cell.gd create mode 100644 script/cell.gd.uid create mode 100644 script/main.gd create mode 100644 script/main.gd.uid diff --git a/asset/cell.png b/asset/cell.png new file mode 100644 index 0000000000000000000000000000000000000000..d7529c3ea3e4c62464994cbca785b4d1f47b6cb8 GIT binary patch literal 182 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc0wmQNuC@Uw&H|6fVg`m7HxOnNnd0mS6qGJ; zjVKAuPb(=;EJ|f?PR%K3^PzX!dKQsS#vH~5=YIR2u<4k| Y5M void: + _grid_final_y_row = size.y / block_size + _grid_final_x_row = size.x / block_size + print("Grid is " + str(_grid_final_x_row) + " by " + str(_grid_final_y_row)) + + for i in range(0, _grid_final_x_row): + for j in range(0, _grid_final_y_row): + _grid_filled.append(false) + + _on_turn_timer_timeout() + + +func _input(event: InputEvent) -> void: + if event.is_action_pressed("player_left"): + _move_cell(Vector2i(-1, 0)) + elif event.is_action_pressed("player_right"): + _move_cell(Vector2i(1, 0)) + + if event.is_action_pressed("player_up"): + # TODO: rotate + pass + + if event.is_action("player_down"): + _move_cell(Vector2i(0, 1)) + + +func _add_player_cell(): + var scene: PackedScene = cell_catalogue.pick_random() + var cell: Cell = scene.instantiate() + cell.block_size = block_size + + _player_position = Vector2i(5, 0) + cell.position = _player_position * block_size + _player_cell = cell + + add_child(cell) + _num_pieces += 1 + print("Added piece: " + str(_num_pieces)) + + +func _move_cell(v: Vector2i): + var new_player_position: Vector2i = _player_position + v + + if new_player_position.x < 0 or new_player_position.x >= _grid_final_x_row: + # ignore input that moves beyond lateral boundaries + return + + if _is_grid_position_taken(new_player_position): + # if movement is vertical and blocked, then we're done + if v.y != 0: + _end_round() + return + + # update player position + _player_position = new_player_position + if _player_cell: + _player_cell.position = _player_position * block_size + + # if at the bottom of the grid, we're done + if (_player_position.y == _grid_final_y_row - 1): + _end_round() + +func _end_round(): + print("Time for a new piece!") + # disconnect player controls from current piece + _player_cell = null + + # fill in collision grid with piece cells + _set_grid_position(_player_position, true) + #_print_grid() + + # now start a new round + _on_turn_timer_timeout() + + +func _on_turn_timer_timeout() -> void: + if _player_cell == null: + _add_player_cell() + else: + _move_cell(Vector2i(0, 1)) + %turnTimer.start() + + +func _set_grid_position(v: Vector2i, b: bool): + _grid_filled[v.x + v.y * _grid_final_x_row] = b + + +func _is_grid_position_taken(v: Vector2i) -> bool: + return _grid_filled[v.x + v.y * _grid_final_x_row] + + +func _print_grid(): + for i in range(0, _grid_final_y_row): + var row = [] + for j in range(0, _grid_final_x_row): + row.append(_grid_filled[j + i * _grid_final_x_row]) + print(str(row)) diff --git a/script/board.gd.uid b/script/board.gd.uid new file mode 100644 index 0000000..ba54aff --- /dev/null +++ b/script/board.gd.uid @@ -0,0 +1 @@ +uid://bex8wrsgbx63r diff --git a/script/cell.gd b/script/cell.gd new file mode 100644 index 0000000..e1f37ef --- /dev/null +++ b/script/cell.gd @@ -0,0 +1,18 @@ +@tool +extends Node2D +class_name Cell + + +@export var block_size: int = 10: + set(s): + block_size = s + _set_size_to_block_size() + + +func _ready() -> void: + _set_size_to_block_size() + + +func _set_size_to_block_size(): + if get_node_or_null("TextureRect"): + $TextureRect.size = Vector2(block_size, block_size) diff --git a/script/cell.gd.uid b/script/cell.gd.uid new file mode 100644 index 0000000..f25e5a2 --- /dev/null +++ b/script/cell.gd.uid @@ -0,0 +1 @@ +uid://ds1rixr57lt7e diff --git a/script/main.gd b/script/main.gd new file mode 100644 index 0000000..4fe5041 --- /dev/null +++ b/script/main.gd @@ -0,0 +1,32 @@ +extends Node + + +@export var fullscreen: bool = true +@export var screen_size_fractional: float = 0.5 + + +func _ready() -> void: + _set_project_version() + _set_screen_size() + + +func _set_project_version() -> void: + var version = ProjectSettings.get_setting("application/config/version") + %buildVersion.text = "v" + str(version) + + +func _set_screen_size() -> void: + var _screen_size: Vector2i = DisplayServer.screen_get_size() + if fullscreen: + DisplayServer.window_set_position(Vector2i.ZERO) + DisplayServer.window_set_size(_screen_size) + else: + var _win_pos = _screen_size * (1.0 - screen_size_fractional) * 0.5 + var _win_size = _screen_size * screen_size_fractional + DisplayServer.window_set_position(_win_pos) + DisplayServer.window_set_size(_win_size) + + +func _input(event: InputEvent) -> void: + if event.is_action_pressed("ui_cancel"): + get_tree().quit() diff --git a/script/main.gd.uid b/script/main.gd.uid new file mode 100644 index 0000000..2914f78 --- /dev/null +++ b/script/main.gd.uid @@ -0,0 +1 @@ +uid://8iyak3tfdvkh -- 2.47.2