-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer_stats.gd
65 lines (48 loc) · 1.57 KB
/
player_stats.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
extends Node
signal max_health_changed(new_value)
signal health_changed(new_value)
signal score_changed(new_value)
signal equip_changed(new_texture)
var max_health: int = 5 setget set_maxhealth
var health: int = 5 setget set_health
var score: int = 0 setget set_score
var equip_texture: Texture = null setget set_equip
var player_path: NodePath setget set_playerpath
var player: Node2D = null setget ,get_player
func set_health(value: int):
health = clamp(value, 0, max_health)
emit_signal("health_changed", health)
func set_maxhealth(value: int):
var diff: int = value - max_health
max_health = max(1, value)
if health:
self.health = health + diff if diff > 0 else health
emit_signal("max_health_changed", max_health)
func set_score(value: int):
score = max(0, value)
emit_signal("score_changed", score)
func set_equip(value: Texture):
equip_texture = value
emit_signal("equip_changed", value)
func set_playerpath(value: NodePath):
player_path = value
if value:
player = get_node_or_null(player_path)
func get_player():
return player
func _on_player_dead():
# save player score
print("Saving")
var file = File.new()
var scores: Array = []
if file.file_exists(Options.HIGHSCORE_FILE):
file.open(Options.HIGHSCORE_FILE, file.READ)
var json = JSON.parse(file.get_as_text())
if json.error != 0:
push_warning("Error loading highscores")
scores = json.result as Array
file.close()
scores.push_back({"name": Options.player_name, "score": score, "date": OS.get_date()})
file.open(Options.HIGHSCORE_FILE, file.WRITE)
file.store_line(to_json(scores))
file.close()