From 17a4804f1afe82fa68861aeece3969b97745907c Mon Sep 17 00:00:00 2001 From: adrienmalin <41926238+adrienmalin@users.noreply.github.com> Date: Wed, 2 Jan 2019 13:20:51 +0100 Subject: [PATCH] the signals --- FlashText.gd | 7 +++-- Main.gd | 76 ++++++++-------------------------------------- Main.tscn | 44 ++++++++++++++++----------- MidiPlayer.gd | 19 ++++++++++++ Stats.gd | 63 +++++++++++++++++++++++++++++++------- midi/MidiPlayer.gd | 4 ++- 6 files changed, 117 insertions(+), 96 deletions(-) create mode 100644 MidiPlayer.gd diff --git a/FlashText.gd b/FlashText.gd index f59c384..f54042b 100644 --- a/FlashText.gd +++ b/FlashText.gd @@ -4,10 +4,13 @@ var texts = PoolStringArray() func print(text): texts.append(text) - if texts.size() > 2: + if texts.size() > 5: texts.remove(0) $Label.text = texts.join("\n") $AnimationPlayer.play("Flash") func _on_AnimationPlayer_animation_finished(anim_name): - texts.resize(0) \ No newline at end of file + texts.resize(0) + +func _on_Stats_flash_text(text): + self.print(text) diff --git a/Main.gd b/Main.gd index 95602d0..2cf59cb 100644 --- a/Main.gd +++ b/Main.gd @@ -19,18 +19,6 @@ const movements = { "soft_drop": Vector3(0, -1, 0) } -const SCORES = [ - [0, 4, 1], - [1, 8, 2], - [3, 12], - [5, 16], - [8] -] -const LINES_CLEARED_NAMES = ["", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS"] -const T_SPIN_NAMES = ["", "T-SPIN", "MINI T-SPIN"] - -const LINE_CLEAR_MIDI_CHANNELS = [2, 6] - var random_bag = [] var next_piece = random_piece() @@ -42,6 +30,9 @@ var autoshift_action = "" var playing = true +signal piece_dropped(score) +signal piece_locked(lines, t_spin) + func random_piece(): if not random_bag: random_bag = [ @@ -62,14 +53,7 @@ func new_game(): $Stats.new_game() new_piece() resume() - new_level() - -func new_level(): $Stats.new_level() - $FlashText.print("Level\n%d"%$Stats.level) - $DropTimer.wait_time = pow(0.8 - (($Stats.level - 1) * 0.007), $Stats.level - 1) - if $Stats.level > 15: - $LockDelay.wait_time = 0.5 * pow(0.9, $Stats.level-15) func new_piece(): current_piece = next_piece @@ -125,15 +109,13 @@ func _on_AutoShiftTimer_timeout(): func process_autoshift_action(): if move(movements[autoshift_action]): if autoshift_action == "soft_drop": - $Stats.score += 1 - $Stats/HBC/VBC1/Score.text = str($Stats.score) + emit_signal("piece_dropped", 1) func hard_drop(): var score = 0 while move(movements["soft_drop"]): score += 2 - $Stats.score += score - $Stats/HBC/VBC1/Score.text = str($Stats.score) + emit_signal("piece_dropped", score) lock() func move(movement): @@ -158,50 +140,11 @@ func _on_LockDelay_timeout(): lock() func lock(): - var score $GridMap.lock(current_piece) remove_child(current_piece) - var lines_cleared = $GridMap.clear_lines() - if lines_cleared or current_piece.t_spin: - score = SCORES[lines_cleared][current_piece.t_spin] - $Stats.update_score(score) - if current_piece.t_spin: - $FlashText.print(T_SPIN_NAMES[current_piece.t_spin]) - if lines_cleared: - $FlashText.print(LINES_CLEARED_NAMES[lines_cleared]) - $FlashText.print(str(100*score)) - - # Combos - if lines_cleared: - $Stats.combos += 1 - if $Stats.combos > 0: - $Stats.score += (20 if lines_cleared==1 else 50) * $Stats.combos * $Stats.level - $Stats/HBC/VBC1/Score.text = str($Stats.score) - if $Stats.combos == 1: - $FlashText.print("COMBO") - else: - $FlashText.print("COMBO x%d"%$Stats.combos) - - # SFX - if lines_cleared == Tetromino.NB_MINOES: - for channel in LINE_CLEAR_MIDI_CHANNELS: - $MidiPlayer.channel_status[channel].vomume = 127 - $LineCLearTimer.wait_time = 0.86 - else: - for channel in LINE_CLEAR_MIDI_CHANNELS: - $MidiPlayer.channel_status[channel].vomume = 100 - $LineCLearTimer.wait_time = 0.43 - $MidiPlayer.unmute_channels(LINE_CLEAR_MIDI_CHANNELS) - $LineCLearTimer.start() - else: - $Stats.combos = -1 - if $Stats.goal <= 0: - new_level() + emit_signal("piece_locked", $GridMap.clear_lines(), current_piece.t_spin) new_piece() -func _on_LineCLearTimer_timeout(): - $MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS) - func hold(): if not current_piece_held: current_piece_held = true @@ -223,8 +166,8 @@ func resume(): $LockDelay.start() $Stats.time = OS.get_system_time_secs() - $Stats.time $Stats/Clock.start() + $MidiPlayer.mute_channels($MidiPlayer.LINE_CLEAR_MIDI_CHANNELS) $MidiPlayer.resume() - $MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS) $GridMap.visible = true next_piece.visible = true current_piece.visible = true @@ -253,3 +196,8 @@ func game_over(): func _notification(what): if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT: pause() + +func _on_Stats_level_up(): + $DropTimer.wait_time = pow(0.8 - (($Stats.level - 1) * 0.007), $Stats.level - 1) + if $Stats.level > 15: + $LockDelay.wait_time = 0.5 * pow(0.9, $Stats.level-15) diff --git a/Main.tscn b/Main.tscn index b1bb656..4ebacef 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,13 +1,14 @@ -[gd_scene load_steps=16 format=2] +[gd_scene load_steps=17 format=2] [ext_resource path="res://starmap_g8k.jpg" type="Texture" id=1] [ext_resource path="res://Main.gd" type="Script" id=2] [ext_resource path="res://Tetrominos/Mino/MinoLibrary.tres" type="MeshLibrary" id=3] [ext_resource path="res://GridMap.gd" type="Script" id=4] [ext_resource path="res://midi/MidiPlayer.tscn" type="PackedScene" id=5] -[ext_resource path="res://Stats.tscn" type="PackedScene" id=6] -[ext_resource path="res://FlashText.gd" type="Script" id=7] -[ext_resource path="res://fonts/525-ROUN.TTF" type="DynamicFontData" id=8] +[ext_resource path="res://MidiPlayer.gd" type="Script" id=6] +[ext_resource path="res://Stats.tscn" type="PackedScene" id=7] +[ext_resource path="res://FlashText.gd" type="Script" id=8] +[ext_resource path="res://fonts/525-ROUN.TTF" type="DynamicFontData" id=9] [sub_resource type="PanoramaSky" id=1] @@ -171,7 +172,7 @@ subdivide_depth = 0 size = 50 use_mipmaps = true use_filter = false -font_data = ExtResource( 8 ) +font_data = ExtResource( 9 ) _sections_unfolded = [ "Font", "Settings" ] [sub_resource type="Animation" id=7] @@ -369,26 +370,23 @@ wait_time = 0.03 one_shot = false autostart = false -[node name="LineCLearTimer" type="Timer" parent="." index="10"] +[node name="MidiPlayer" parent="." index="10" instance=ExtResource( 5 )] + +script = ExtResource( 6 ) +loop_start = 1.81 + +[node name="LineCLearTimer" type="Timer" parent="MidiPlayer" index="1"] process_mode = 1 wait_time = 1.41 one_shot = true autostart = false -[node name="MidiPlayer" parent="." index="11" instance=ExtResource( 5 )] - -file = "res://midi/Tetris - Song A.mid" -volume_db = -24 -loop = true -loop_start = 1.71 -soundfont = "res://midi/TimGM6mb.sf2" - -[node name="Stats" parent="." index="12" instance=ExtResource( 6 )] +[node name="Stats" parent="." index="11" instance=ExtResource( 7 )] visible = false -[node name="FlashText" type="Control" parent="." index="13"] +[node name="FlashText" type="Control" parent="." index="12"] anchor_left = 0.0 anchor_top = 0.0 @@ -402,7 +400,7 @@ mouse_filter = 0 mouse_default_cursor_shape = 0 size_flags_horizontal = 1 size_flags_vertical = 1 -script = ExtResource( 7 ) +script = ExtResource( 8 ) [node name="Label" type="Label" parent="FlashText" index="0"] @@ -440,6 +438,12 @@ anims/Flash = SubResource( 7 ) blend_times = [ ] _sections_unfolded = [ "Playback Options" ] +[connection signal="piece_dropped" from="." to="Stats" method="_on_Main_piece_dropped"] + +[connection signal="piece_locked" from="." to="Stats" method="_on_Main_piece_locked"] + +[connection signal="piece_locked" from="." to="MidiPlayer" method="_on_Main_piece_locked"] + [connection signal="timeout" from="DropTimer" to="." method="_on_DropTimer_timeout"] [connection signal="timeout" from="LockDelay" to="." method="_on_LockDelay_timeout"] @@ -448,7 +452,11 @@ _sections_unfolded = [ "Playback Options" ] [connection signal="timeout" from="AutoShiftTimer" to="." method="_on_AutoShiftTimer_timeout"] -[connection signal="timeout" from="LineCLearTimer" to="." method="_on_LineCLearTimer_timeout"] +[connection signal="timeout" from="MidiPlayer/LineCLearTimer" to="MidiPlayer" method="_on_LineCLearTimer_timeout"] + +[connection signal="flash_text" from="Stats" to="FlashText" method="_on_Stats_flash_text"] + +[connection signal="level_up" from="Stats" to="." method="_on_Stats_level_up"] [connection signal="animation_finished" from="FlashText/AnimationPlayer" to="FlashText" method="_on_AnimationPlayer_animation_finished"] diff --git a/MidiPlayer.gd b/MidiPlayer.gd new file mode 100644 index 0000000..883c34f --- /dev/null +++ b/MidiPlayer.gd @@ -0,0 +1,19 @@ +extends "midi/MidiPlayer.gd" + +const LINE_CLEAR_MIDI_CHANNELS = [2, 6] + +func _on_Main_piece_locked(lines, t_spin): + if lines or t_spin: + if lines == Tetromino.NB_MINOES: + for channel in LINE_CLEAR_MIDI_CHANNELS: + channel_status[channel].vomume = 127 + $LineCLearTimer.wait_time = 0.86 + else: + for channel in LINE_CLEAR_MIDI_CHANNELS: + channel_status[channel].vomume = 100 + $LineCLearTimer.wait_time = 0.43 + unmute_channels(LINE_CLEAR_MIDI_CHANNELS) + $LineCLearTimer.start() + +func _on_LineCLearTimer_timeout(): + mute_channels(LINE_CLEAR_MIDI_CHANNELS diff --git a/Stats.gd b/Stats.gd index 49e2067..69d2cba 100644 --- a/Stats.gd +++ b/Stats.gd @@ -1,6 +1,16 @@ extends MarginContainer -const password= "TETRIS 3000" +const password = "TETRIS 3000" + +const SCORES = [ + [0, 4, 1], + [1, 8, 2], + [3, 12], + [5, 16], + [8] +] +const LINES_CLEARED_NAMES = ["", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS"] +const T_SPIN_NAMES = ["", "T-SPIN", "MINI T-SPIN"] var level var goal @@ -9,6 +19,9 @@ var high_score var time var combos +signal flash_text(text) +signal level_up + func _ready(): var save_game = File.new() if not save_game.file_exists("user://high_score.save"): @@ -31,15 +44,8 @@ func new_level(): goal += 5 * level $HBC/VBC1/Level.text = str(level) $HBC/VBC1/Goal.text = str(goal) - -func update_score(new_score): - score += 100 * new_score - $HBC/VBC1/Score.text = str(score) - goal -= new_score - $HBC/VBC1/Goal.text = str(goal) - if score > high_score: - high_score = score - $HBC/VBC1/HighScore.text = str(high_score) + emit_signal("flash_text", "Level\n%d"%$level) + emit_signal("level_up") func _on_Clock_timeout(): var time_elapsed = OS.get_system_time_secs() - time @@ -55,4 +61,39 @@ func _notification(what): save_game.open_encrypted_with_pass("user://high_score.save", File.WRITE, password) save_game.store_line(str(high_score)) save_game.close() - get_tree().quit() \ No newline at end of file + get_tree().quit() + +func _on_Main_piece_dropped(score): + score += lines + $HBC/VBC1/Score.text = str(score) + +func _on_Main_piece_locked(lines, t_spin): + if lines or t_spin: + if t_spin: + $FlashText.print(T_SPIN_NAMES[current_piece.t_spin]) + if lines: + $FlashText.print(LINES_CLEARED_NAMES[lines_cleared]) + var ds = SCORES[lines_cleared][current_piece.t_spin] + goal -= ds + $HBC/VBC1/Goal.text = str(goal) + ds *= 100 + emit_signal("flash_text", str(ds)) + score += ds + $HBC/VBC1/Score.text = str(score) + if score > high_score: + high_score = score + $HBC/VBC1/HighScore.text = str(high_score) + # Combos + if lines: + combos += 1 + if combos > 0: + score += (20 if lines==1 else 50) * combos * level + $HBC/VBC1/Score.text = str(score) + if $Stats.combos == 1: + emit_signal("flash_text", "COMBO") + else: + emit_signal("flash_text", "COMBO x%d"%$Stats.combos) + else: + $Stats.combos = -1 + if $Stats.goal <= 0: + new_level() diff --git a/midi/MidiPlayer.gd b/midi/MidiPlayer.gd index 8c60006..c229db0 100644 --- a/midi/MidiPlayer.gd +++ b/midi/MidiPlayer.gd @@ -1,5 +1,7 @@ extends Node +const Tetromino = preload("res://Tetrominos/Tetromino.gd") + const max_track = 16 const max_channel = 16 const max_note_number = 128 @@ -419,4 +421,4 @@ func unmute_channels(channels): for channel_id in channels: channel_mute[channel_id] = false for note in muted_events[channel_id]: - _process_track_event_note_on(channel_status[channel_id], muted_events[channel_id][note]) \ No newline at end of file + _process_track_event_note_on(channel_status[channel_id], muted_events[channel_id][note]))