diff --git a/project.godot b/project.godot index 24173eb..e1780f2 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,7 @@ config/icon="res://icon.svg" Gamestate="*res://scripts/gamestate.gd" ConfigFileHandler="*res://scripts/configFileHandler.gd" +HighscoreHandler="*res://scripts/highscoreHandler.gd" [display] diff --git a/scenes/game.tscn b/scenes/game.tscn index 53dab19..0509133 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -260,15 +260,39 @@ text = "0.000" vertical_alignment = 1 [node name="countdown_label" type="Label" parent="hud"] -offset_left = 604.0 -offset_top = 312.0 -offset_right = 644.0 -offset_bottom = 342.0 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -20.0 +offset_top = -34.5 +offset_right = 20.0 +offset_bottom = 34.5 +grow_horizontal = 2 +grow_vertical = 2 theme_override_font_sizes/font_size = 50 text = "3" horizontal_alignment = 1 vertical_alignment = 1 +[node name="highscore_label" type="Label" parent="hud"] +visible = false +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -57.5 +offset_top = 2.70999 +offset_right = 57.5 +offset_bottom = 71.71 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_font_sizes/font_size = 50 +horizontal_alignment = 1 +vertical_alignment = 1 + [node name="overlaycolor" type="ColorRect" parent="hud"] self_modulate = Color(1, 1, 1, 0) offset_left = -25.0 diff --git a/scripts/game.gd b/scripts/game.gd index d85bb93..ebe59c3 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -11,6 +11,8 @@ extends Node2D @onready var times_container: HFlowContainer = $hud/times_container @onready var timer_close: Timer = $timer_close +@onready var highscore_label: Label = $hud/highscore_label + const caroffset= 32+4 #space cars on start line const caroffset_side= 16 @@ -35,6 +37,7 @@ var game_ended=false # Called when the node enters the scene tree for the first time. func _ready() -> void: + Gamestate.resetTimeElapsed() #countdown.start() @@ -83,6 +86,8 @@ func _ready() -> void: print("i_position="+str(i_position)+" carpos is ="+str(newcarinstance.getPosition())+" instancepos="+str(newcarinstance.position)+" colori="+str(player.colori)+" color="+str(player.color)) i+=1 + print("mapname="+str(Gamestate.getSelectedMapName()+"_"+str(Gamestate.getRounds()))) + func custom_array_sort_rank(a, b): return a.rank > b.rank #a>b = higher rank (more wins) -> better start position @@ -110,11 +115,14 @@ func _process(delta: float) -> void: if Input.is_action_pressed(Gamestate.userinput_prefix+str(id)): anyplayerkeypressed=true id+=1 + + if anyplayerkeypressed and timer_close.time_left void: print("Close Map") diff --git a/scripts/gamestate.gd b/scripts/gamestate.gd index a045955..42f56b0 100644 --- a/scripts/gamestate.gd +++ b/scripts/gamestate.gd @@ -10,7 +10,8 @@ var userinput_prefix="inputP" var time_elapsed=0 var map_scene_name="" -var map_scene_name_prefix="res://scenes/map_" +var map_scene_path_prefix="res://scenes/" +var map_scene_name_prefix="map_" var map_scene_name_postfix=".tscn" var rounds=3 @@ -26,9 +27,12 @@ func setRounds(r): func getSelectedMap(): #return only short map name "00" return map_scene_name + +func getSelectedMapName(): + return map_scene_name_prefix+map_scene_name func getSelectedMapSceneName(): #returns full scene path "res://scenes/map_00.tscn" - return map_scene_name_prefix+map_scene_name+map_scene_name_postfix + return map_scene_path_prefix+map_scene_name_prefix+map_scene_name+map_scene_name_postfix func setSelectedMap(mapname): map_scene_name=mapname diff --git a/scripts/highscoreHandler.gd b/scripts/highscoreHandler.gd new file mode 100644 index 0000000..d448d9c --- /dev/null +++ b/scripts/highscoreHandler.gd @@ -0,0 +1,41 @@ +extends Node + +const HIGHSCORE_FILE_PATH = "highscores.json" + + + +func updateHighscore(mapname:String,rounds:int,preset:int,time:float): + var highscores=loadHighscoreDict() + if highscores.has(mapname): + var lasthighscore=loadHighscore(mapname) + if time0: + return highscores[mapname][highscores[mapname].size()-1]["time"] #return last entry + else: + return null + + + +func loadHighscoreDict(): + var file = FileAccess.open(HIGHSCORE_FILE_PATH, FileAccess.READ) + if file == null: + return {} + var content = file.get_as_text() + return JSON.parse_string(content) + +func storeHighscoreDict(content:Dictionary): + print("Saved Highscorefile:"+str(content)) + var file = FileAccess.open(HIGHSCORE_FILE_PATH, FileAccess.WRITE) + file.store_string(JSON.stringify(content, "\t"))