2024-10-09 21:02:14 +02:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
|
|
|
signal levelup
|
|
|
|
signal start
|
|
|
|
|
2024-10-18 16:34:51 +02:00
|
|
|
|
2024-10-09 21:02:14 +02:00
|
|
|
var maxspeed = 260
|
|
|
|
var speed = 100
|
|
|
|
var score = 0
|
|
|
|
var gamerunning = false
|
|
|
|
|
2024-10-18 16:34:51 +02:00
|
|
|
|
2024-10-09 21:02:14 +02:00
|
|
|
var hiscore
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
load_save()
|
|
|
|
#write_save()
|
|
|
|
func load_save():
|
|
|
|
var data : FileAccess
|
2024-10-18 16:34:51 +02:00
|
|
|
if (not FileAccess.file_exists("user://save.json")) or (FileAccess.open("user://save.json", FileAccess.READ_WRITE).get_as_text() == ""):
|
|
|
|
data = FileAccess.open("user://save.json", FileAccess.WRITE)
|
|
|
|
data.store_string(FileAccess.open("res://code/first_save.json", FileAccess.READ).get_as_text())
|
2024-10-09 21:02:14 +02:00
|
|
|
data.close()
|
2024-10-18 16:34:51 +02:00
|
|
|
data = FileAccess.open("user://save.json", FileAccess.READ)
|
2024-10-09 21:02:14 +02:00
|
|
|
|
2024-10-18 16:34:51 +02:00
|
|
|
var content_text = JSON.parse_string(data.get_as_text())
|
|
|
|
|
|
|
|
if content_text:
|
|
|
|
print("loaded: ",content_text)
|
|
|
|
hiscore = content_text.get("hiscore", 0)
|
|
|
|
else:
|
|
|
|
print("Failed to parse JSON")
|
|
|
|
data.close()
|
|
|
|
|
2024-10-09 21:02:14 +02:00
|
|
|
|
|
|
|
func write_save():
|
|
|
|
var data : FileAccess
|
|
|
|
data = FileAccess.open("user://save.json",FileAccess.WRITE)
|
2024-10-18 16:34:51 +02:00
|
|
|
var content = JSON.stringify(
|
2024-10-09 21:02:14 +02:00
|
|
|
{
|
2024-10-18 16:34:51 +02:00
|
|
|
"hiscore":$/root/Game/Gui/ScoreContainer.get_score()
|
2024-10-09 21:02:14 +02:00
|
|
|
}
|
|
|
|
)
|
2024-10-18 16:34:51 +02:00
|
|
|
print("saved: ",content)
|
|
|
|
data.store_string(content)
|