Made map loading work
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
[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="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"]
 | 
			
		||||
@@ -1,17 +1,18 @@
 | 
			
		||||
class_name OverworldScene extends Node
 | 
			
		||||
 | 
			
		||||
@export var map:Node3D = null
 | 
			
		||||
 | 
			
		||||
func _enter_tree() -> void:
 | 
			
		||||
	OVERWORLD.mapChanged.connect(onMapChanged)
 | 
			
		||||
 | 
			
		||||
func _ready() -> void:
 | 
			
		||||
	onMapChanged(OVERWORLD.newMap, OVERWORLD.playerDestinationNodeName)
 | 
			
		||||
	pass
 | 
			
		||||
 | 
			
		||||
func _exit_tree() -> void:
 | 
			
		||||
	OVERWORLD.mapChanged.disconnect(onMapChanged)
 | 
			
		||||
 | 
			
		||||
func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void:
 | 
			
		||||
	print("New map time.")
 | 
			
		||||
	var map = $Map
 | 
			
		||||
	print("New map time.", newMap)
 | 
			
		||||
	for childScene in map.get_children():
 | 
			
		||||
		map.remove_child(childScene)
 | 
			
		||||
 | 
			
		||||
@@ -22,12 +23,12 @@ func onMapChanged(newMap:PackedScene, playerDestinationNodeName:String) -> void:
 | 
			
		||||
	map.add_child(newMapInstance)
 | 
			
		||||
 | 
			
		||||
	# Find Player.
 | 
			
		||||
	var player = newMapInstance.get_node("Player")
 | 
			
		||||
	var destNode = newMapInstance.get_node(playerDestinationNodeName)
 | 
			
		||||
	if player && player is Player && destNode:
 | 
			
		||||
		player.global_position = destNode.global_position
 | 
			
		||||
		player.global_rotation.y = destNode.global_rotation.y
 | 
			
		||||
	elif playerDestinationNodeName:
 | 
			
		||||
		push_error("Player, or destination node not found in new map.")
 | 
			
		||||
 | 
			
		||||
	pass
 | 
			
		||||
	if playerDestinationNodeName:
 | 
			
		||||
		var player = newMapInstance.get_node("Player")
 | 
			
		||||
		var destNode = newMapInstance.get_node(playerDestinationNodeName)
 | 
			
		||||
		if player && player is Player && destNode:
 | 
			
		||||
			player.global_position = destNode.global_position
 | 
			
		||||
			player.global_rotation.y = destNode.global_rotation.y
 | 
			
		||||
		elif playerDestinationNodeName:
 | 
			
		||||
			push_error("Player, or destination node not found in new map.")
 | 
			
		||||
		pass
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,8 @@
 | 
			
		||||
 | 
			
		||||
[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")
 | 
			
		||||
map = NodePath("Map")
 | 
			
		||||
 | 
			
		||||
[node name="Map" type="Node3D" parent="."]
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,6 @@ overworld = NodePath("OverworldScene")
 | 
			
		||||
initial = NodePath("InitialScene")
 | 
			
		||||
metadata/_custom_type_script = "uid://ml70iui7qpo4"
 | 
			
		||||
 | 
			
		||||
[node name="InitialScene" parent="." instance=ExtResource("2_hkmoa")]
 | 
			
		||||
visible = false
 | 
			
		||||
 | 
			
		||||
[node name="OverworldScene" parent="." instance=ExtResource("2_o1wvd")]
 | 
			
		||||
visible = false
 | 
			
		||||
 | 
			
		||||
[node name="InitialScene" parent="." instance=ExtResource("2_hkmoa")]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,34 +1,85 @@
 | 
			
		||||
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 newMapLoaded:bool = false
 | 
			
		||||
 | 
			
		||||
func _init() -> void:
 | 
			
		||||
func isMapChanging() -> bool:
 | 
			
		||||
	return newMapPath != ""
 | 
			
		||||
 | 
			
		||||
func _enter_tree() -> void:
 | 
			
		||||
	pass
 | 
			
		||||
 | 
			
		||||
func _exit_tree() -> void:
 | 
			
		||||
	TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd)
 | 
			
		||||
	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.fadeOutEnd.connect(onFadeOutEnd)
 | 
			
		||||
	newMap = map
 | 
			
		||||
	self.playerDestinationNodeName = playerDestinationNodeName
 | 
			
		||||
 | 
			
		||||
func onFadeOutEnd() -> void:
 | 
			
		||||
	# Fade out has occurred, are we still waiting for the map to load?
 | 
			
		||||
	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.fadeInEnd.connect(onFadeInEnd)
 | 
			
		||||
	mapChanged.emit(newMap, playerDestinationNodeName)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func onFadeInEnd() -> void:
 | 
			
		||||
	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
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ class_name MainMenu extends Control
 | 
			
		||||
@export var btnNewGame:Button
 | 
			
		||||
@export var btnSettings:Button
 | 
			
		||||
@export var settingsMenu:ClosableMenu
 | 
			
		||||
@export_file("*.tscn") var newGameScene:String
 | 
			
		||||
 | 
			
		||||
func _ready() -> void:
 | 
			
		||||
	btnNewGame.pressed.connect(onNewGamePressed)
 | 
			
		||||
@@ -10,6 +11,7 @@ func _ready() -> void:
 | 
			
		||||
 | 
			
		||||
func onNewGamePressed() -> void:
 | 
			
		||||
	SCENE.setScene(SceneSingleton.SceneType.OVERWORLD)
 | 
			
		||||
	OVERWORLD.mapChange(newGameScene, "PlayerSpawnPoint")
 | 
			
		||||
 | 
			
		||||
func onSettingsPressed() -> void:
 | 
			
		||||
	print("Settings button pressed")
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ script = ExtResource("1_vp3lc")
 | 
			
		||||
btnNewGame = NodePath("VBoxContainer/NewGame")
 | 
			
		||||
btnSettings = NodePath("VBoxContainer/Settings")
 | 
			
		||||
settingsMenu = NodePath("MainMenuSettings")
 | 
			
		||||
newGameScene = "uid://b16ysbx7ah03u"
 | 
			
		||||
metadata/_custom_type_script = "uid://btfeuku41py2b"
 | 
			
		||||
 | 
			
		||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user