-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.gd
60 lines (42 loc) · 1.42 KB
/
Global.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
extends Node
var current_scene = null
var save_file: String = "res://save/savegame.save"
# Called when the node enters the scene tree for the first time.
func _ready():
OS.set_window_size(Vector2(1024, 768))
var root = get_tree().root
current_scene = root.get_child(root.get_child_count() - 1)
func goto_scene(path: String):
#make the scene transition deferred to avoid walking over
# other code execution
call_deferred("_deferred_goto_scene", path)
func _deferred_goto_scene(path: String):
#remove the current scene
current_scene.free()
#Load the new scene
var new_scene = ResourceLoader.load(path)
#Create an instance of it
current_scene = new_scene.instance()
#add it to the tree
get_tree().root.add_child(current_scene)
get_tree().current_scene = current_scene
func change_song(song: AudioStream):
# if this song is a new song
if $MusicPlayer.get_stream() != song:
$MusicPlayer.set_stream(song)
$MusicPlayer.play()
func save_game():
var save_game = File.new()
save_game.open(save_file, File.WRITE)
var save_data = PlayerStats.save_game()
save_game.store_line(to_json(save_data))
save_game.close()
func load_game() -> bool:
var save_game = File.new()
if not save_game.file_exists(save_file):
return false
save_game.open(save_file, File.READ)
#while save_game.get_position() < save_game.get_len():
var save_data = parse_json(save_game.get_line())
PlayerStats.load_game(save_data)
return true