Made map loading work

This commit is contained in:
2025-08-03 22:40:05 -05:00
parent e0dd14c460
commit 53ad2da596
11 changed files with 88 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
[gd_scene load_steps=13 format=3 uid="uid://b16ysbx7ah03u"] [gd_scene load_steps=13 format=3 uid="uid://b16ysbx7ah03u"]
[ext_resource type="PackedScene" uid="uid://bicg5j33d72ld" path="res://maps/MapTundra.tscn" id="1_2gju5"] [ext_resource type="PackedScene" uid="uid://bicg5j33d72ld" path="res://maps/MapTundra/BaseMapTundra.tscn" id="1_2gju5"]
[ext_resource type="PackedScene" uid="uid://2ch34sio36nv" path="res://entities/Player.tscn" id="2_rlkm5"] [ext_resource type="PackedScene" uid="uid://2ch34sio36nv" path="res://entities/Player.tscn" id="2_rlkm5"]
[ext_resource type="Script" uid="uid://csb0i132lcu0w" path="res://entities/MapCamera.gd" id="3_n77mx"] [ext_resource type="Script" uid="uid://csb0i132lcu0w" path="res://entities/MapCamera.gd" id="3_n77mx"]
[ext_resource type="PackedScene" uid="uid://bng2mc7fu5aik" path="res://entities/NPC.tscn" id="4_nb1wl"] [ext_resource type="PackedScene" uid="uid://bng2mc7fu5aik" path="res://entities/NPC.tscn" id="4_nb1wl"]

View File

@@ -1,17 +1,18 @@
class_name OverworldScene extends Node class_name OverworldScene extends Node
@export var map:Node3D = null
func _enter_tree() -> void: func _enter_tree() -> void:
OVERWORLD.mapChanged.connect(onMapChanged) OVERWORLD.mapChanged.connect(onMapChanged)
func _ready() -> void: func _ready() -> void:
onMapChanged(OVERWORLD.newMap, OVERWORLD.playerDestinationNodeName) pass
func _exit_tree() -> void: func _exit_tree() -> void:
OVERWORLD.mapChanged.disconnect(onMapChanged) OVERWORLD.mapChanged.disconnect(onMapChanged)
func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void: func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void:
print("New map time.") print("New map time.", newMap)
var map = $Map
for childScene in map.get_children(): for childScene in map.get_children():
map.remove_child(childScene) map.remove_child(childScene)
@@ -22,6 +23,7 @@ func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void:
map.add_child(newMapInstance) map.add_child(newMapInstance)
# Find Player. # Find Player.
if playerDestinationNodeName:
var player = newMapInstance.get_node("Player") var player = newMapInstance.get_node("Player")
var destNode = newMapInstance.get_node(playerDestinationNodeName) var destNode = newMapInstance.get_node(playerDestinationNodeName)
if player && player is Player && destNode: if player && player is Player && destNode:
@@ -29,5 +31,4 @@ func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void:
player.global_rotation.y = destNode.global_rotation.y player.global_rotation.y = destNode.global_rotation.y
elif playerDestinationNodeName: elif playerDestinationNodeName:
push_error("Player, or destination node not found in new map.") push_error("Player, or destination node not found in new map.")
pass pass

View File

@@ -2,7 +2,8 @@
[ext_resource type="Script" uid="uid://dpvccegdmn7s6" path="res://meta/OverworldScene.gd" id="1_fa54r"] [ext_resource type="Script" uid="uid://dpvccegdmn7s6" path="res://meta/OverworldScene.gd" id="1_fa54r"]
[node name="OverworldScene" type="Node3D"] [node name="OverworldScene" type="Node3D" node_paths=PackedStringArray("map")]
script = ExtResource("1_fa54r") script = ExtResource("1_fa54r")
map = NodePath("Map")
[node name="Map" type="Node3D" parent="."] [node name="Map" type="Node3D" parent="."]

View File

@@ -10,8 +10,6 @@ overworld = NodePath("OverworldScene")
initial = NodePath("InitialScene") initial = NodePath("InitialScene")
metadata/_custom_type_script = "uid://ml70iui7qpo4" metadata/_custom_type_script = "uid://ml70iui7qpo4"
[node name="InitialScene" parent="." instance=ExtResource("2_hkmoa")]
visible = false
[node name="OverworldScene" parent="." instance=ExtResource("2_o1wvd")] [node name="OverworldScene" parent="." instance=ExtResource("2_o1wvd")]
visible = false
[node name="InitialScene" parent="." instance=ExtResource("2_hkmoa")]

View File

@@ -1,34 +1,85 @@
class_name OverworldSingleton extends Node class_name OverworldSingleton extends Node
signal mapChanged(newMap:PackedScene, playerDestinationNodeName:String) signal mapChanged(map:PackedScene, playerDestinationNodeName:String)
var newMap:PackedScene
var newMapPath:String
var hasFadedOut:bool = false
var playerDestinationNodeName:String var playerDestinationNodeName:String
var newMapLoaded:bool = false
func _init() -> void: func isMapChanging() -> bool:
return newMapPath != ""
func _enter_tree() -> void:
pass pass
func _exit_tree() -> void: func _exit_tree() -> void:
TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd) TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd)
TRANSITION.fadeInEnd.disconnect(onFadeInEnd) TRANSITION.fadeInEnd.disconnect(onFadeInEnd)
func mapChange(map:PackedScene, playerDestinationNodeName:String) -> void: func _process(delta:float) -> void:
if(!isMapChanging()):
return
var status = ResourceLoader.load_threaded_get_status(newMapPath)
if status == ResourceLoader.THREAD_LOAD_FAILED:
push_error("Failed to load map: " + newMapPath)
newMapPath = ""
elif status == ResourceLoader.THREAD_LOAD_INVALID_RESOURCE:
push_error("Failed to load map: " + newMapPath)
newMapPath = ""
elif status == ResourceLoader.THREAD_LOAD_IN_PROGRESS:
print("Map loading in progress for: " + newMapPath)
elif status == ResourceLoader.THREAD_LOAD_LOADED:
newMapLoaded = true
newMapReady()
func mapChange(map:String, playerDestinationNodeName:String) -> void:
# Begin Loading new map
newMapPath = map
playerDestinationNodeName = playerDestinationNodeName
ResourceLoader.load_threaded_request(newMapPath)
# Begin fadeout
hasFadedOut = false
TRANSITION.fade(TransitionSingleton.FadeType.FADE_OUT) TRANSITION.fade(TransitionSingleton.FadeType.FADE_OUT)
TRANSITION.fadeOutEnd.connect(onFadeOutEnd) TRANSITION.fadeOutEnd.connect(onFadeOutEnd)
newMap = map
self.playerDestinationNodeName = playerDestinationNodeName
func onFadeOutEnd() -> void: func onFadeOutEnd() -> void:
# Fade out has occurred, are we still waiting for the map to load?
TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd) TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd)
hasFadedOut = true
# Make sure we aren't still waiting for the map to load.
if !newMapLoaded:
return
newMapReady()
func newMapReady():
if !hasFadedOut:
return
# Instantiate the new map
var mapResource = ResourceLoader.load_threaded_get(newMapPath)
if mapResource == null:
push_error("Failed to load map resource: " + newMapPath)
return
# Stop tracking new map name
newMapPath = ""
# Emit event
mapChanged.emit(mapResource, playerDestinationNodeName)
# Begin Fade In
TRANSITION.fade(TransitionSingleton.FadeType.FADE_IN) TRANSITION.fade(TransitionSingleton.FadeType.FADE_IN)
TRANSITION.fadeInEnd.connect(onFadeInEnd) TRANSITION.fadeInEnd.connect(onFadeInEnd)
mapChanged.emit(newMap, playerDestinationNodeName)
func onFadeInEnd() -> void: func onFadeInEnd() -> void:
TRANSITION.fadeInEnd.disconnect(onFadeInEnd) TRANSITION.fadeInEnd.disconnect(onFadeInEnd)
newMap = null
func isMapChanging() -> bool:
# If the newMap is set, then we are in the process of changing maps.
return newMap != null

View File

@@ -3,6 +3,7 @@ class_name MainMenu extends Control
@export var btnNewGame:Button @export var btnNewGame:Button
@export var btnSettings:Button @export var btnSettings:Button
@export var settingsMenu:ClosableMenu @export var settingsMenu:ClosableMenu
@export_file("*.tscn") var newGameScene:String
func _ready() -> void: func _ready() -> void:
btnNewGame.pressed.connect(onNewGamePressed) btnNewGame.pressed.connect(onNewGamePressed)
@@ -10,6 +11,7 @@ func _ready() -> void:
func onNewGamePressed() -> void: func onNewGamePressed() -> void:
SCENE.setScene(SceneSingleton.SceneType.OVERWORLD) SCENE.setScene(SceneSingleton.SceneType.OVERWORLD)
OVERWORLD.mapChange(newGameScene, "PlayerSpawnPoint")
func onSettingsPressed() -> void: func onSettingsPressed() -> void:
print("Settings button pressed") print("Settings button pressed")

View File

@@ -15,6 +15,7 @@ script = ExtResource("1_vp3lc")
btnNewGame = NodePath("VBoxContainer/NewGame") btnNewGame = NodePath("VBoxContainer/NewGame")
btnSettings = NodePath("VBoxContainer/Settings") btnSettings = NodePath("VBoxContainer/Settings")
settingsMenu = NodePath("MainMenuSettings") settingsMenu = NodePath("MainMenuSettings")
newGameScene = "uid://b16ysbx7ah03u"
metadata/_custom_type_script = "uid://btfeuku41py2b" metadata/_custom_type_script = "uid://btfeuku41py2b"
[node name="VBoxContainer" type="VBoxContainer" parent="."] [node name="VBoxContainer" type="VBoxContainer" parent="."]