Commit fc385999 authored by Nathan Lovato's avatar Nathan Lovato

Refactor scoreboard and loader to register and sort high scores

parent 72ae3cda
......@@ -5,17 +5,19 @@ onready var scores_column = $Column/ScoresColumn
const ScoreRow = preload("ScoreRow.tscn")
const ScoreLoader = preload("ScoreLoader.gd")
func start():
var loader := ScoreLoader.new()
var scores = loader.get_scores()
var scores := []
func start(player_score:int):
var score_loader := ScoreLoader.new()
score_loader.register(player_score)
var scores = score_loader.get_scores()
for i in range(10):
var row = ScoreRow.instance()
scores_column.add_child(row)
row.setup(i, scores[i])
visible = true
for row in scores_column.get_children():
row.start()
yield(get_tree().create_timer(0.07), "timeout")
func _ready() -> void:
start()
......@@ -24,13 +24,8 @@ text = "High scores"
align = 1
uppercase = true
[node name="HSeparator" type="HSeparator" parent="Column"]
margin_top = 116.0
margin_right = 840.0
margin_bottom = 120.0
[node name="ScoresColumn" type="VBoxContainer" parent="Column"]
margin_top = 124.0
margin_top = 116.0
margin_right = 840.0
margin_bottom = 1180.0
size_flags_vertical = 3
......
......@@ -12,42 +12,59 @@ const DEFAULT_SCORE := {
score = 1,
}
var _scores := [] setget , get_scores
func score_file_exists() -> bool:
return Directory.new().file_exists(SCORES_PATH)
func register(score:int):
if _scores == []:
self.load()
var sorted_scores :Array = _scores
sorted_scores.append({initials='YOU', score=score})
sorted_scores.sort_custom(self, 'sort_scores')
sorted_scores.pop_back()
_scores = sorted_scores
func _get_default_scores() -> Array:
var scores := []
for i in range(10):
scores.append(DEFAULT_SCORE)
return scores
static func sort_scores(a:Dictionary, b:Dictionary) -> bool:
return a['score'] >= b['score']
func save(scores:Array=[]) -> void:
if not scores.size() in [0, 10]:
if scores.size() != 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:
func load() -> void:
"""
Loads scores from the disk and returns them as a list
Loads scores from the disk and stores them in the _scores variable
"""
if not score_file_exists():
return _get_default_scores()
if not _score_file_exists():
_scores = _get_default_scores()
return
var file := ConfigFile.new()
if file.load(SCORES_PATH) != OK:
return []
var scores := []
return
_scores = []
for i in range(10):
var score :Dictionary = file.get_value(SCORE_SECTION, str(i), DEFAULT_SCORE)
scores.append(score)
_scores.append(score)
func get_scores() -> Array:
return _scores if _scores != [] else _get_default_scores()
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
......@@ -3,13 +3,14 @@ extends Label
onready var tween : Tween = $Tween
const WHITE_OPAQUE := Color('aaffffff')
const WHITE_TRANSPARENT := Color('44ffffff')
const WHITE_SEMI_TRANSPARENT := Color('44ffffff')
const WHITE_TRANSPARENT := Color('00ffffff')
var score := 0 setget set_score
func _ready() -> void:
modulate = WHITE_TRANSPARENT
modulate = WHITE_SEMI_TRANSPARENT
func set_score(value) -> void:
......@@ -17,6 +18,15 @@ func set_score(value) -> void:
text = str(score)
tween.interpolate_property(
self, 'modulate',
WHITE_OPAQUE, WHITE_TRANSPARENT, 0.8,
WHITE_OPAQUE, WHITE_SEMI_TRANSPARENT, 0.8,
Tween.TRANS_QUAD, Tween.EASE_IN)
tween.start()
func fade_out():
tween.stop_all()
tween.interpolate_property(
self, 'modulate',
modulate, WHITE_TRANSPARENT, 0.6,
Tween.TRANS_QUAD, Tween.EASE_IN)
tween.start()
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