Added main menu and also dissolve screen transition
This commit is contained in:
parent
ca6cc3a370
commit
90bd03d194
9 changed files with 193 additions and 5 deletions
23
Autoloads/dissolve.gdshader
Executable file
23
Autoloads/dissolve.gdshader
Executable file
|
|
@ -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;
|
||||
}
|
||||
1
Autoloads/dissolve.gdshader.uid
Executable file
1
Autoloads/dissolve.gdshader.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://drh44p3d5hy3s
|
||||
65
Autoloads/transition.gd
Executable file
65
Autoloads/transition.gd
Executable file
|
|
@ -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
|
||||
1
Autoloads/transition.gd.uid
Executable file
1
Autoloads/transition.gd.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://b1yyg7b4txqtc
|
||||
33
UI/main_menu.gd
Executable file
33
UI/main_menu.gd
Executable file
|
|
@ -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()
|
||||
1
UI/main_menu.gd.uid
Executable file
1
UI/main_menu.gd.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://gvqslclaud6b
|
||||
65
UI/main_menu.tscn
Executable file
65
UI/main_menu.tscn
Executable file
|
|
@ -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"]
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue