Commit 72ae3cda authored by Nathan Lovato's avatar Nathan Lovato
Browse files

Add arcade-style high score table, including save and load from disk

parent eb690c9e
Pipeline #5064 passed with stage
in 4 minutes and 16 seconds
......@@ -2,14 +2,19 @@ extends Control
onready var scores_column = $Column/ScoresColumn
const score_row = preload("res://ScoreBoard/ScoreRow.tscn")
const ScoreRow = preload("ScoreRow.tscn")
const ScoreLoader = preload("ScoreLoader.gd")
func start():
var loader := ScoreLoader.new()
var scores = loader.get_scores()
for i in range(10):
var score_instance = score_row.instance()
scores_column.add_child(score_instance)
score_instance.setup(i)
score_instance.start()
var row = ScoreRow.instance()
scores_column.add_child(row)
row.setup(i, scores[i])
for row in scores_column.get_children():
row.start()
yield(get_tree().create_timer(0.07), "timeout")
func _ready() -> void:
......
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://ScoreBoard/ScoreBoard.gd" type="Script" id=1]
[ext_resource path="res://assets/fonts/ScoreTitle.tres" type="DynamicFont" id=2]
[ext_resource path="res://ScoreBoard/ScoreRow.tscn" type="PackedScene" id=3]
[node name="ScoreBoard" type="Control"]
anchor_right = 1.0
......@@ -36,11 +35,3 @@ margin_right = 840.0
margin_bottom = 1180.0
size_flags_vertical = 3
[node name="ScoreRow" parent="Column/ScoresColumn" instance=ExtResource( 3 )]
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_top = 0.0
margin_right = 840.0
margin_bottom = 88.0
"""
Saves and loads scores to/from the disk
Each score is a dictionary of the form
{ initials:String, score:int }
"""
const SCORES_PATH := "user://scores"
const SCORE_SECTION := "scores"
const DEFAULT_SCORE := {
initials = "RMS",
score = 1,
}
func score_file_exists() -> bool:
return Directory.new().file_exists(SCORES_PATH)
func _get_default_scores() -> Array:
var scores := []
for i in range(10):
scores.append(DEFAULT_SCORE)
return scores
func save(scores:Array=[]) -> void:
if not scores.size() in [0, 10]:
return
if scores == []:
scores = _get_default_scores()
var file := ConfigFile.new()
for i in range(scores.size()):
file.set_value(SCORE_SECTION, str(i), scores[i])
file.save(SCORES_PATH)
func get_scores() -> Array:
"""
Loads scores from the disk and returns them as a list
"""
if not score_file_exists():
return _get_default_scores()
var file := ConfigFile.new()
if file.load(SCORES_PATH) != OK:
return []
var scores := []
for i in range(10):
var score :Dictionary = file.get_value(SCORE_SECTION, str(i), DEFAULT_SCORE)
scores.append(score)
return scores
......@@ -9,10 +9,10 @@ const COLOR_TRANSPARENT = Color('00ffffff')
const COLOR_OPAQUE = Color('00ffffff')
func setup(index:int, score:int=1, initials:String="rms") -> void:
func setup(index:int, score:Dictionary={}) -> void:
rank_label.set_rank(index)
score_label.text = "%03d" % score
initials_label.text = initials
score_label.text = "%03d" % score.score
initials_label.text = score.initials
func start() -> void:
......
......@@ -23,7 +23,7 @@ func set_rank(index:int) -> void:
[sub_resource type="Animation" id=2]
resource_name = "fade_in"
length = 0.4
length = 0.3
tracks/0/type = "value"
tracks/0/path = NodePath(".:modulate")
tracks/0/interp = 1
......@@ -31,7 +31,7 @@ tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/keys = {
"times": PoolRealArray( 0, 0.4 ),
"times": PoolRealArray( 0, 0.3 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 0,
"values": [ Color( 1, 1, 1, 0 ), Color( 1, 1, 1, 1 ) ]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment