Skip to content

Commit

Permalink
Add request scene
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 committed Aug 19, 2024
1 parent 50c6aa2 commit 1d507a7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Levels/level_01/Level_01.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
[ext_resource type="PackedScene" uid="uid://csbw7e8w63gif" path="res://Characters/hero.tscn" id="6_1veeq"]
[ext_resource type="PackedScene" uid="uid://b1p77kho11wc" path="res://Objects/Props/Barrels/Obj_Barrel_01.tscn" id="9_f7gfs"]
[ext_resource type="PackedScene" uid="uid://d2glbpj75vbl8" path="res://Characters/EnemyGreenOoze.tscn" id="11_opamk"]
[ext_resource type="PackedScene" path="res://UI/UI.tscn" id="12_gvknc"]
[ext_resource type="PackedScene" uid="uid://c4f4xoibdq0s3" path="res://UI/UI.tscn" id="12_gvknc"]
[ext_resource type="AudioStream" uid="uid://htkcydmx1tfa" path="res://Audio/soundtrack/Burn The World Waltz .mp3" id="13_kw5mq"]
[ext_resource type="PackedScene" uid="uid://c4bb5dcwvv8so" path="res://Objects/Props/Book/Obj_Book_01.tscn" id="17_68np3"]
[ext_resource type="PackedScene" path="res://Characters/Rat/EnemyRat.tscn" id="17_lavig"]
[ext_resource type="PackedScene" uid="uid://bgogck1k1rl0i" path="res://Characters/Rat/EnemyRat.tscn" id="17_lavig"]
[ext_resource type="PackedScene" uid="uid://d286oxenjlily" path="res://Objects/Props/Pencil/Obj_Pencil_01.tscn" id="18_tdhot"]

[sub_resource type="AudioStreamPlaylist" id="AudioStreamPlaylist_4xpmh"]
Expand All @@ -20,7 +20,7 @@ stream_0 = ExtResource("13_kw5mq")
y_sort_enabled = true
script = ExtResource("1_6x71g")
size_target = 800
level_complete = "res://Menus/Level_Transistion_Scene.tscn"
level_complete = "res://Menus/Level_Transition_Scene.tscn"

[node name="Walls and Ground" type="TileMapLayer" parent="."]
use_parent_material = true
Expand Down
5 changes: 2 additions & 3 deletions Levels/level_01/level_01.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ func _ready() -> void:
func check_level_finished(hero_size: int):
if hero_size >= size_target:
print("YOU'RE WINNER")
# TODO: preload scenes
GlobalSignals.request_scene.emit(level_complete)
get_tree().create_timer(1).timeout.connect(func (): get_tree().change_scene_to_file("res://Menus/Level_Transition_Scene.tscn"))
get_tree().create_timer(1).timeout.connect(func(): GlobalSignals.request_scene.emit(level_complete))


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
Expand Down
2 changes: 1 addition & 1 deletion Menus/Level_Transition_Scene.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=3 uid="uid://dsjdtyli4m3o8"]
[gd_scene load_steps=9 format=3 uid="uid://c1ruh03xank16"]

[ext_resource type="Theme" uid="uid://bewgrgj1ylev2" path="res://Menus/MenuTheme.tres" id="1_s701w"]
[ext_resource type="Script" path="res://Menus/level_transition_scene.gd" id="2_hq2x5"]
Expand Down
81 changes: 78 additions & 3 deletions Scripts/GameManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const pause_menu := "res://Menus/PauseMenu.tscn"
var pause := preload(pause_menu).instantiate()

signal in_menu(value : bool)

var is_menu := true

var scene_path : String

# ---
# Pause Menu
# ---

func hide_menu():
if pause.is_inside_tree():
get_tree().root.remove_child(pause)
Expand All @@ -22,16 +27,86 @@ func toggle_menu():
get_tree().root.add_child(pause)
get_tree().paused = true

# ---
# Scene Change
# ---

func check_scene_path() -> String:
if scene_path == null or scene_path.is_empty():
return "scene path is empty"
if not scene_path.ends_with(".tscn") or not scene_path.begins_with("res://"):
return "scene path incomplete"
if not ResourceLoader.exists(scene_path):
return "scene path does not exsist"

return ""

func valid_scene_path() -> bool:
var scene_status = check_scene_path()
if scene_status:
push_warning(scene_status, ": ", scene_path)
print_rich("[color=yellow]%s: [b]'%s'[/b][/color]" % [scene_status, scene_path])
return false

return true

func do_scene_change():
var resource = ResourceLoader.load_threaded_get(scene_path)
var err = get_tree().change_scene_to_packed(resource)
if err:
push_error("failed to change scenes: %d" % err)
get_tree().quit(1)
scene_path = ""
GlobalSignals.change_scene.emit()

func load_scene() -> void:
if ResourceLoader.has_cached(scene_path):
do_scene_change()
return
ResourceLoader.load_threaded_request(scene_path)
set_process(true)

func change_scene(scene: String):
scene_path = scene
prints("Changing scene to", scene_path)
if not valid_scene_path():
return
load_scene()

func get_scene_status() -> ResourceLoader.ThreadLoadStatus:
if not valid_scene_path():
return ResourceLoader.THREAD_LOAD_INVALID_RESOURCE
return ResourceLoader.load_threaded_get_status(scene_path)

func check_scene_status():
var resource_status = get_scene_status()
match(resource_status):
ResourceLoader.THREAD_LOAD_INVALID_RESOURCE, ResourceLoader.THREAD_LOAD_FAILED:
push_error("failed to change scenes")
set_process(false)
ResourceLoader.THREAD_LOAD_LOADED:
do_scene_change()
set_process(false)

# ---
# Setup
# ---

func _ready():
process_mode = Node.PROCESS_MODE_ALWAYS
GlobalSignals.change_scene.connect(hide_menu)
GlobalSignals.request_scene.connect(change_scene)
in_menu.connect(func(value): is_menu = value)
set_process(false)

func _input(event: InputEvent):
if(event.is_action_pressed("escape")):
print(get_tree().root.get_child(0))
#get_tree().root.get_node()
# TODO: check if menu using root
#print(get_tree().root.get_child(0))
if is_menu:
get_tree().change_scene_to_file(main_menu)
else:
toggle_menu()

func _process(_delta):
check_scene_status()
47 changes: 5 additions & 42 deletions Scripts/Nodes/SceneButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extends Button

func set_scene(value : String) -> void:
scene_path = value
disabled = not valid_scene_path()
var scene_status = check_scene_path()
disabled = not scene_status.is_empty()
update_configuration_warnings()

func check_scene_path() -> String:
Expand All @@ -27,52 +28,14 @@ func _get_configuration_warnings() -> PackedStringArray:
return [scene_status]
return []

func valid_scene_path() -> bool:
var scene_status = check_scene_path()
if scene_status:
push_warning(self, " ", scene_status)
print_rich("[color=yellow]%s [b]'%s'[/b][/color]" % [text, scene_path])

return scene_status.is_empty()

func change_scene() -> void:
var resource = ResourceLoader.load_threaded_get(scene_path)
var err = get_tree().change_scene_to_packed(resource)
if err:
push_error("failed to change scenes: %d" % err)
get_tree().quit(1)
GameManager.in_menu.emit(to_menu)
GlobalSignals.change_scene.emit()

func load_scene() -> void:
prints("Changing scene to", scene_path)
if not valid_scene_path():
push_error("\"%s\" failed to change scenes: %s" % [text, scene_path])
return
if ResourceLoader.has_cached(scene_path):
change_scene()
return
ResourceLoader.load_threaded_request(scene_path)
set_process(true)
GlobalSignals.request_scene.emit(scene_path)

func _ready():
update_configuration_warnings()
disabled = not valid_scene_path()
var scene_status = check_scene_path()
disabled = not scene_status.is_empty()
if not disabled:
pressed.connect(load_scene)
set_process(false)

func get_status() -> ResourceLoader.ThreadLoadStatus:
if not valid_scene_path():
return ResourceLoader.THREAD_LOAD_INVALID_RESOURCE
return ResourceLoader.load_threaded_get_status(scene_path)

func _process(_delta):
var resource_status = get_status()
match(resource_status):
ResourceLoader.THREAD_LOAD_INVALID_RESOURCE, ResourceLoader.THREAD_LOAD_FAILED:
push_error("failed to change scenes")
set_process(false)
ResourceLoader.THREAD_LOAD_LOADED:
change_scene()
set_process(false)

0 comments on commit 1d507a7

Please sign in to comment.