-
Notifications
You must be signed in to change notification settings - Fork 4
/
SceneManager.gd
85 lines (77 loc) · 2.45 KB
/
SceneManager.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extends Control
# Declare member variables here. Examples:
var startScene = preload("res://menu/Menu.tscn")
var currentScene
var loader
var wait_frames
var loadingNode
var loadingPreInst = preload("res://loader/Loading.tscn")
var next_resource
var time_max = 100 # msec
# Called when the node enters the scene tree for the first time.
func _ready():
$Other.modulate.a = 0
$Main.modulate.a = 1
# Load up the first scene
currentScene = startScene.instance()
$Main/Viewport.add_child(currentScene)
func pushScene(scene):
get_tree().get_root().set_disable_input(true)
$Main/Viewport.remove_child(currentScene)
$Other/Viewport.add_child(currentScene)
$Other.modulate.a = 1
$Main.modulate.a = 0
$Main/Viewport.add_child(scene)
currentScene = scene
$AnimationPlayer.play("nextScene")
func popScene(scene):
get_tree().get_root().set_disable_input(true)
$Other.modulate.a = 0
$Main.modulate.a = 1
$Other/Viewport.add_child(scene)
currentScene = scene
$AnimationPlayer.play("previousScene")
func _on_AnimationPlayer_animation_finished(anim_name):
if (anim_name == "previousScene"):
for child in $Main/Viewport.get_children():
child.queue_free()
$Other/Viewport.remove_child(currentScene)
$Main/Viewport.add_child(currentScene)
$Main.modulate.a = 1
$Other.modulate.a = 0
get_tree().get_root().set_disable_input(false)
func openWithLoading(path):
loader = ResourceLoader.load_interactive(path)
if loader == null: # check for errors
return
loadingNode=loadingPreInst.instance()
currentScene.add_child(loadingNode)
wait_frames = 0
func _process(delta):
if (loader):
if wait_frames > 0: # wait for frames to let the "loading" animation to show up
wait_frames -= 1
return
var t = OS.get_ticks_msec()
while OS.get_ticks_msec() < t + time_max: # use "time_max" to control how much time we block this thread
# poll your loader
var err = loader.poll()
if err == ERR_FILE_EOF: # load finished
loadingNode.setProgress(100)
next_resource = loader.get_resource()
loader = null
wait_frames=1
break
elif err==OK:
if (loadingNode):
var progress = float(loader.get_stage()) / loader.get_stage_count()
loadingNode.setProgress(round(progress*100))
else: # error during loading
loader = null
break
if (loader==null and next_resource!=null):
if wait_frames > 0: # wait for frames to let the "loading" animation to show up
wait_frames -= 1
return
pushScene(next_resource.instance())
next_resource=null