Skip to content

Commit

Permalink
menus-and-scene-transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
braydeejohnson committed Jul 27, 2023
1 parent c7f72fc commit 8dcdf86
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 115 deletions.
25 changes: 25 additions & 0 deletions assets/themes/default_theme.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[gd_resource type="Theme" load_steps=7 format=3 uid="uid://65dakj0ukuj6"]

[ext_resource type="FontFile" uid="uid://dtywkluh6lms5" path="res://assets/fonts/Early GameBoy.ttf" id="1_7bvv0"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_urgfj"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_7314y"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_eqtds"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_db648"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_42rn8"]

[resource]
default_font = ExtResource("1_7bvv0")
default_font_size = 16
Button/colors/font_focus_color = Color(0.25098, 0.384314, 0.592157, 1)
Button/colors/font_hover_color = Color(0.721569, 0.831373, 1, 1)
Button/colors/font_pressed_color = Color(0.207843, 0.266667, 0.380392, 1)
Button/styles/disabled = SubResource("StyleBoxEmpty_urgfj")
Button/styles/focus = SubResource("StyleBoxEmpty_7314y")
Button/styles/hover = SubResource("StyleBoxEmpty_eqtds")
Button/styles/normal = SubResource("StyleBoxEmpty_db648")
Button/styles/pressed = SubResource("StyleBoxEmpty_42rn8")
50 changes: 39 additions & 11 deletions scenes/game.gd
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
extends Node2D
class_name Game

@export var tank: Tank
@export var TankScene: PackedScene
@export var WorldScene: PackedScene
@export var ui: UI

@onready var camera: Camera = $Camera2D

func _ready():
if !tank.collected.is_connected(ui._on_collected):
tank.collected.connect(ui._on_collected)
if !tank.reload_progress.is_connected(ui._on_reload_progress):
tank.reload_progress.connect(ui._on_reload_progress)
if !tank.reloaded.is_connected(ui._on_reloaded):
tank.reloaded.connect(ui._on_reloaded)

call_deferred("start_game")

var tank: Tank
var world: World


func start_game():
world = WorldScene.instantiate()
add_child(world)
move_child(world, 0)

tank = TankScene.instantiate()
world.add_child(tank)
tank.position = Vector2(922, 623)
tank.collected.connect(ui._on_collected)
tank.reload_progress.connect(ui._on_reload_progress)
tank.reloaded.connect(ui._on_reloaded)

camera.change_target(tank)
get_tree().paused = false

await get_tree().create_timer(2).timeout
var crate: Crate = World.get_closest_crate_to(tank.position)
if !crate:
Expand All @@ -32,3 +40,23 @@ func start_game():
camera.change_target(tank)
camera.change_mode(Camera.MODES.TARGET_MOUSE_BLENDED)
tank.has_control = true


func _on_ui_start_game():
start_game()


func _on_ui_quit_to_menu():
if world:
world.queue_free()
world = null
tank = null
camera.reset()


func _on_ui_menu_closed():
get_tree().paused = false


func _on_ui_menu_opened():
get_tree().paused = true
25 changes: 13 additions & 12 deletions scenes/game.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@
[ext_resource type="PackedScene" uid="uid://dts7spmsrfa7u" path="res://scenes/entities/tank/tank.tscn" id="2_21h7o"]
[ext_resource type="PackedScene" uid="uid://0ck0mnhnjt8m" path="res://scenes/ui/ui.tscn" id="4_uclr4"]
[ext_resource type="AudioStream" uid="uid://jt1uonaoisg" path="res://assets/audio/music/Rolemusic - The White Frame.mp3" id="5_c1nwv"]
[ext_resource type="Script" path="res://scenes/camera.gd" id="6_hjgyj"]
[ext_resource type="Script" path="res://scenes/ui/camera.gd" id="6_khat2"]

[node name="game" type="Node2D" node_paths=PackedStringArray("tank", "ui")]
[node name="game" type="Node2D" node_paths=PackedStringArray("ui")]
script = ExtResource("1_uu6mj")
tank = NodePath("Tank")
TankScene = ExtResource("2_21h7o")
WorldScene = ExtResource("1_2mp7s")
ui = NodePath("UI")

[node name="World" parent="." instance=ExtResource("1_2mp7s")]

[node name="Tank" parent="." instance=ExtResource("2_21h7o")]
position = Vector2(1464, 844)

[node name="UI" parent="." instance=ExtResource("4_uclr4")]
process_mode = 3

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
process_mode = 3
stream = ExtResource("5_c1nwv")
volume_db = -20.0
autoplay = true
bus = &"Music"

[node name="Camera2D" type="Camera2D" parent="." node_paths=PackedStringArray("target")]
[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(1452, 438)
zoom = Vector2(2, 2)
limit_left = 0
limit_top = 0
limit_right = 3056
limit_bottom = 1586
editor_draw_limits = true
script = ExtResource("6_hjgyj")
target = NodePath("../Tank")
script = ExtResource("6_khat2")
SMOOTH_SPEED = 2.0

[connection signal="menu_closed" from="UI" to="." method="_on_ui_menu_closed"]
[connection signal="menu_opened" from="UI" to="." method="_on_ui_menu_opened"]
[connection signal="quit_to_menu" from="UI" to="." method="_on_ui_quit_to_menu"]
[connection signal="start_game" from="UI" to="." method="_on_ui_start_game"]
12 changes: 8 additions & 4 deletions scenes/camera.gd → scenes/ui/camera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ class_name Camera

enum MODES { TARGET, TARGET_MOUSE_BLENDED }

@export var target: Node = null
@export var target: Node = null:
set(tar):
fallback_target = tar if fallback_target == null else fallback_target
target = tar
@export var mode: MODES = MODES.TARGET_MOUSE_BLENDED
@export var MAX_DISTANCE: float = 50
@export var SMOOTH_SPEED: float = 1

var target_position := Vector2.INF
var fallback_target: Node = null

func _ready():
fallback_target = target


func _process(delta):
Expand Down Expand Up @@ -46,3 +46,7 @@ func change_target(new_target: Node) -> void:
func _clear_target() -> void:
target = fallback_target
change_mode(MODES.TARGET_MOUSE_BLENDED)

func reset() -> void:
target = null
fallback_target = null
43 changes: 43 additions & 0 deletions scenes/ui/menus/game_menu.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extends Control

signal return_to_game()
signal main_menu()

@onready var SFX_BUS_ID = AudioServer.get_bus_index("SFX")
@onready var MUSIC_BUS_ID = AudioServer.get_bus_index("Music")
@onready var buttons_v_box = %ButtonsVBox


func _on_music_slider_value_changed(value):
AudioServer.set_bus_volume_db(MUSIC_BUS_ID, linear_to_db(value))
AudioServer.set_bus_mute(MUSIC_BUS_ID, value < .05)


func _on_sfx_slider_value_changed(value):
AudioServer.set_bus_volume_db(SFX_BUS_ID, linear_to_db(value))
AudioServer.set_bus_mute(SFX_BUS_ID, value < .05)


func focus_button() -> void:
if buttons_v_box:
var button: Button = buttons_v_box.get_child(0)
if button is Button:
button.grab_focus()


func _on_visibility_changed():
if visible:
focus_button()



func _on_return_to_game_button_pressed():
return_to_game.emit()


func _on_main_menu_button_pressed():
main_menu.emit()


func _on_quit_button_pressed():
get_tree().quit()
109 changes: 109 additions & 0 deletions scenes/ui/menus/game_menu.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
[gd_scene load_steps=3 format=3 uid="uid://daxygoynvdyya"]

[ext_resource type="Theme" uid="uid://65dakj0ukuj6" path="res://assets/themes/default_theme.tres" id="1_mwxwu"]
[ext_resource type="Script" path="res://scenes/ui/menus/game_menu.gd" id="2_pgx83"]

[node name="GameMenu" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = ExtResource("1_mwxwu")
script = ExtResource("2_pgx83")

[node name="ColorRect" 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.0705882, 0.0705882, 0.0705882, 0.780392)

[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 256
theme_override_constants/margin_right = 256

[node name="ButtonsVBox" type="VBoxContainer" parent="MarginContainer"]
unique_name_in_owner = true
layout_mode = 2
alignment = 1

[node name="ReturnToGameButton" type="Button" parent="MarginContainer/ButtonsVBox"]
layout_mode = 2
focus_neighbor_top = NodePath("../QuitButton")
focus_neighbor_bottom = NodePath("../GridContainer/MusicSlider")
focus_next = NodePath("../GridContainer/MusicSlider")
focus_previous = NodePath("../QuitButton")
text = "Continue Game"
alignment = 0

[node name="GridContainer" type="GridContainer" parent="MarginContainer/ButtonsVBox"]
layout_mode = 2
size_flags_vertical = 4
theme_override_constants/h_separation = 16
columns = 2

[node name="MusicLabel" type="Label" parent="MarginContainer/ButtonsVBox/GridContainer"]
layout_mode = 2
text = "Music"

[node name="MusicSlider" type="HSlider" parent="MarginContainer/ButtonsVBox/GridContainer"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
focus_neighbor_top = NodePath("../../ReturnToGameButton")
focus_neighbor_bottom = NodePath("../SFXSlider")
focus_next = NodePath("../SFXSlider")
focus_previous = NodePath("../../ReturnToGameButton")
max_value = 1.0
step = 0.05

[node name="SFXLabel" type="Label" parent="MarginContainer/ButtonsVBox/GridContainer"]
layout_mode = 2
text = "SFX"

[node name="SFXSlider" type="HSlider" parent="MarginContainer/ButtonsVBox/GridContainer"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
focus_neighbor_top = NodePath("../MusicSlider")
focus_neighbor_bottom = NodePath("../../MainMenuButton")
focus_next = NodePath("../../MainMenuButton")
focus_previous = NodePath("../MusicSlider")
max_value = 1.0
step = 0.05
value = 1.0

[node name="MainMenuButton" type="Button" parent="MarginContainer/ButtonsVBox"]
layout_mode = 2
focus_neighbor_top = NodePath("../GridContainer/SFXSlider")
focus_neighbor_bottom = NodePath("../QuitButton")
focus_next = NodePath("../QuitButton")
focus_previous = NodePath("../GridContainer/SFXSlider")
text = "Return to menu"
alignment = 0

[node name="QuitButton" type="Button" parent="MarginContainer/ButtonsVBox"]
layout_mode = 2
focus_neighbor_top = NodePath("../MainMenuButton")
focus_neighbor_bottom = NodePath("../ReturnToGameButton")
focus_next = NodePath("../ReturnToGameButton")
focus_previous = NodePath("../MainMenuButton")
text = "Quit Game"
alignment = 0

[connection signal="visibility_changed" from="." to="." method="_on_visibility_changed"]
[connection signal="pressed" from="MarginContainer/ButtonsVBox/ReturnToGameButton" to="." method="_on_return_to_game_button_pressed"]
[connection signal="value_changed" from="MarginContainer/ButtonsVBox/GridContainer/MusicSlider" to="." method="_on_music_slider_value_changed"]
[connection signal="value_changed" from="MarginContainer/ButtonsVBox/GridContainer/SFXSlider" to="." method="_on_sfx_slider_value_changed"]
[connection signal="pressed" from="MarginContainer/ButtonsVBox/MainMenuButton" to="." method="_on_main_menu_button_pressed"]
[connection signal="pressed" from="MarginContainer/ButtonsVBox/QuitButton" to="." method="_on_quit_button_pressed"]
28 changes: 28 additions & 0 deletions scenes/ui/menus/main_menu.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extends Control

signal start_game()
@onready var buttons_v_box = %ButtonsVBox

func _ready() -> void:
focus_button()


func _on_start_game_button_pressed() -> void:
start_game.emit()
hide()


func _on_quit_button_pressed() -> void:
get_tree().quit()


func focus_button() -> void:
if buttons_v_box:
var button: Button = buttons_v_box.get_child(0)
if button is Button:
button.grab_focus()


func _on_visibility_changed():
if visible:
focus_button()
Loading

0 comments on commit 8dcdf86

Please sign in to comment.