DuckRun/code/global.gd

65 lines
1.3 KiB
GDScript3
Raw Permalink Normal View History

extends Node
signal levelup
signal start
var maxspeed = 260
var speed = 100
var score = 0
var active_biom
var gamerunning = false
var hiscore:int
var coins:int
func _ready() -> void:
var save = load_save()
hiscore = save["hiscore"]
coins = save["hiscore"]
func reset_game():
speed = 100
score = 0
active_biom = "n"
gamerunning = false
_ready()
get_tree().reload_current_scene()
var data : FileAccess
func load_save() -> Dictionary:
# wenn kein save vorhanden ist, erstelle eins
if (not FileAccess.file_exists("user://save.json")) or (FileAccess.open("user://save.json", FileAccess.READ_WRITE).get_as_text() == ""):
init_save()
data = FileAccess.open("user://save.json", FileAccess.READ)
var content_text: Dictionary = JSON.parse_string(data.get_as_text())
if content_text:
print("loaded: ",content_text)
return content_text
else:
print("Failed to parse JSON")
return {}
func write_save():
var sdata : FileAccess
sdata = FileAccess.open("user://save.json",FileAccess.WRITE)
var content = JSON.stringify(
{
"hiscore":$/root/Game/Gui/ScoreContainer.get_score()
}
)
print("saved: ",content)
sdata.store_string(content)
2024-10-22 19:52:35 +02:00
func init_save():
data = FileAccess.open("user://save.json", FileAccess.WRITE)
data.store_string(FileAccess.open("res://code/first_save.json", FileAccess.READ).get_as_text())
data.close()