Some line removal logic

This commit is contained in:
karl 2021-11-17 17:02:34 +01:00
parent b1a4e83b91
commit 8c8d64486b
5 changed files with 72 additions and 6 deletions

View File

@ -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()

View File

@ -17,3 +17,5 @@ wait_time = 0.5
autostart = true
[node name="ShapeSquare" parent="." instance=ExtResource( 3 )]
[node name="StaticBlocks" type="Node2D" parent="."]

View File

@ -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 )

6
TetrisShape.gd Normal file
View File

@ -0,0 +1,6 @@
extends Node2D
class_name TetrisShape
func get_blocks():
return get_children()

View File

@ -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"