diff --git a/Autoloads/dissolve.gdshader b/Autoloads/dissolve.gdshader new file mode 100755 index 0000000..9bb5925 --- /dev/null +++ b/Autoloads/dissolve.gdshader @@ -0,0 +1,23 @@ +shader_type canvas_item; + +// Retro "dissolve" / "fizzle fade" transition. +// Every screen pixel (or block of pixels) gets a fixed random number from 0 to 1. +// As progress goes 0 -> 1, any pixel whose number is below progress is hidden, +// letting the new scene underneath show through. Same pixel = same number every +// frame, so it looks random but always finishes cleanly. + +uniform float progress : hint_range(0.0, 1.0) = 0.0; +// Size of each flipping block, in game pixels. 1 = single pixels, 4-8 = chunky. +uniform float cell_size = 1.0; +// If above 0, progress snaps to this many steps for a jerky old-hardware feel. +uniform float steps = 0.0; + +float hash(vec2 p) { + return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); +} + +void fragment() { + vec2 cell = floor(FRAGCOORD.xy / cell_size); + float p = steps > 0.0 ? floor(progress * steps) / steps : progress; + if (hash(cell) < p) discard; +} diff --git a/Autoloads/dissolve.gdshader.uid b/Autoloads/dissolve.gdshader.uid new file mode 100755 index 0000000..ba30959 --- /dev/null +++ b/Autoloads/dissolve.gdshader.uid @@ -0,0 +1 @@ +uid://drh44p3d5hy3s diff --git a/Autoloads/transition.gd b/Autoloads/transition.gd new file mode 100755 index 0000000..1b529ee --- /dev/null +++ b/Autoloads/transition.gd @@ -0,0 +1,65 @@ +extends CanvasLayer + +# Autoloaded as "Transition". Changes scenes with a 90s-style pixel dissolve: +# Transition.change_scene("res://Levels/some_level.tscn") +# Transition.reload_scene() +# +# How it works: take a screenshot of the current frame, lay it over everything, +# swap the scene underneath, then let the shader eat the screenshot away pixel +# by pixel to reveal the new scene. + +# Tweak these to change the look. +const DURATION = 0.6 # seconds for the whole dissolve +const CELL_SIZE = 1.0 # 1 = single game pixels, bigger = chunkier blocks +const STEPS = 0.0 # 0 = smooth, ~12-20 = jerky retro steps + +var rect := TextureRect.new() +var mat := ShaderMaterial.new() +var busy := false + +func _ready() -> void: + layer = 100 # draw above every other CanvasLayer + process_mode = Node.PROCESS_MODE_ALWAYS # keep working even if the game is paused + + mat.shader = preload("res://Autoloads/dissolve.gdshader") + mat.set_shader_parameter("cell_size", CELL_SIZE) + mat.set_shader_parameter("steps", STEPS) + + rect.material = mat + rect.set_anchors_preset(Control.PRESET_FULL_RECT) + rect.stretch_mode = TextureRect.STRETCH_SCALE + rect.mouse_filter = Control.MOUSE_FILTER_IGNORE + rect.visible = false + add_child(rect) + +func _unhandled_input(event: InputEvent) -> void: + # PROTOTYPE: press F9 to reload the level with the dissolve. Debug builds only. + if OS.is_debug_build() and event is InputEventKey and event.pressed \ + and not event.echo and event.keycode == KEY_F9: + reload_scene() + +func change_scene(path: String) -> void: + _dissolve(func(): get_tree().change_scene_to_file(path)) + +func reload_scene() -> void: + _dissolve(func(): get_tree().reload_current_scene()) + +func _dissolve(swap_scene: Callable) -> void: + if busy: return + busy = true + + # Snapshot the frame that's on screen right now. + var img := get_viewport().get_texture().get_image() + rect.texture = ImageTexture.create_from_image(img) + mat.set_shader_parameter("progress", 0.0) + rect.visible = true + + swap_scene.call() # the new scene loads underneath the snapshot + + var tween := create_tween() + tween.tween_property(mat, "shader_parameter/progress", 1.0, DURATION) + await tween.finished + + rect.visible = false + rect.texture = null + busy = false diff --git a/Autoloads/transition.gd.uid b/Autoloads/transition.gd.uid new file mode 100755 index 0000000..135a5fa --- /dev/null +++ b/Autoloads/transition.gd.uid @@ -0,0 +1 @@ +uid://b1yyg7b4txqtc diff --git a/UI/main_menu.gd b/UI/main_menu.gd new file mode 100755 index 0000000..9f8cd40 --- /dev/null +++ b/UI/main_menu.gd @@ -0,0 +1,33 @@ +extends Control +# Quick placeholder title screen. Start goes straight to the test level for now. + +const FIRST_LEVEL = "res://Levels/test_level.tscn" + +@onready var mainPanel = $CenterContainer +@onready var startButton = $CenterContainer/VBoxContainer/StartButton +@onready var optionsButton = $CenterContainer/VBoxContainer/OptionsButton +@onready var optionsMenu = $OptionsMenu + +func _ready() -> void: + # Keyboard focus on Start so arrow keys + Enter work right away. + startButton.grab_focus() + +func _unhandled_input(event: InputEvent) -> void: + # Escape backs out of the options screen. + if event.is_action_pressed("pause") and optionsMenu.visible: + optionsMenu.close() + get_viewport().set_input_as_handled() + +func _on_start_button_pressed() -> void: + Transition.change_scene(FIRST_LEVEL) + +func _on_options_button_pressed() -> void: + mainPanel.hide() + optionsMenu.open() + +func _on_options_menu_closed() -> void: + mainPanel.show() + optionsButton.grab_focus() + +func _on_quit_button_pressed() -> void: + get_tree().quit() diff --git a/UI/main_menu.gd.uid b/UI/main_menu.gd.uid new file mode 100755 index 0000000..f14eb0b --- /dev/null +++ b/UI/main_menu.gd.uid @@ -0,0 +1 @@ +uid://gvqslclaud6b diff --git a/UI/main_menu.tscn b/UI/main_menu.tscn new file mode 100755 index 0000000..108f25e --- /dev/null +++ b/UI/main_menu.tscn @@ -0,0 +1,65 @@ +[gd_scene format=3] + +[ext_resource type="Script" path="res://UI/main_menu.gd" id="1_main"] +[ext_resource type="PackedScene" path="res://UI/options_menu.tscn" id="2_options"] + +[node name="MainMenu" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_main") + +[node name="Background" type="ColorRect" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +color = Color(0.12, 0.1, 0.2, 1) + +[node name="CenterContainer" type="CenterContainer" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"] +custom_minimum_size = Vector2(160, 0) +layout_mode = 2 +theme_override_constants/separation = 8 + +[node name="Title" type="Label" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +theme_override_font_sizes/font_size = 32 +text = "Slime Platformer" +horizontal_alignment = 1 + +[node name="Spacer" type="Control" parent="CenterContainer/VBoxContainer"] +custom_minimum_size = Vector2(0, 16) +layout_mode = 2 + +[node name="StartButton" type="Button" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +text = "Start" + +[node name="OptionsButton" type="Button" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +text = "Options" + +[node name="QuitButton" type="Button" parent="CenterContainer/VBoxContainer"] +layout_mode = 2 +text = "Quit" + +[node name="OptionsMenu" parent="." instance=ExtResource("2_options")] +layout_mode = 1 + +[connection signal="pressed" from="CenterContainer/VBoxContainer/StartButton" to="." method="_on_start_button_pressed"] +[connection signal="pressed" from="CenterContainer/VBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"] +[connection signal="pressed" from="CenterContainer/VBoxContainer/QuitButton" to="." method="_on_quit_button_pressed"] +[connection signal="closed" from="OptionsMenu" to="." method="_on_options_menu_closed"] diff --git a/UI/pause_menu.gd b/UI/pause_menu.gd index decc492..570afe3 100755 --- a/UI/pause_menu.gd +++ b/UI/pause_menu.gd @@ -44,11 +44,9 @@ func _on_options_menu_closed() -> void: optionsButton.grab_focus() func _on_return_to_menu_button_pressed() -> void: - # TODO: Once there's a main menu scene, unpause and switch to it, e.g. - # get_tree().paused = false - # get_tree().change_scene_to_file("res://UI/main_menu.tscn") # Unpausing first matters: the paused state carries over to the new scene. - print("Main menu not made yet!") + get_tree().paused = false + Transition.change_scene("res://UI/main_menu.tscn") func _on_exit_button_pressed() -> void: get_tree().quit() diff --git a/project.godot b/project.godot index 20c254a..1d35cbb 100755 --- a/project.godot +++ b/project.godot @@ -11,13 +11,14 @@ config_version=5 [application] config/name="slime-platformer" -run/main_scene="uid://co1lfi5fpj54e" +run/main_scene="res://UI/main_menu.tscn" config/features=PackedStringArray("4.7", "GL Compatibility") config/icon="res://icon.svg" [autoload] Settings="*res://Autoloads/settings.gd" +Transition="*res://Autoloads/transition.gd" [display]