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
|
2024-10-21 21:22:40 +02:00
|
|
|
var active_biom
|
2024-10-09 21:02:14 +02:00
|
|
|
var gamerunning = false
|
|
|
|
|
2024-10-18 16:34:51 +02:00
|
|
|
|
2024-11-10 19:51:00 +01:00
|
|
|
var hiscore:int
|
|
|
|
var coins:int
|
2024-10-09 21:02:14 +02:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2024-11-10 19:51:00 +01:00
|
|
|
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
|
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() == ""):
|
2024-11-10 19:51:00 +01:00
|
|
|
init_save()
|
|
|
|
|
2024-10-18 16:34:51 +02:00
|
|
|
data = FileAccess.open("user://save.json", FileAccess.READ)
|
2024-11-10 19:51:00 +01:00
|
|
|
var content_text: Dictionary = JSON.parse_string(data.get_as_text())
|
2024-10-18 16:34:51 +02:00
|
|
|
|
|
|
|
if content_text:
|
|
|
|
print("loaded: ",content_text)
|
2024-11-10 19:51:00 +01:00
|
|
|
return content_text
|
2024-10-18 16:34:51 +02:00
|
|
|
else:
|
|
|
|
print("Failed to parse JSON")
|
2024-11-10 19:51:00 +01:00
|
|
|
return {}
|
2024-10-18 16:34:51 +02:00
|
|
|
|
2024-10-09 21:02:14 +02:00
|
|
|
|
|
|
|
func write_save():
|
2024-11-10 19:51:00 +01:00
|
|
|
var sdata : FileAccess
|
|
|
|
sdata = 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)
|
2024-11-10 19:51:00 +01:00
|
|
|
sdata.store_string(content)
|
2024-10-22 19:52:35 +02:00
|
|
|
|
2024-11-10 19:51:00 +01: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()
|