Add game over state
This commit is contained in:
parent
1e54f7bb52
commit
01be514adc
16
GameBoard.gd
16
GameBoard.gd
@ -3,6 +3,7 @@ extends Node2D
|
|||||||
|
|
||||||
signal score_changed(new_score)
|
signal score_changed(new_score)
|
||||||
signal new_next_shape(next_shape_scene)
|
signal new_next_shape(next_shape_scene)
|
||||||
|
signal game_over
|
||||||
|
|
||||||
var shapes = [
|
var shapes = [
|
||||||
preload("res://Shapes/ShapeI.tscn"),
|
preload("res://Shapes/ShapeI.tscn"),
|
||||||
@ -35,6 +36,8 @@ const LINE_CLEAR_SCORES = [
|
|||||||
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
|
randomize()
|
||||||
|
|
||||||
# Swipe input
|
# Swipe input
|
||||||
$SwipeHandler.connect("swipe_up", self, "rotate_shape")
|
$SwipeHandler.connect("swipe_up", self, "rotate_shape")
|
||||||
$SwipeHandler.connect("swipe_left", self, "move_left")
|
$SwipeHandler.connect("swipe_left", self, "move_left")
|
||||||
@ -64,6 +67,9 @@ func get_random_shape():
|
|||||||
|
|
||||||
|
|
||||||
func update_board():
|
func update_board():
|
||||||
|
if player_lost():
|
||||||
|
game_over()
|
||||||
|
|
||||||
if can_active_move_down():
|
if can_active_move_down():
|
||||||
move_active_down()
|
move_active_down()
|
||||||
else:
|
else:
|
||||||
@ -73,6 +79,11 @@ func update_board():
|
|||||||
update_active_shape()
|
update_active_shape()
|
||||||
|
|
||||||
|
|
||||||
|
func game_over():
|
||||||
|
$GravityTimer.stop()
|
||||||
|
emit_signal("game_over")
|
||||||
|
|
||||||
|
|
||||||
func move_active_down():
|
func move_active_down():
|
||||||
active_shape.position.y += RASTER_SIZE
|
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))
|
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):
|
func _can_active_move_towards(diff: Vector2):
|
||||||
for block in active_shape.get_blocks():
|
for block in active_shape.get_blocks():
|
||||||
var new_x = block.global_position.x + diff.x
|
var new_x = block.global_position.x + diff.x
|
||||||
|
6
Main.gd
6
Main.gd
@ -6,3 +6,9 @@ func _ready():
|
|||||||
|
|
||||||
$GameBoard.connect("score_changed", $UI, "update_score")
|
$GameBoard.connect("score_changed", $UI, "update_score")
|
||||||
$GameBoard.connect("new_next_shape", $UI, "set_next_shape")
|
$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()
|
||||||
|
37
Main.tscn
37
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://GameBoard.tscn" type="PackedScene" id=1]
|
||||||
[ext_resource path="res://UI.gd" type="Script" id=2]
|
[ext_resource path="res://UI.gd" type="Script" id=2]
|
||||||
[ext_resource path="res://Main.gd" type="Script" id=3]
|
[ext_resource path="res://Main.gd" type="Script" id=3]
|
||||||
[ext_resource path="res://Resources/Kenney Future.ttf" type="DynamicFontData" id=4]
|
[ext_resource path="res://Resources/UITextLarge.tres" type="DynamicFont" id=4]
|
||||||
|
|
||||||
[sub_resource type="DynamicFont" id=1]
|
|
||||||
size = 128
|
|
||||||
font_data = ExtResource( 4 )
|
|
||||||
|
|
||||||
[node name="Main" type="Node"]
|
[node name="Main" type="Node"]
|
||||||
script = ExtResource( 3 )
|
script = ExtResource( 3 )
|
||||||
@ -38,7 +34,7 @@ margin_bottom = 150.0
|
|||||||
margin_top = 3.0
|
margin_top = 3.0
|
||||||
margin_right = 107.0
|
margin_right = 107.0
|
||||||
margin_bottom = 147.0
|
margin_bottom = 147.0
|
||||||
custom_fonts/font = SubResource( 1 )
|
custom_fonts/font = ExtResource( 4 )
|
||||||
text = "0"
|
text = "0"
|
||||||
|
|
||||||
[node name="Spacer" type="Control" parent="UI/VBoxContainer/HBoxContainer"]
|
[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"]
|
[node name="Origin" type="Node2D" parent="UI/VBoxContainer/HBoxContainer/NextShape"]
|
||||||
position = Vector2( 67, 100 )
|
position = Vector2( 67, 100 )
|
||||||
scale = Vector2( 0.5, 0.5 )
|
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"
|
||||||
|
7
Resources/UITextLarge.tres
Normal file
7
Resources/UITextLarge.tres
Normal file
@ -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 )
|
4
UI.gd
4
UI.gd
@ -16,6 +16,10 @@ func set_next_shape(shape_scene):
|
|||||||
$VBoxContainer/HBoxContainer/NextShape/Origin.add_child(shape_scene.instance())
|
$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
|
# Resizes the UI to accomodate for notches, rounded corners, etc on mobile
|
||||||
# Adapted from https://github.com/godotengine/godot/issues/49887
|
# Adapted from https://github.com/godotengine/godot/issues/49887
|
||||||
func _set_safe_area():
|
func _set_safe_area():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user