tetris-mobile/GameBoard.gd
2021-11-17 17:52:33 +01:00

90 lines
2.0 KiB
GDScript

extends Node2D
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://ShapeL.tscn").instance()
add_child(new_shape)
return new_shape
func update_board():
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
func move_right():
# TODO: Check for collision
active_shape.position.x += RASTER_SIZE
func rotate_shape():
active_shape.rotation_degrees += 90
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()
elif event.is_action_pressed("move_right"):
move_right()
elif event.is_action_pressed("rotate"):
rotate_shape()
elif event.is_action_pressed("drop"):
drop()