forked from uzkbwza/smileygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_view.gd
108 lines (74 loc) · 2.66 KB
/
game_view.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
extends CanvasLayer
class_name GameView
signal scene_instantiated(scene: GameScene)
@onready var game_sub_viewport: SubViewport = %GameSubViewport
var instantiation_thread: Thread
var scenes = {
"MainMenu": "res://screens/MainMenu.tscn",
"Game": "res://screens/game.tscn"
}
enum ScreenTransitionType {
None,
Fade,
}
func _ready() -> void:
pass
func load_scene(scene_path: StringName, scene_data: Dictionary={}) -> GameScene:
if Debug.enabled:
Debug.clear_items()
var progress = [0]
var scene: PackedScene = await Utils.load_resource_threaded(scene_path, func() -> Signal: return get_tree().process_frame, update_progress)
if game_sub_viewport.get_child_count() > 0:
game_sub_viewport.get_child(0).free()
instantiate_scene_threaded(scene)
var child: GameScene = await scene_instantiated
instantiation_thread.wait_to_finish()
child.scene_change_triggered.connect(on_scene_change_triggered, CONNECT_DEFERRED)
child.load_scene_data(scene_data)
game_sub_viewport.add_child.call_deferred(child)
var current_screen = child
return child
func instantiate_scene_threaded(scene: PackedScene):
if instantiation_thread and instantiation_thread.is_alive():
instantiation_thread.wait_to_finish()
instantiation_thread = Thread.new()
instantiation_thread.start(func():
var instantiated = scene.instantiate()
scene_instantiated.emit.call_deferred(instantiated)
)
func finished_instantiating_scene(scene: GameScene):
scene_instantiated.emit(scene)
func update_progress(progress):
if progress:
var p = progress[0]
func _exit_tree() -> void:
if instantiation_thread and instantiation_thread.is_alive():
instantiation_thread.wait_to_finish()
func screen_transition_start(type: ScreenTransitionType):
match type:
ScreenTransitionType.None:
return
ScreenTransitionType.Fade:
%ScreenFadeRect.color.a = 0.0
var tween = create_tween()
tween.tween_property(%ScreenFadeRect, "color:a", 1.0, 0.25)
await tween.finished
return
func screen_transition_end(type: ScreenTransitionType):
match type:
ScreenTransitionType.None:
return
ScreenTransitionType.Fade:
%ScreenFadeRect.color.a = 1.0
var tween = create_tween()
tween.tween_property(%ScreenFadeRect, "color:a", 0.0, 0.25)
await tween.finished
return
func on_scene_change_triggered(new_scene_name: StringName, screen_transition:ScreenTransitionType, scene_data: Dictionary) -> void:
await screen_transition_start(screen_transition)
if new_scene_name == "Quit":
get_tree().quit()
return
var new_scene: GameScene = await load_scene(scenes[new_scene_name], scene_data)
await get_tree().physics_frame
await screen_transition_end(new_scene.get_screen_transition_in())