-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.gd
257 lines (207 loc) · 6.99 KB
/
Game.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
extends Node2D
var song_id = "song_01"
var audio
var map
var audio_path = "res://songs/01/From The Dust - Supernova_CC_BY.mp3"
var map_path = "res://songs/01/From The Dust - Supernova_CC_BY.mboy"
var curr_bar_index = 0
var last_bar_index = null
var tempo
var bar_length_in_m
var quarter_time_in_sec
var speed
var note_scale
var start_pos_in_sec
var start_pos_in_px
var music_scn = preload("res://Music.tscn")
var music
var flow_scn = preload("res://Flow.tscn")
var flow
var is_ready = false
var is_finished = false
var total_score = 0
const SHOOT_LINE_Y = 645
var missed_notes_temp = 0
var heart_duration = GameSpace.heart_duration
onready var popup_screen = $PopupScreen
onready var header = $Header
var mc = MemoryCard
func _init():
GamePool.setup()
func _ready():
GameSpace.paused = false
GameSpace.failed = false
$Pointer.hide()
# curr_song is empty in debug mode
var curr_song = GameSpace.curr_song
if curr_song:
audio_path = curr_song.audio_path
map_path = curr_song.map_path
curr_bar_index = curr_song.start_index
last_bar_index = curr_song.end_index
song_id = curr_song.song_id
map = curr_song.map
audio = load(audio_path)
if !map: map = GameSpace.read_json_file(map_path)
setup()
$Target.connect("missed", self, "_on_missed_shot")
$Target.connect("hit", self, "_on_hit")
$Target.connect("bonus_hit", self, "_on_bonus_hit")
$BottomC/Area2D.connect("area_entered", self, "_on_bottom_area_enter")
$BottomC/Area2D.connect("area_exited", self, "_on_bottom_area_exited")
GameEvent.connect("no_lifes", self, "_on_no_lifes")
popup_screen.connect("menu_btn_press", self, "_on_menu_btn_press")
popup_screen.connect("replay_btn_press", self, "_on_replay_btn_press")
popup_screen.connect("play_btn_press", self, "_on_play_btn_press")
header.connect("pause_btn_press", self, "_on_pause_btn_press")
# memory card
mc.setup(song_id)
func setup():
print("start at:", curr_bar_index)
tempo = int(map.tempo)
bar_length_in_m = 1600 # Godot meters
quarter_time_in_sec = 60/float(tempo) # 60/60 = 1, 60/85 = 0.71
speed = bar_length_in_m/float(4*quarter_time_in_sec) # each bar has 4 quarters #
note_scale = bar_length_in_m/float(4*400) # 400 is the length of 1 quarter in MBOY Editor
start_pos_in_sec = (float(map.start_pos)/400.0) * quarter_time_in_sec
start_pos_in_px = start_pos_in_sec * speed
music = music_scn.instance()
music.audio = audio
music.speed = speed
music.tempo = tempo
# should include start_pos_in_px (related to start_pos_in_sec)
music.pre_start_length = bar_length_in_m + start_pos_in_px
# include start_pos_in_sec here
music.start_pos_in_sec = start_pos_in_sec + curr_bar_index*4*quarter_time_in_sec # we confirm that all bars have fixed length - 4 quarters
add_child(music)
var bars = map.tracks[0].bars
if last_bar_index == null || last_bar_index > bars.size()-1: last_bar_index = bars.size()-1
bars = bars.slice(curr_bar_index, last_bar_index)
bars.push_front({"index": -1, "quarters_count": 4, "notes": []})
flow = flow_scn.instance()
flow.note_scale = note_scale
flow.bar_length_in_m = bar_length_in_m
flow.speed = Vector2(0, speed)
flow.start_y = SHOOT_LINE_Y - start_pos_in_px
flow.set_bars_data(bars) # set bars data and make calculations
flow.connect("finished", self, "_on_flow_finished")
$BottomC.position.y = SHOOT_LINE_Y
$FlowC.add_child(flow)
header.setup_title(map.audio.artist, map.audio.title)
header.update_total_score(total_score)
header.hide_total_score()
header.setup_progress(music.get_length())
is_ready = true
func _process(delta):
if not is_ready or is_finished:
return
if GameSpace.paused:
return
flow.process_with_time(music.time, delta)
header.update_progress(music.get_playback_position())
func _notification(what):
if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT:
_on_pause_btn_press()
func _on_missed_shot(pos):
print("missed at:", pos)
var h = GamePool.get_instance("bullet_hole")
h.position = pos
$BulletHoleC.add_child(h)
func _on_hit(score, score_color, particle_color, note_size, pos, dir=1):
if abs(pos.y - SHOOT_LINE_Y) >= 120:
score = 0
elif abs(pos.y - SHOOT_LINE_Y) >= 60:
score = int(score/2.0)
var shot_particle = GamePool.get_instance("shot_particle")
shot_particle.position = pos
shot_particle.particle_color = particle_color
shot_particle.note_size = note_size
$ShotParticleC.add_child(shot_particle)
update_score(score, score_color, pos, dir)
func _on_bonus_hit(score, score_color, pos, dir=1):
update_score(score, score_color, pos, dir)
func update_score(score, score_color, pos, dir=1):
var shot_score = GamePool.get_instance("shot_score")
shot_score.score = score
shot_score.position = pos
shot_score.modulate = score_color
shot_score.dir = dir
$ShotScoreC.add_child(shot_score)
if score != 0:
total_score += score
if total_score < 0: total_score = 0
header.update_total_score(total_score)
header.show_total_score()
func _on_bottom_area_enter(area):
var n = area.get_parent()
if n.is_in_group("note") or n.is_in_group("instant"):
n.entered_bottom = true
func _on_bottom_area_exited(area):
var n = area.get_parent()
if n.is_in_group("note"): #or n.is_in_group("instant"):
if !n.is_collected:
if n.size == 'big':
# ignore note damage and use GameSpace params
missed_notes_temp += GameSpace.big_miss_damage
else:
missed_notes_temp += GameSpace.short_miss_damage
if missed_notes_temp >= heart_duration:
$LifesSet.lost_life()
missed_notes_temp = 0
$HurtScreen/AnimationPlayer.stop()
$HurtScreen/AnimationPlayer.play("hurt")
func _on_no_lifes():
#return
GameSpace.paused = true
GameSpace.failed = true
music.pause()
popup_screen.show_fail()
$Target.hide()
$Pointer.show()
func _on_menu_btn_press():
yield(get_tree().create_timer(0.2), "timeout")
SceneLoader.goto_scene("res://SongsMenu.tscn")
func _on_replay_btn_press():
# reload song to get config and map changes in dev mode
if OS.is_debug_build() and GameSpace.curr_song:
GameSpace.curr_song.reload_song()
yield(get_tree().create_timer(0.2), "timeout")
SceneLoader.goto_scene("res://Game.tscn")
func _on_pause_btn_press():
GameSpace.paused = true
music.pause()
popup_screen.show_pause()
$Target.hide()
$Pointer.show()
header.show_pause_btn(false)
func _on_play_btn_press():
GameSpace.paused = false
music.resume()
popup_screen.hide_all()
$Target.show()
$Pointer.hide()
header.show_pause_btn(true)
func _on_flow_finished():
is_finished = true
GameSpace.paused = true
music.pause()
$Target.hide()
$Pointer.show()
var progress_level = calc_progress_level()
print(total_score, "/", flow.max_score, " : ", progress_level)
popup_screen.show_success(total_score, progress_level)
mc.save_finished_track()
if mc.get_highscore() == null or (mc.get_highscore() != null and mc.get_highscore() < total_score):
mc.save_highscore(total_score)
mc.set_param("progress_level", progress_level)
mc.write()
#print(mc.data)
func calc_progress_level():
if total_score > flow.max_score+200:
return 3
elif total_score > flow.max_score*0.75:
return 2
elif total_score > 0:
return 1
else:
return 0