From 01be514adc1c580aa4a09882800f447e066d445f Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 21 Nov 2021 15:06:47 +0100 Subject: [PATCH] Add game over state --- GameBoard.gd | 16 ++++++++++++++++ Main.gd | 6 ++++++ Main.tscn | 37 ++++++++++++++++++++++++++++++------- Resources/UITextLarge.tres | 7 +++++++ UI.gd | 4 ++++ 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 Resources/UITextLarge.tres diff --git a/GameBoard.gd b/GameBoard.gd index 0cd9d56..4e56891 100644 --- a/GameBoard.gd +++ b/GameBoard.gd @@ -3,6 +3,7 @@ extends Node2D signal score_changed(new_score) signal new_next_shape(next_shape_scene) +signal game_over var shapes = [ preload("res://Shapes/ShapeI.tscn"), @@ -35,6 +36,8 @@ const LINE_CLEAR_SCORES = [ func _ready(): + randomize() + # Swipe input $SwipeHandler.connect("swipe_up", self, "rotate_shape") $SwipeHandler.connect("swipe_left", self, "move_left") @@ -64,6 +67,9 @@ func get_random_shape(): func update_board(): + if player_lost(): + game_over() + if can_active_move_down(): move_active_down() else: @@ -73,6 +79,11 @@ func update_board(): update_active_shape() +func game_over(): + $GravityTimer.stop() + emit_signal("game_over") + + func move_active_down(): active_shape.position.y += RASTER_SIZE @@ -89,6 +100,11 @@ func can_active_move_right(): return _can_active_move_towards(Vector2(RASTER_SIZE, 0.0)) +func player_lost(): + # The player lost if the active shape is within another shape, so it "cannot move towards itself" + return not _can_active_move_towards(Vector2.ZERO) + + func _can_active_move_towards(diff: Vector2): for block in active_shape.get_blocks(): var new_x = block.global_position.x + diff.x diff --git a/Main.gd b/Main.gd index 269450f..bf9c6d5 100644 --- a/Main.gd +++ b/Main.gd @@ -6,3 +6,9 @@ func _ready(): $GameBoard.connect("score_changed", $UI, "update_score") $GameBoard.connect("new_next_shape", $UI, "set_next_shape") + $GameBoard.connect("game_over", $UI, "on_game_over") + $UI/GameOverUI/VBoxContainer/RestartButton.connect("pressed", self, "reload") + + +func reload(): + get_tree().reload_current_scene() diff --git a/Main.tscn b/Main.tscn index b2816c1..bdad54b 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,13 +1,9 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=5 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] -[ext_resource path="res://Resources/Kenney Future.ttf" type="DynamicFontData" id=4] - -[sub_resource type="DynamicFont" id=1] -size = 128 -font_data = ExtResource( 4 ) +[ext_resource path="res://Resources/UITextLarge.tres" type="DynamicFont" id=4] [node name="Main" type="Node"] script = ExtResource( 3 ) @@ -38,7 +34,7 @@ margin_bottom = 150.0 margin_top = 3.0 margin_right = 107.0 margin_bottom = 147.0 -custom_fonts/font = SubResource( 1 ) +custom_fonts/font = ExtResource( 4 ) text = "0" [node name="Spacer" type="Control" parent="UI/VBoxContainer/HBoxContainer"] @@ -57,3 +53,30 @@ 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 ) + +[node name="GameOverUI" type="CenterContainer" parent="UI"] +visible = false +anchor_right = 1.0 +anchor_bottom = 1.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="VBoxContainer" type="VBoxContainer" parent="UI/GameOverUI"] +margin_left = 42.0 +margin_top = 811.0 +margin_right = 1037.0 +margin_bottom = 1109.0 + +[node name="GameOverText" type="Label" parent="UI/GameOverUI/VBoxContainer"] +margin_right = 995.0 +margin_bottom = 144.0 +custom_fonts/font = ExtResource( 4 ) +text = "Game Over!" + +[node name="RestartButton" type="Button" parent="UI/GameOverUI/VBoxContainer"] +margin_top = 148.0 +margin_right = 995.0 +margin_bottom = 298.0 +custom_fonts/font = ExtResource( 4 ) +text = "Restart" diff --git a/Resources/UITextLarge.tres b/Resources/UITextLarge.tres new file mode 100644 index 0000000..d98ab55 --- /dev/null +++ b/Resources/UITextLarge.tres @@ -0,0 +1,7 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://Resources/Kenney Future.ttf" type="DynamicFontData" id=1] + +[resource] +size = 128 +font_data = ExtResource( 1 ) diff --git a/UI.gd b/UI.gd index 9712468..0ba598c 100644 --- a/UI.gd +++ b/UI.gd @@ -16,6 +16,10 @@ func set_next_shape(shape_scene): $VBoxContainer/HBoxContainer/NextShape/Origin.add_child(shape_scene.instance()) +func on_game_over(): + $GameOverUI.visible = true + + # Resizes the UI to accomodate for notches, rounded corners, etc on mobile # Adapted from https://github.com/godotengine/godot/issues/49887 func _set_safe_area():