Add score and next shape UI
This commit is contained in:
parent
97315f74e6
commit
7de25e7c21
46
GameBoard.gd
46
GameBoard.gd
@ -1,6 +1,9 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
signal score_changed(new_score)
|
||||
signal new_next_shape(next_shape_scene)
|
||||
|
||||
var shapes = [
|
||||
preload("res://ShapeI.tscn"),
|
||||
preload("res://ShapeJ.tscn"),
|
||||
@ -12,6 +15,9 @@ var shapes = [
|
||||
]
|
||||
|
||||
var active_shape: TetrisShape
|
||||
var next_shape
|
||||
|
||||
var score := 0
|
||||
|
||||
const RASTER_SIZE = 64
|
||||
const BOTTOM = RASTER_SIZE * 10
|
||||
@ -19,6 +25,14 @@ const SPAWN = -RASTER_SIZE * 7
|
||||
const LEFT = -RASTER_SIZE * 5
|
||||
const RIGHT = RASTER_SIZE * 5
|
||||
|
||||
const LINE_CLEAR_SCORES = [
|
||||
0,
|
||||
10,
|
||||
25,
|
||||
60,
|
||||
100
|
||||
]
|
||||
|
||||
|
||||
func _ready():
|
||||
# Swipe input
|
||||
@ -29,14 +43,24 @@ func _ready():
|
||||
|
||||
$GravityTimer.connect("timeout", self, "update_board")
|
||||
|
||||
active_shape = get_random_shape()
|
||||
update_active_shape()
|
||||
|
||||
|
||||
func update_active_shape():
|
||||
if not next_shape:
|
||||
active_shape = get_random_shape().instance()
|
||||
else:
|
||||
active_shape = next_shape.instance()
|
||||
|
||||
add_child(active_shape)
|
||||
active_shape.position.y = SPAWN
|
||||
|
||||
next_shape = get_random_shape()
|
||||
emit_signal("new_next_shape", next_shape)
|
||||
|
||||
|
||||
func get_random_shape():
|
||||
var new_shape = shapes[randi() % shapes.size()].instance()
|
||||
add_child(new_shape)
|
||||
new_shape.position.y = SPAWN
|
||||
return new_shape
|
||||
return shapes[randi() % shapes.size()]
|
||||
|
||||
|
||||
func update_board():
|
||||
@ -46,7 +70,7 @@ func update_board():
|
||||
turn_active_into_static()
|
||||
check_for_full_line()
|
||||
|
||||
active_shape = get_random_shape()
|
||||
update_active_shape()
|
||||
|
||||
|
||||
func move_active_down():
|
||||
@ -106,10 +130,15 @@ func check_for_full_line():
|
||||
|
||||
# Remember which blocks we need to move down later (we can't apply this immediately because that
|
||||
# would mess with comparisons to the line_counts
|
||||
var pending_position_diffs = {}
|
||||
var pending_position_diffs := {}
|
||||
|
||||
# Check how many full lines we have this frame because the score increases non-linearly
|
||||
var number_of_full_lines := 0
|
||||
|
||||
for line_count_y in line_counts.keys():
|
||||
if line_counts[line_count_y] == 10:
|
||||
number_of_full_lines += 1
|
||||
|
||||
# Free this line
|
||||
for line_block in $StaticBlocks.get_children():
|
||||
if line_block.position.y == line_count_y:
|
||||
@ -127,6 +156,9 @@ func check_for_full_line():
|
||||
if is_instance_valid(block):
|
||||
block.position.y += pending_position_diffs[block]
|
||||
|
||||
score += LINE_CLEAR_SCORES[number_of_full_lines]
|
||||
emit_signal("score_changed", score)
|
||||
|
||||
|
||||
func move_left():
|
||||
if can_active_move_left():
|
||||
|
8
Main.gd
Normal file
8
Main.gd
Normal file
@ -0,0 +1,8 @@
|
||||
extends Node
|
||||
|
||||
|
||||
func _ready():
|
||||
$UI.set_next_shape($GameBoard.next_shape)
|
||||
|
||||
$GameBoard.connect("score_changed", $UI, "update_score")
|
||||
$GameBoard.connect("new_next_shape", $UI, "set_next_shape")
|
49
Main.tscn
49
Main.tscn
@ -1,8 +1,53 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://GameBoard.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://UI.gd" type="Script" id=2]
|
||||
[ext_resource path="res://Main.gd" type="Script" id=3]
|
||||
|
||||
[node name="Main" type="Node2D"]
|
||||
[node name="Main" type="Node"]
|
||||
script = ExtResource( 3 )
|
||||
|
||||
[node name="GameBoard" parent="." instance=ExtResource( 1 )]
|
||||
position = Vector2( 547, 813 )
|
||||
|
||||
[node name="UI" type="Control" parent="."]
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
script = ExtResource( 2 )
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="UI"]
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="UI/VBoxContainer"]
|
||||
margin_right = 1080.0
|
||||
margin_bottom = 150.0
|
||||
|
||||
[node name="Score" type="Label" parent="UI/VBoxContainer/HBoxContainer"]
|
||||
margin_top = 68.0
|
||||
margin_right = 8.0
|
||||
margin_bottom = 82.0
|
||||
text = "0"
|
||||
|
||||
[node name="Spacer" type="Control" parent="UI/VBoxContainer/HBoxContainer"]
|
||||
margin_left = 12.0
|
||||
margin_right = 926.0
|
||||
margin_bottom = 150.0
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="NextShape" type="PanelContainer" parent="UI/VBoxContainer/HBoxContainer"]
|
||||
margin_left = 930.0
|
||||
margin_right = 1080.0
|
||||
margin_bottom = 150.0
|
||||
rect_min_size = Vector2( 150, 150 )
|
||||
|
||||
[node name="Origin" type="Node2D" parent="UI/VBoxContainer/HBoxContainer/NextShape"]
|
||||
position = Vector2( 67, 100 )
|
||||
scale = Vector2( 0.5, 0.5 )
|
||||
|
12
UI.gd
Normal file
12
UI.gd
Normal file
@ -0,0 +1,12 @@
|
||||
extends Control
|
||||
|
||||
|
||||
func update_score(new_score):
|
||||
$VBoxContainer/HBoxContainer/Score.text = str(new_score)
|
||||
|
||||
|
||||
func set_next_shape(shape_scene):
|
||||
for child in $VBoxContainer/HBoxContainer/NextShape/Origin.get_children():
|
||||
child.free()
|
||||
|
||||
$VBoxContainer/HBoxContainer/NextShape/Origin.add_child(shape_scene.instance())
|
Loading…
x
Reference in New Issue
Block a user