-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.gd
145 lines (109 loc) · 3.15 KB
/
global.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
extends Node
var start_time = OS.get_system_time_secs()
var time_passed = 0
var time_paused = 0
var last_pause_time = 0
var counter = 0
# Ramping up difficulty until this time (s), then constant.
var endgame_time = 240
var game_over = false
var screen_size = Vector2(
ProjectSettings.get_setting("display/window/size/width"),
ProjectSettings.get_setting("display/window/size/height"))
var score : int = 0
var combo : int = 0
var highest_combo : int = 0
var enemies_killed : int = 0
var music : bool = true
var first_run = true # Show info.
const HIGH_SCORES_FILE = "user://highscores.cfg"
# Stores as an array of arrays with score and name.
var high_scores = [
[25000, "Space Godette"],
[20000, "Juan Alpaca"],
[15000, "Minilens Mk II"],
[10000, "Billy Blaze"],
[9001, "Vegeta"],
[5000, "Melon Musk"],
]
func _ready():
# set pause mode to process, keep running when game is paused
set_pause_mode(PAUSE_MODE_PROCESS)
randomize()
load_high_scores()
if (randi() % 2):
$music1.play()
$music2.stop()
else:
$music1.stop()
$music2.play()
func _physics_process(_delta):
# Counter used to avoid calling OS functions too often.
counter += 1
if counter > 30:
counter = 0
time_passed = OS.get_system_time_secs() - start_time - time_paused
# toggle all music and sfx
if Input.is_action_just_pressed("toggle music") and music:
AudioServer.set_bus_mute(0,true)
music = false
elif Input.is_action_just_pressed("toggle music") and not music:
AudioServer.set_bus_mute(0,false)
music = true
func get_score_modifier():
return float(time_passed) / endgame_time
func get_difficulty_ratio():
return min(get_score_modifier(), 1.0)
func increase_score(base_score):
if game_over:
return
enemies_killed += 1
combo += 1
var score_mod = pow(1 + get_score_modifier(), 1.3)
score += base_score * combo * score_mod
if combo > highest_combo:
highest_combo = combo
func set_game_over():
game_over = true
high_scores.append([score, "YOU"])
high_scores.sort_custom(self, "sort_high_scores")
high_scores.pop_back()
save_high_scores()
func sort_high_scores(a, b):
return a[0] > b[0]
func reset():
start_time = OS.get_system_time_secs()
time_passed = 0
time_paused = 0
last_pause_time = start_time
game_over = false
score = 0
combo = 0
highest_combo = 0
enemies_killed = 0
func restart():
reset()
get_tree().reload_current_scene()
func pause(enable):
get_tree().set_pause(enable)
# Correct the passing of time while in menu.
if enable:
last_pause_time = OS.get_system_time_secs()
else:
time_paused += OS.get_system_time_secs() - last_pause_time
func load_high_scores():
var cfg = ConfigFile.new()
var err = cfg.load(HIGH_SCORES_FILE)
if err == OK: # Load high scores.
high_scores = cfg.get_value("highscores", "list", high_scores)
elif err == ERR_FILE_NOT_FOUND: # Save default high scores.
cfg.set_value("highscores", "list", high_scores)
cfg.save(HIGH_SCORES_FILE)
else:
print("An error occurred while loading high scores.")
func save_high_scores():
var cfg = ConfigFile.new()
cfg.set_value("highscores", "list", high_scores)
var err = cfg.save(HIGH_SCORES_FILE)
if err != OK:
print("An error occurred while saving high scores.")