win/blur scenes

This commit is contained in:
Felipe M. 2023-10-15 22:51:14 +02:00
parent 1302f0abb9
commit f983fed94a
Signed by: fmartingr
GPG Key ID: CCFBC5637D4000A8
8 changed files with 155 additions and 16 deletions

24
Engine.gd Normal file
View File

@ -0,0 +1,24 @@
extends Node
signal game_paused
signal game_unpaused
signal game_finished(elapsed: float, movement_count: int)
signal game_reset
var is_game_paused: bool = false
func toggle_pause():
if is_game_paused:
game_unpaused.emit()
else:
game_paused.emit()
is_game_paused = !is_game_paused
func finish_game(elapsed: float, movement_count: int):
game_finished.emit(elapsed, movement_count)
func reset_game():
game_reset.emit()

View File

@ -6,7 +6,7 @@ const RAY_LENGTH = 5
@onready var disks = get_node("disks")
@export var disk_count: int = 3
@export var disk_count: int = 1
var movement_count = 0
@ -16,6 +16,9 @@ var starting_cylinder
# Stores the origin cylinder from a drag & drop movement
var origin_cylinder
# Set if the input should be disabled for the game (for pause/win scenes)
var input_disabled: bool = false
signal finished_movement
func reset():
@ -62,6 +65,11 @@ func _init_disks():
cylinder.append_disk(disk)
func _ready():
GlobalEngine.game_paused.connect(_on_game_paused)
GlobalEngine.game_unpaused.connect(_on_game_unpaused)
GlobalEngine.game_finished.connect(_on_game_finished)
GlobalEngine.game_reset.connect(_on_game_reset)
reset()
func _physics_process(_delta):
@ -69,6 +77,9 @@ func _physics_process(_delta):
_calculate_input()
func _calculate_input():
if input_disabled:
return
var hovering_cylinder
var space_state = get_world_3d().direct_space_state
var cam = get_viewport().get_camera_3d()
@ -113,8 +124,23 @@ func _calculate_input():
origin_cylinder = null
func _on_finished_movement(disk, cylinder):
func _on_finished_movement(_disk, cylinder):
movement_count += 1
if len(cylinder.disks) == disk_count and cylinder != starting_cylinder:
print("game finished!")
# TODO: elapsed time
print("Game finished!")
GlobalEngine.finish_game(0, movement_count)
func _on_game_paused():
input_disabled = true
func _on_game_unpaused():
input_disabled = false
func _on_game_finished():
input_disabled = true
func _on_game_reset():
reset()
#input_disabled = false

View File

@ -15,6 +15,10 @@ run/main_scene="res://scenes/game.tscn"
config/features=PackedStringArray("4.1", "Mobile")
config/icon="res://icon.jpg"
[autoload]
GlobalEngine="*res://Engine.gd"
[filesystem]
import/fbx/enabled=false

View File

@ -6,15 +6,12 @@ var to_lod: float
var action: int = 0 # 0 = none, 1 = blur, -1 = unblur
func _ready():
blur()
func _process(_delta):
var material = $Background.material
var lod = material.get("shader_parameter/lod")
var mat = $Background.material
var lod = mat.get("shader_parameter/lod")
if (action == 1 and lod <= to_lod) or (action == -1 and lod >= to_lod):
material.set("shader_parameter/lod", lod + speed * action)
$BlurText.text = "%.02f" % material.get("shader_parameter/lod")
mat.set("shader_parameter/lod", lod + speed * action)
$BlurText.text = "%.02f" % mat.get("shader_parameter/lod")
func _input(event):
if event is InputEventKey and Input.is_key_pressed(KEY_O):

14
scenes/Win.gd Normal file
View File

@ -0,0 +1,14 @@
extends Node2D
# Called when the node enters the scene tree for the first time.
func _ready():
GlobalEngine.game_finished.connect(_on_game_finished)
func _on_game_finished(elapsed: float, movement_count: int):
$NumberOfMovements.text = "%d" % movement_count
$TimeElapsed.text = "%2f" % elapsed
func _on_play_again_button_pressed():
GlobalEngine.reset_game()

51
scenes/Win.tscn Normal file
View File

@ -0,0 +1,51 @@
[gd_scene load_steps=2 format=3 uid="uid://ll4oe2eiykl2"]
[ext_resource type="Script" path="res://scenes/Win.gd" id="1_1sadr"]
[node name="Win" type="Node2D"]
script = ExtResource("1_1sadr")
[node name="Win Label" type="RichTextLabel" parent="."]
offset_left = 199.0
offset_top = 126.0
offset_right = 306.0
offset_bottom = 166.0
text = "You won!"
fit_content = true
tab_size = 6
[node name="LabelNumberOfMovements" type="RichTextLabel" parent="."]
offset_left = 784.0
offset_top = 114.0
offset_right = 1101.0
offset_bottom = 154.0
text = "Number of movements:"
[node name="NumberOfMovements" type="RichTextLabel" parent="."]
offset_left = 846.0
offset_top = 136.0
offset_right = 935.0
offset_bottom = 176.0
[node name="LabelTimeElapsed" type="RichTextLabel" parent="."]
offset_left = 783.0
offset_top = 199.0
offset_right = 1100.0
offset_bottom = 239.0
text = "Time Elapsed:
"
[node name="TimeElapsed" type="RichTextLabel" parent="."]
offset_left = 838.0
offset_top = 230.0
offset_right = 1003.0
offset_bottom = 270.0
[node name="PlayAgainButton" type="Button" parent="."]
offset_left = 252.0
offset_top = 392.0
offset_right = 412.0
offset_bottom = 501.0
text = "Play again"
[connection signal="pressed" from="PlayAgainButton" to="." method="_on_play_again_button_pressed"]

View File

@ -1,11 +1,29 @@
extends Node
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
GlobalEngine.game_paused.connect(_on_game_paused)
GlobalEngine.game_unpaused.connect(_on_game_unpaused)
GlobalEngine.game_finished.connect(_on_game_finished)
GlobalEngine.game_reset.connect(_on_game_reset)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func _input(event):
if event is InputEventKey and Input.is_key_pressed(KEY_P):
GlobalEngine.toggle_pause()
func _on_game_paused():
$Blur.blur()
func _on_game_unpaused():
$Blur.unblur()
func _on_game_finished(_elapsed: float, _movement_count: int):
print("Game._on_game_finsihed")
$Blur.blur()
$Win.show()
func _on_game_reset():
$Blur.unblur()
$Win.hide()

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=4 format=3 uid="uid://dp783hpcki0qp"]
[gd_scene load_steps=5 format=3 uid="uid://dp783hpcki0qp"]
[ext_resource type="Script" path="res://scenes/game.gd" id="1_bf6na"]
[ext_resource type="PackedScene" uid="uid://dlhvv8pl2cpxr" path="res://elements/game board.tscn" id="1_hvkf0"]
[ext_resource type="PackedScene" uid="uid://romjvk75gdnh" path="res://scenes/Blur.tscn" id="3_6fx1e"]
[ext_resource type="PackedScene" uid="uid://ll4oe2eiykl2" path="res://scenes/Win.tscn" id="4_32lyl"]
[node name="Game Scene" type="Node"]
script = ExtResource("1_bf6na")
@ -23,3 +24,7 @@ omni_range = 4.61418
transform = Transform3D(1, 0, 0, 0, 0.938962, 0.34402, 0, -0.34402, 0.938962, -0.140397, 2.30478, 4.06853)
[node name="Blur" parent="." instance=ExtResource("3_6fx1e")]
[node name="Win" parent="." instance=ExtResource("4_32lyl")]
visible = false
position = Vector2(2.08165e-12, 2.08165e-12)