-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_state.gd
More file actions
65 lines (58 loc) · 1.72 KB
/
Copy pathgame_state.gd
File metadata and controls
65 lines (58 loc) · 1.72 KB
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
extends Node
signal restart_game
signal coin_collected
var coins: int = 0
var total_coins: int = 0
var elapsed_time: float = 0.0
var is_level_running: bool = false
const HIGHSCORE_PATH = "user://highscores.json"
const MAX_HIGHSCORES = 10
func collect_coin() -> void:
coins += 1
coin_collected.emit()
func format_time(seconds: float) -> String:
var total_seconds: int = int(seconds)
var mins: int = total_seconds / 60
var secs: int = total_seconds % 60
return "%02d:%02d" % [mins, secs]
func get_highscores() -> Array:
if not FileAccess.file_exists(HIGHSCORE_PATH):
return []
var file = FileAccess.open(HIGHSCORE_PATH, FileAccess.READ)
if file == null:
return []
var data = JSON.parse_string(file.get_as_text())
file.close()
if not data is Array:
return []
var valid: Array = []
for entry in data:
if entry is Dictionary \
and entry.has("name") and entry["name"] is String \
and entry.has("time") and (entry["time"] is float or entry["time"] is int) \
and entry.has("coins") and (entry["coins"] is float or entry["coins"] is int):
valid.append({
"name": entry["name"],
"time": float(entry["time"]),
"coins": int(entry["coins"])
})
return valid
func save_highscore(player_name: String) -> bool:
player_name = player_name.strip_edges()
if player_name.is_empty():
player_name = "Anonymous"
var scores = get_highscores()
scores.append({
"name": player_name,
"time": elapsed_time,
"coins": coins
})
scores.sort_custom(func(a, b): return a["time"] < b["time"])
if scores.size() > MAX_HIGHSCORES:
scores.resize(MAX_HIGHSCORES)
var file = FileAccess.open(HIGHSCORE_PATH, FileAccess.WRITE)
if file == null:
return false
file.store_string(JSON.stringify(scores))
file.close()
return true