-
Notifications
You must be signed in to change notification settings - Fork 0
/
hall_of_fame.gd
155 lines (131 loc) · 3.88 KB
/
hall_of_fame.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (c) 2021, Ben Niemann <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
extends Control
signal closed
var new_pos = null
var opening = true
var closing = false
var editing = false
var editor = null
var scores = []
func _init() -> void:
load_scores()
func _ready() -> void:
refresh_scores()
$animations.play('fade_in')
$animations.seek(0, true)
var _unused = $animations.connect('animation_finished', self, '__fade_in_finished', [], CONNECT_ONESHOT)
func _unhandled_key_input(_event: InputEventKey) -> void:
if not opening and not closing and not editing:
closing = true
$animations.play('fade_out')
var _unused = $animations.connect('animation_finished', self, '__fade_out_finished', [], CONNECT_ONESHOT)
func set_score(score: int) -> void:
if score != null:
var pos = 0
for entry in scores:
if score > entry[1]:
new_pos = pos
break
pos += 1
if new_pos != null:
scores.insert(new_pos, ["", score])
while len(scores) > 10:
scores.pop_back()
refresh_scores()
func load_scores() -> void:
var fp = File.new()
if fp.file_exists("user://highscores.json"):
fp.open("user://highscores.json", File.READ)
scores = parse_json(fp.get_as_text())
fp.close()
else:
scores = [
["albert", 10000],
["stephen", 5000],
["pink", 2500],
["", 0],
["", 0],
["", 0],
["", 0],
["", 0],
["", 0],
["", 0],
]
func save_scores() -> void:
var fp = File.new()
fp.open("user://highscores.json", File.WRITE)
fp.store_string(to_json(scores))
fp.close()
func refresh_scores() -> void:
var box = $box/scores
editor = null
while box.get_child_count() > 0:
var child = box.get_child(0)
box.remove_child(child)
child.queue_free()
var idx = 1
for entry in scores:
var rank = Label.new()
rank.text = str(idx)
rank.align = Label.ALIGN_RIGHT
rank.size_flags_vertical = Control.SIZE_EXPAND
box.add_child(rank)
if idx - 1 == new_pos:
editor = LineEdit.new()
editor.placeholder_text = "Your Name..."
editor.caret_blink = true
editor.max_length = 12
editor.size_flags_horizontal = Control.SIZE_EXPAND_FILL
editor.size_flags_vertical = Control.SIZE_EXPAND
box.add_child(editor)
else:
var name = Label.new()
name.text = entry[0]
name.size_flags_horizontal = Control.SIZE_EXPAND
name.size_flags_vertical = Control.SIZE_EXPAND
box.add_child(name)
var score = Label.new()
score.text = str(entry[1]) if entry[1] > 0 else ""
score.align = Label.ALIGN_RIGHT
score.size_flags_vertical = Control.SIZE_EXPAND
box.add_child(score)
idx += 1
if editor != null:
editing = true
editor.connect('text_entered', self, "__editing_finished", [])
func __fade_in_finished(_anim) -> void:
if editor != null:
editor.grab_focus()
opening = false
if new_pos != null:
$message.text = "New Highscore!"
else:
$message.text = "Press Any Key to Continue"
$animations.play('msg_fade')
func __fade_out_finished(_anim) -> void:
emit_signal('closed')
func __editing_finished(name) -> void:
if len(name) > 0:
scores[new_pos][0] = name
save_scores()
new_pos = null
refresh_scores()
$animations.play_backwards('msg_fade')
yield ($animations, 'animation_finished')
editing = false
$message.text = "Press Any Key to Continue"
$animations.play('msg_fade')