From 8c8d64486b2cc1061fd19f062632a6de344310b1 Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 17 Nov 2021 17:02:34 +0100 Subject: [PATCH] Some line removal logic --- GameBoard.gd | 56 +++++++++++++++++++++++++++++++++++++++++++----- GameBoard.tscn | 2 ++ ShapeSquare.tscn | 4 +++- TetrisShape.gd | 6 ++++++ project.godot | 10 +++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 TetrisShape.gd diff --git a/GameBoard.gd b/GameBoard.gd index 20593db..ca08acf 100644 --- a/GameBoard.gd +++ b/GameBoard.gd @@ -1,23 +1,62 @@ extends Node2D -onready var active_shape: Node2D = $ShapeSquare +onready var active_shape: TetrisShape = $ShapeSquare const RASTER_SIZE = 64 +const BOTTOM = RASTER_SIZE * 10 func _ready(): $GravityTimer.connect("timeout", self, "update_board") +func get_random_shape(): + # TODO: Make random + var new_shape = preload("res://ShapeSquare.tscn").instance() + add_child(new_shape) + return new_shape + + func update_board(): - if active_shape.position.y == RASTER_SIZE * 9: - # TODO: Turn into static blocks - return + if active_shape.position.y == RASTER_SIZE * 10: + turn_active_into_static() + check_for_full_line() + + active_shape = get_random_shape() active_shape.position.y += RASTER_SIZE +func turn_active_into_static(): + for block in active_shape.get_blocks(): + var global_shape_position = block.global_position + active_shape.remove_child(block) + $StaticBlocks.add_child(block) + block.global_position = global_shape_position + + active_shape.free() + + +func check_for_full_line(): + var line_counts = {} # Maps a y position to a count + + for block in $StaticBlocks.get_children(): + if not line_counts.has(block.position.y): + line_counts[block.position.y] = 0 + line_counts[block.position.y] += 1 + + for line_count_y in line_counts.keys(): + if line_counts[line_count_y] == 10: + # Free this line + for line_block in $StaticBlocks.get_children(): + if line_block.position.y == line_count_y: + line_block.free() + elif line_block.position.y > line_count_y: + # Move higher-up blocks down + line_block.position.y += RASTER_SIZE + + func move_left(): # TODO: Check for collision active_shape.position.x -= RASTER_SIZE @@ -28,6 +67,13 @@ func move_right(): active_shape.position.x += RASTER_SIZE +func drop(): + active_shape.position.y = BOTTOM + + # Restart the timer to give full time for sliding the piece + $GravityTimer.start() + + func _input(event): if event.is_action_pressed("move_left"): move_left() @@ -36,4 +82,4 @@ func _input(event): elif event.is_action_pressed("rotate"): pass elif event.is_action_pressed("drop"): - pass + drop() diff --git a/GameBoard.tscn b/GameBoard.tscn index 8f2ae00..610e327 100644 --- a/GameBoard.tscn +++ b/GameBoard.tscn @@ -17,3 +17,5 @@ wait_time = 0.5 autostart = true [node name="ShapeSquare" parent="." instance=ExtResource( 3 )] + +[node name="StaticBlocks" type="Node2D" parent="."] diff --git a/ShapeSquare.tscn b/ShapeSquare.tscn index 35ebad9..4f75ec4 100644 --- a/ShapeSquare.tscn +++ b/ShapeSquare.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://Block.tscn" type="PackedScene" id=1] +[ext_resource path="res://TetrisShape.gd" type="Script" id=2] [node name="ShapeSquare" type="Node2D"] +script = ExtResource( 2 ) [node name="Block" parent="." instance=ExtResource( 1 )] position = Vector2( 32, 32 ) diff --git a/TetrisShape.gd b/TetrisShape.gd new file mode 100644 index 0000000..1e1d838 --- /dev/null +++ b/TetrisShape.gd @@ -0,0 +1,6 @@ +extends Node2D +class_name TetrisShape + + +func get_blocks(): + return get_children() diff --git a/project.godot b/project.godot index c5b9213..001b2bc 100644 --- a/project.godot +++ b/project.godot @@ -8,6 +8,16 @@ config_version=4 +_global_script_classes=[ { +"base": "Node2D", +"class": "TetrisShape", +"language": "GDScript", +"path": "res://TetrisShape.gd" +} ] +_global_script_class_icons={ +"TetrisShape": "" +} + [application] config/name="Tetris"